api.go 8.3 KB

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