api.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/response"
  6. "git.sxidc.com/go-framework/baize/framework/core/domain"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  10. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  11. "git.sxidc.com/go-framework/baize/framework/core/tag/check"
  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. Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  37. userEntity, err := domain.ToConcrete[*Entity](e)
  38. if err != nil {
  39. return err
  40. }
  41. encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
  42. if err != nil {
  43. return 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 errors.New(err.Error())
  52. }
  53. userEntity.Phone = encryptedPhone
  54. encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
  55. if err != nil {
  56. return errors.New(err.Error())
  57. }
  58. userEntity.Email = encryptedEmail
  59. checkResult := check.Struct(userEntity, fieldMap)
  60. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPassword)
  61. if err != nil {
  62. return err
  63. }
  64. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldName)
  65. if err != nil {
  66. return err
  67. }
  68. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPhone)
  69. if err != nil {
  70. return err
  71. }
  72. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldEmail)
  73. if err != nil {
  74. return err
  75. }
  76. return nil
  77. },
  78. }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  79. Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  80. userEntity, err := domain.ToConcrete[*Entity](e)
  81. if err != nil {
  82. return err
  83. }
  84. if strutils.IsStringNotEmpty(userEntity.Password) {
  85. encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
  86. if err != nil {
  87. return errors.New(err.Error())
  88. }
  89. userEntity.Password = encryptedPassword
  90. }
  91. if strutils.IsStringEmpty(userEntity.Name) {
  92. userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
  93. }
  94. if strutils.IsStringNotEmpty(userEntity.Phone) {
  95. encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
  96. if err != nil {
  97. return errors.New(err.Error())
  98. }
  99. userEntity.Phone = encryptedPhone
  100. }
  101. if strutils.IsStringNotEmpty(userEntity.Email) {
  102. encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
  103. if err != nil {
  104. return errors.New(err.Error())
  105. }
  106. userEntity.Email = encryptedEmail
  107. }
  108. checkResult := check.Struct(userEntity, fieldMap)
  109. if strutils.IsStringNotEmpty(userEntity.Password) {
  110. err := domain.CheckField(checkResult, e.DomainCNName(), FieldPassword)
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. if strutils.IsStringNotEmpty(userEntity.Name) {
  116. err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. if strutils.IsStringNotEmpty(userEntity.Phone) {
  122. err := domain.CheckField(checkResult, e.DomainCNName(), FieldPhone)
  123. if err != nil {
  124. return err
  125. }
  126. }
  127. if strutils.IsStringNotEmpty(userEntity.Email) {
  128. err := domain.CheckField(checkResult, e.DomainCNName(), FieldEmail)
  129. if err != nil {
  130. return err
  131. }
  132. }
  133. return nil
  134. },
  135. }), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
  136. if value == nil {
  137. return true, nil
  138. }
  139. switch fieldName {
  140. case FieldUserName:
  141. userName, ok := value.(string)
  142. if !ok {
  143. return false, errors.New("用户名字段类型不是string")
  144. }
  145. if strutils.IsStringNotEmpty(userName) {
  146. conditions.Like(ColumnUserName, "%"+userName+"%")
  147. }
  148. return true, nil
  149. case FieldName:
  150. name, ok := value.(string)
  151. if !ok {
  152. return false, errors.New("姓名字段类型不是string")
  153. }
  154. if strutils.IsStringNotEmpty(name) {
  155. conditions.Like(ColumnName, "%"+name+"%")
  156. }
  157. return true, nil
  158. case FieldPhone:
  159. phone, ok := value.(string)
  160. if !ok {
  161. return false, errors.New("手机号字段类型不是string")
  162. }
  163. if strutils.IsStringNotEmpty(phone) {
  164. encryptedPhone, err := encoding.AESEncrypt(phone, simple.AESKey)
  165. if err != nil {
  166. return false, errors.New(err.Error())
  167. }
  168. conditions.Equal(ColumnPhone, encryptedPhone)
  169. }
  170. return true, nil
  171. case FieldEmail:
  172. email, ok := value.(string)
  173. if !ok {
  174. return false, errors.New("邮箱字段类型不是string")
  175. }
  176. if strutils.IsStringNotEmpty(email) {
  177. encryptedEmail, err := encoding.AESEncrypt(email, simple.AESKey)
  178. if err != nil {
  179. return false, errors.New(err.Error())
  180. }
  181. conditions.Equal(ColumnEmail, encryptedEmail)
  182. }
  183. return true, nil
  184. default:
  185. return false, nil
  186. }
  187. }), entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[Info]{
  188. OnSuccessReturn: func(e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[Info]) (response.InfosData[Info], error) {
  189. errResponse := response.InfosData[Info]{
  190. Infos: make([]Info, 0),
  191. }
  192. retInfos := make([]Info, len(output.Infos))
  193. infos := output.Infos
  194. for index, info := range infos {
  195. if strutils.IsStringNotEmpty(info.Phone) {
  196. decryptedPhone, err := encoding.AESDecrypt(info.Phone, simple.AESKey)
  197. if err != nil {
  198. return errResponse, errors.New(err.Error())
  199. }
  200. info.Phone = decryptedPhone
  201. }
  202. if strutils.IsStringNotEmpty(info.Email) {
  203. decryptedEmail, err := encoding.AESDecrypt(info.Email, simple.AESKey)
  204. if err != nil {
  205. return errResponse, errors.New(err.Error())
  206. }
  207. info.Email = decryptedEmail
  208. }
  209. retInfos[index] = info
  210. }
  211. return response.InfosData[Info]{
  212. Infos: retInfos,
  213. TotalCount: output.TotalCount,
  214. PageNo: output.PageNo,
  215. }, nil
  216. },
  217. }), entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[Info]{
  218. OnSuccessReturn: func(e entity.Entity, i *infrastructure.Infrastructure, output Info) (Info, error) {
  219. if strutils.IsStringNotEmpty(output.Name) {
  220. encryptedName, err := encoding.AESEncrypt(output.Name, simple.AESKey)
  221. if err != nil {
  222. return Info{}, errors.New(err.Error())
  223. }
  224. output.Name = encryptedName
  225. }
  226. if strutils.IsStringNotEmpty(output.Email) {
  227. encryptedEmail, err := encoding.AESEncrypt(output.Email, simple.AESKey)
  228. if err != nil {
  229. return Info{}, errors.New(err.Error())
  230. }
  231. output.Email = encryptedEmail
  232. }
  233. return output, nil
  234. },
  235. }))
  236. }