api.go 7.7 KB

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