api.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/domain"
  6. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  7. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  10. "git.sxidc.com/go-framework/baize/framework/core/tag/check"
  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. func (simple *Simple) Bind(binder *binding.Binder) {
  23. entity_crud.BindSimple(binder, &entity_crud.Simple[Info]{
  24. Entity: &Entity{},
  25. Schema: simple.Schema,
  26. CreateJsonBody: &CreateUserJsonBody{},
  27. DeleteQueryParams: &DeleteUserQueryParams{},
  28. UpdateJsonBody: &UpdateUserJsonBody{},
  29. QueryQueryParams: &GetUsersQueryParams{},
  30. GetByIDQueryParams: &GetUserQueryParams{},
  31. }, entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
  32. Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  33. userEntity, err := domain.ToConcrete[*Entity](e)
  34. if err != nil {
  35. return err
  36. }
  37. encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
  38. if err != nil {
  39. return err
  40. }
  41. userEntity.Password = encryptedPassword
  42. if strutils.IsStringEmpty(userEntity.Name) {
  43. userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
  44. }
  45. encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
  46. if err != nil {
  47. return err
  48. }
  49. userEntity.Phone = encryptedPhone
  50. encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
  51. if err != nil {
  52. return err
  53. }
  54. userEntity.Email = encryptedEmail
  55. checkResult := check.Struct(userEntity, fieldMap)
  56. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPassword)
  57. if err != nil {
  58. return err
  59. }
  60. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldName)
  61. if err != nil {
  62. return err
  63. }
  64. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPhone)
  65. if err != nil {
  66. return err
  67. }
  68. err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldEmail)
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. },
  74. }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
  75. Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  76. userEntity, err := domain.ToConcrete[*Entity](e)
  77. if err != nil {
  78. return err
  79. }
  80. if strutils.IsStringNotEmpty(userEntity.Password) {
  81. encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
  82. if err != nil {
  83. return err
  84. }
  85. userEntity.Password = encryptedPassword
  86. }
  87. if strutils.IsStringEmpty(userEntity.Name) {
  88. userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
  89. }
  90. if strutils.IsStringNotEmpty(userEntity.Phone) {
  91. encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
  92. if err != nil {
  93. return err
  94. }
  95. userEntity.Phone = encryptedPhone
  96. }
  97. if strutils.IsStringNotEmpty(userEntity.Email) {
  98. encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
  99. if err != nil {
  100. return err
  101. }
  102. userEntity.Email = encryptedEmail
  103. }
  104. checkResult := check.Struct(userEntity, fieldMap)
  105. if strutils.IsStringNotEmpty(userEntity.Password) {
  106. err := domain.CheckField(checkResult, e.DomainCNName(), FieldPassword)
  107. if err != nil {
  108. return err
  109. }
  110. }
  111. if strutils.IsStringNotEmpty(userEntity.Name) {
  112. err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. if strutils.IsStringNotEmpty(userEntity.Phone) {
  118. err := domain.CheckField(checkResult, e.DomainCNName(), FieldPhone)
  119. if err != nil {
  120. return err
  121. }
  122. }
  123. if strutils.IsStringNotEmpty(userEntity.Email) {
  124. err := domain.CheckField(checkResult, e.DomainCNName(), FieldEmail)
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. return nil
  130. },
  131. }), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
  132. if value == nil {
  133. return true, nil
  134. }
  135. switch fieldName {
  136. case FieldUserName:
  137. userName, ok := value.(string)
  138. if !ok {
  139. return false, errors.New("用户名字段类型不是string")
  140. }
  141. if strutils.IsStringNotEmpty(userName) {
  142. conditions.Like(ColumnUserName, "%"+userName+"%")
  143. }
  144. return true, nil
  145. case FieldName:
  146. name, ok := value.(string)
  147. if !ok {
  148. return false, errors.New("姓名字段类型不是string")
  149. }
  150. if strutils.IsStringNotEmpty(name) {
  151. conditions.Like(ColumnName, "%"+name+"%")
  152. }
  153. return true, nil
  154. case FieldPhone:
  155. phone, ok := value.(string)
  156. if !ok {
  157. return false, errors.New("手机号字段类型不是string")
  158. }
  159. if strutils.IsStringNotEmpty(phone) {
  160. encryptedPassword, err := encoding.AESEncrypt(phone, simple.AESKey)
  161. if err != nil {
  162. return false, err
  163. }
  164. conditions.Equal(ColumnPassword, encryptedPassword)
  165. }
  166. return true, nil
  167. case FieldEmail:
  168. email, ok := value.(string)
  169. if !ok {
  170. return false, errors.New("邮箱字段类型不是string")
  171. }
  172. if strutils.IsStringNotEmpty(email) {
  173. encryptedPassword, err := encoding.AESEncrypt(email, simple.AESKey)
  174. if err != nil {
  175. return false, err
  176. }
  177. conditions.Equal(ColumnPassword, encryptedPassword)
  178. }
  179. return true, nil
  180. default:
  181. return false, nil
  182. }
  183. }))
  184. }