api.go 8.1 KB

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