api.go 6.9 KB

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