api.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package user
  2. import (
  3. "git.sxidc.com/go-framework/baize/convenient/entity_crud"
  4. "git.sxidc.com/go-framework/baize/framework/binding"
  5. "git.sxidc.com/go-framework/baize/framework/core/api"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain"
  8. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  10. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  11. "git.sxidc.com/go-tools/utils/encoding"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. "github.com/pkg/errors"
  14. )
  15. // Simple Bind参数
  16. type Simple struct {
  17. // schema
  18. Schema string
  19. // AES加密用到的Key
  20. AESKey string
  21. // 鉴权中间件
  22. AuthMiddleware binding.Middleware
  23. }
  24. func (simple *Simple) Bind(binder *binding.Binder) {
  25. entity_crud.BindSimple(binder, &entity_crud.Simple[Info]{
  26. Entity: &Entity{},
  27. Schema: simple.Schema,
  28. CreateJsonBody: &CreateUserJsonBody{},
  29. DeleteQueryParams: &DeleteUserQueryParams{},
  30. UpdateJsonBody: &UpdateUserJsonBody{},
  31. QueryQueryParams: &GetUsersQueryParams{},
  32. GetByIDQueryParams: &GetUserQueryParams{},
  33. }, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware),
  34. entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
  35. Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  36. userEntity, err := domain.ToConcrete[*Entity](e)
  37. if err != nil {
  38. return nil, err
  39. }
  40. encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
  41. if err != nil {
  42. return nil, errors.New(err.Error())
  43. }
  44. userEntity.Password = encryptedPassword
  45. if strutils.IsStringEmpty(userEntity.Name) {
  46. userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
  47. }
  48. encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
  49. if err != nil {
  50. return nil, errors.New(err.Error())
  51. }
  52. userEntity.Phone = encryptedPhone
  53. encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
  54. if err != nil {
  55. return nil, errors.New(err.Error())
  56. }
  57. userEntity.Email = encryptedEmail
  58. return nil, nil
  59. },
  60. }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  61. Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  62. userEntity, err := domain.ToConcrete[*Entity](e)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if strutils.IsStringNotEmpty(userEntity.Password) {
  67. encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
  68. if err != nil {
  69. return nil, errors.New(err.Error())
  70. }
  71. userEntity.Password = encryptedPassword
  72. }
  73. if strutils.IsStringEmpty(userEntity.Name) {
  74. userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
  75. }
  76. if strutils.IsStringNotEmpty(userEntity.Phone) {
  77. encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
  78. if err != nil {
  79. return nil, errors.New(err.Error())
  80. }
  81. userEntity.Phone = encryptedPhone
  82. }
  83. if strutils.IsStringNotEmpty(userEntity.Email) {
  84. encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
  85. if err != nil {
  86. return nil, errors.New(err.Error())
  87. }
  88. userEntity.Email = encryptedEmail
  89. }
  90. return nil, nil
  91. },
  92. }), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
  93. if value == nil {
  94. return true, nil
  95. }
  96. switch fieldName {
  97. case FieldUserName:
  98. userName, ok := value.(string)
  99. if !ok {
  100. return false, errors.New("用户名字段类型不是string")
  101. }
  102. if strutils.IsStringNotEmpty(userName) {
  103. conditions.Like(ColumnUserName, "%"+userName+"%")
  104. }
  105. return true, nil
  106. case FieldName:
  107. name, ok := value.(string)
  108. if !ok {
  109. return false, errors.New("姓名字段类型不是string")
  110. }
  111. if strutils.IsStringNotEmpty(name) {
  112. conditions.Like(ColumnName, "%"+name+"%")
  113. }
  114. return true, nil
  115. case FieldPhone:
  116. phone, ok := value.(string)
  117. if !ok {
  118. return false, errors.New("手机号字段类型不是string")
  119. }
  120. if strutils.IsStringNotEmpty(phone) {
  121. encryptedPhone, err := encoding.AESEncrypt(phone, simple.AESKey)
  122. if err != nil {
  123. return false, errors.New(err.Error())
  124. }
  125. conditions.Equal(ColumnPhone, encryptedPhone)
  126. }
  127. return true, nil
  128. case FieldEmail:
  129. email, ok := value.(string)
  130. if !ok {
  131. return false, errors.New("邮箱字段类型不是string")
  132. }
  133. if strutils.IsStringNotEmpty(email) {
  134. encryptedEmail, err := encoding.AESEncrypt(email, simple.AESKey)
  135. if err != nil {
  136. return false, errors.New(err.Error())
  137. }
  138. conditions.Equal(ColumnEmail, encryptedEmail)
  139. }
  140. return true, nil
  141. default:
  142. return false, nil
  143. }
  144. }), entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[Info]{
  145. OnSuccessReturn: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[Info]) (response.InfosData[Info], error) {
  146. errResponse := response.InfosData[Info]{
  147. Infos: make([]Info, 0),
  148. }
  149. retInfos := make([]Info, len(output.Infos))
  150. infos := output.Infos
  151. for index, info := range infos {
  152. if strutils.IsStringNotEmpty(info.Phone) {
  153. decryptedPhone, err := encoding.AESDecrypt(info.Phone, simple.AESKey)
  154. if err != nil {
  155. return errResponse, errors.New(err.Error())
  156. }
  157. info.Phone = decryptedPhone
  158. }
  159. if strutils.IsStringNotEmpty(info.Email) {
  160. decryptedEmail, err := encoding.AESDecrypt(info.Email, simple.AESKey)
  161. if err != nil {
  162. return errResponse, errors.New(err.Error())
  163. }
  164. info.Email = decryptedEmail
  165. }
  166. retInfos[index] = info
  167. }
  168. return response.InfosData[Info]{
  169. Infos: retInfos,
  170. TotalCount: output.TotalCount,
  171. PageNo: output.PageNo,
  172. }, nil
  173. },
  174. }), entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[Info]{
  175. OnSuccessReturn: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure, output Info) (Info, error) {
  176. if strutils.IsStringNotEmpty(output.Phone) {
  177. decryptedPhone, err := encoding.AESDecrypt(output.Phone, simple.AESKey)
  178. if err != nil {
  179. return Info{}, errors.New(err.Error())
  180. }
  181. output.Phone = decryptedPhone
  182. }
  183. if strutils.IsStringNotEmpty(output.Email) {
  184. decryptedEmail, err := encoding.AESDecrypt(output.Email, simple.AESKey)
  185. if err != nil {
  186. return Info{}, errors.New(err.Error())
  187. }
  188. output.Email = decryptedEmail
  189. }
  190. return output, nil
  191. },
  192. }))
  193. }