|
|
@@ -1,6 +1,7 @@
|
|
|
package user
|
|
|
|
|
|
import (
|
|
|
+ "git.sxidc.com/go-framework/baize/convenient/domain/auth/middlewares"
|
|
|
"git.sxidc.com/go-framework/baize/convenient/entity_crud"
|
|
|
"git.sxidc.com/go-framework/baize/framework/binding"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/api/response"
|
|
|
@@ -33,253 +34,258 @@ func (simple *Simple) Bind(binder *binding.Binder) {
|
|
|
UpdateJsonBody: &UpdateUserJsonBody{},
|
|
|
QueryQueryParams: &GetUsersQueryParams{},
|
|
|
GetByIDQueryParams: &GetUserQueryParams{},
|
|
|
- }, entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
|
|
|
- Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
|
|
|
- userEntity, err := domain.ToConcrete[*Entity](e)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- userEntity.Password = encryptedPassword
|
|
|
-
|
|
|
- if strutils.IsStringEmpty(userEntity.Name) {
|
|
|
- userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
|
|
|
- }
|
|
|
-
|
|
|
- encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- userEntity.Phone = encryptedPhone
|
|
|
-
|
|
|
- encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- userEntity.Email = encryptedEmail
|
|
|
-
|
|
|
- checkResult := check.Struct(userEntity, fieldMap)
|
|
|
-
|
|
|
- err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPassword)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldName)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPhone)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldEmail)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- },
|
|
|
- }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
|
|
|
- Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
|
|
|
- userEntity, err := domain.ToConcrete[*Entity](e)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+ }, entity_crud.WithCreateMiddlewares(middlewares.Authentication()),
|
|
|
+ entity_crud.WithDeleteMiddlewares(middlewares.Authentication()),
|
|
|
+ entity_crud.WithUpdateMiddlewares(middlewares.Authentication()),
|
|
|
+ entity_crud.WithQueryMiddlewares[Info](middlewares.Authentication()),
|
|
|
+ entity_crud.WithGetByIDMiddlewares[Info](middlewares.Authentication()),
|
|
|
+ entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
|
|
|
+ Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
|
|
|
+ userEntity, err := domain.ToConcrete[*Entity](e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userEntity.Password) {
|
|
|
encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
|
|
|
if err != nil {
|
|
|
return errors.New(err.Error())
|
|
|
}
|
|
|
|
|
|
userEntity.Password = encryptedPassword
|
|
|
- }
|
|
|
|
|
|
- if strutils.IsStringEmpty(userEntity.Name) {
|
|
|
- userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
|
|
|
- }
|
|
|
+ if strutils.IsStringEmpty(userEntity.Name) {
|
|
|
+ userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
|
|
|
+ }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userEntity.Phone) {
|
|
|
encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
|
|
|
if err != nil {
|
|
|
return errors.New(err.Error())
|
|
|
}
|
|
|
|
|
|
userEntity.Phone = encryptedPhone
|
|
|
- }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userEntity.Email) {
|
|
|
encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
|
|
|
if err != nil {
|
|
|
return errors.New(err.Error())
|
|
|
}
|
|
|
|
|
|
userEntity.Email = encryptedEmail
|
|
|
- }
|
|
|
|
|
|
- checkResult := check.Struct(userEntity, fieldMap)
|
|
|
+ checkResult := check.Struct(userEntity, fieldMap)
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userEntity.Password) {
|
|
|
- err := domain.CheckField(checkResult, e.DomainCNName(), FieldPassword)
|
|
|
+ err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPassword)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userEntity.Name) {
|
|
|
- err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
|
|
|
+ err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldName)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userEntity.Phone) {
|
|
|
- err := domain.CheckField(checkResult, e.DomainCNName(), FieldPhone)
|
|
|
+ err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPhone)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userEntity.Email) {
|
|
|
- err := domain.CheckField(checkResult, e.DomainCNName(), FieldEmail)
|
|
|
+ err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldEmail)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- return nil
|
|
|
- },
|
|
|
- }), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
|
|
|
- if value == nil {
|
|
|
- return true, nil
|
|
|
- }
|
|
|
-
|
|
|
- switch fieldName {
|
|
|
- case FieldUserName:
|
|
|
- userName, ok := value.(string)
|
|
|
- if !ok {
|
|
|
- return false, errors.New("用户名字段类型不是string")
|
|
|
- }
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
|
|
|
+ Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
|
|
|
+ userEntity, err := domain.ToConcrete[*Entity](e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(userName) {
|
|
|
- conditions.Like(ColumnUserName, "%"+userName+"%")
|
|
|
- }
|
|
|
+ if strutils.IsStringNotEmpty(userEntity.Password) {
|
|
|
+ encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New(err.Error())
|
|
|
+ }
|
|
|
|
|
|
- return true, nil
|
|
|
- case FieldName:
|
|
|
- name, ok := value.(string)
|
|
|
- if !ok {
|
|
|
- return false, errors.New("姓名字段类型不是string")
|
|
|
- }
|
|
|
+ userEntity.Password = encryptedPassword
|
|
|
+ }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(name) {
|
|
|
- conditions.Like(ColumnName, "%"+name+"%")
|
|
|
- }
|
|
|
+ if strutils.IsStringEmpty(userEntity.Name) {
|
|
|
+ userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8]
|
|
|
+ }
|
|
|
|
|
|
- return true, nil
|
|
|
- case FieldPhone:
|
|
|
- phone, ok := value.(string)
|
|
|
- if !ok {
|
|
|
- return false, errors.New("手机号字段类型不是string")
|
|
|
- }
|
|
|
+ if strutils.IsStringNotEmpty(userEntity.Phone) {
|
|
|
+ encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New(err.Error())
|
|
|
+ }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(phone) {
|
|
|
- encryptedPassword, err := encoding.AESEncrypt(phone, simple.AESKey)
|
|
|
- if err != nil {
|
|
|
- return false, errors.New(err.Error())
|
|
|
+ userEntity.Phone = encryptedPhone
|
|
|
}
|
|
|
|
|
|
- conditions.Equal(ColumnPassword, encryptedPassword)
|
|
|
- }
|
|
|
+ if strutils.IsStringNotEmpty(userEntity.Email) {
|
|
|
+ encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New(err.Error())
|
|
|
+ }
|
|
|
|
|
|
- return true, nil
|
|
|
- case FieldEmail:
|
|
|
- email, ok := value.(string)
|
|
|
- if !ok {
|
|
|
- return false, errors.New("邮箱字段类型不是string")
|
|
|
- }
|
|
|
+ userEntity.Email = encryptedEmail
|
|
|
+ }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(email) {
|
|
|
- encryptedPassword, err := encoding.AESEncrypt(email, simple.AESKey)
|
|
|
- if err != nil {
|
|
|
- return false, errors.New(err.Error())
|
|
|
+ checkResult := check.Struct(userEntity, fieldMap)
|
|
|
+
|
|
|
+ if strutils.IsStringNotEmpty(userEntity.Password) {
|
|
|
+ err := domain.CheckField(checkResult, e.DomainCNName(), FieldPassword)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- conditions.Equal(ColumnPassword, encryptedPassword)
|
|
|
- }
|
|
|
+ if strutils.IsStringNotEmpty(userEntity.Name) {
|
|
|
+ err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return true, nil
|
|
|
- default:
|
|
|
- return false, nil
|
|
|
- }
|
|
|
- }), entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[Info]{
|
|
|
- OnSuccessReturn: func(e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[Info]) (response.InfosData[Info], error) {
|
|
|
- errResponse := response.InfosData[Info]{
|
|
|
- Infos: make([]Info, 0),
|
|
|
+ if strutils.IsStringNotEmpty(userEntity.Phone) {
|
|
|
+ err := domain.CheckField(checkResult, e.DomainCNName(), FieldPhone)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringNotEmpty(userEntity.Email) {
|
|
|
+ err := domain.CheckField(checkResult, e.DomainCNName(), FieldEmail)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ }), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
|
|
|
+ if value == nil {
|
|
|
+ return true, nil
|
|
|
}
|
|
|
|
|
|
- retInfos := make([]Info, len(output.Infos))
|
|
|
- infos := output.Infos
|
|
|
+ switch fieldName {
|
|
|
+ case FieldUserName:
|
|
|
+ userName, ok := value.(string)
|
|
|
+ if !ok {
|
|
|
+ return false, errors.New("用户名字段类型不是string")
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringNotEmpty(userName) {
|
|
|
+ conditions.Like(ColumnUserName, "%"+userName+"%")
|
|
|
+ }
|
|
|
+
|
|
|
+ return true, nil
|
|
|
+ case FieldName:
|
|
|
+ name, ok := value.(string)
|
|
|
+ if !ok {
|
|
|
+ return false, errors.New("姓名字段类型不是string")
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringNotEmpty(name) {
|
|
|
+ conditions.Like(ColumnName, "%"+name+"%")
|
|
|
+ }
|
|
|
|
|
|
- for index, info := range infos {
|
|
|
- if strutils.IsStringNotEmpty(info.Name) {
|
|
|
- encryptedName, err := encoding.AESEncrypt(info.Name, simple.AESKey)
|
|
|
+ return true, nil
|
|
|
+ case FieldPhone:
|
|
|
+ phone, ok := value.(string)
|
|
|
+ if !ok {
|
|
|
+ return false, errors.New("手机号字段类型不是string")
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringNotEmpty(phone) {
|
|
|
+ encryptedPassword, err := encoding.AESEncrypt(phone, simple.AESKey)
|
|
|
if err != nil {
|
|
|
- return errResponse, errors.New(err.Error())
|
|
|
+ return false, errors.New(err.Error())
|
|
|
}
|
|
|
|
|
|
- info.Name = encryptedName
|
|
|
+ conditions.Equal(ColumnPassword, encryptedPassword)
|
|
|
+ }
|
|
|
+
|
|
|
+ return true, nil
|
|
|
+ case FieldEmail:
|
|
|
+ email, ok := value.(string)
|
|
|
+ if !ok {
|
|
|
+ return false, errors.New("邮箱字段类型不是string")
|
|
|
}
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(info.Email) {
|
|
|
- encryptedEmail, err := encoding.AESEncrypt(info.Email, simple.AESKey)
|
|
|
+ if strutils.IsStringNotEmpty(email) {
|
|
|
+ encryptedPassword, err := encoding.AESEncrypt(email, simple.AESKey)
|
|
|
if err != nil {
|
|
|
- return errResponse, errors.New(err.Error())
|
|
|
+ return false, errors.New(err.Error())
|
|
|
}
|
|
|
|
|
|
- info.Email = encryptedEmail
|
|
|
+ conditions.Equal(ColumnPassword, encryptedPassword)
|
|
|
}
|
|
|
|
|
|
- retInfos[index] = info
|
|
|
+ return true, nil
|
|
|
+ default:
|
|
|
+ return false, nil
|
|
|
}
|
|
|
+ }), entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[Info]{
|
|
|
+ OnSuccessReturn: func(e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[Info]) (response.InfosData[Info], error) {
|
|
|
+ errResponse := response.InfosData[Info]{
|
|
|
+ Infos: make([]Info, 0),
|
|
|
+ }
|
|
|
|
|
|
- return response.InfosData[Info]{
|
|
|
- Infos: retInfos,
|
|
|
- TotalCount: output.TotalCount,
|
|
|
- PageNo: output.PageNo,
|
|
|
- }, nil
|
|
|
- },
|
|
|
- }), entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[Info]{
|
|
|
- OnSuccessReturn: func(e entity.Entity, i *infrastructure.Infrastructure, output Info) (Info, error) {
|
|
|
- if strutils.IsStringNotEmpty(output.Name) {
|
|
|
- encryptedName, err := encoding.AESEncrypt(output.Name, simple.AESKey)
|
|
|
- if err != nil {
|
|
|
- return Info{}, errors.New(err.Error())
|
|
|
+ retInfos := make([]Info, len(output.Infos))
|
|
|
+ infos := output.Infos
|
|
|
+
|
|
|
+ for index, info := range infos {
|
|
|
+ if strutils.IsStringNotEmpty(info.Name) {
|
|
|
+ encryptedName, err := encoding.AESEncrypt(info.Name, simple.AESKey)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ info.Name = encryptedName
|
|
|
+ }
|
|
|
+
|
|
|
+ if strutils.IsStringNotEmpty(info.Email) {
|
|
|
+ encryptedEmail, err := encoding.AESEncrypt(info.Email, simple.AESKey)
|
|
|
+ if err != nil {
|
|
|
+ return errResponse, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ info.Email = encryptedEmail
|
|
|
+ }
|
|
|
+
|
|
|
+ retInfos[index] = info
|
|
|
}
|
|
|
|
|
|
- output.Name = encryptedName
|
|
|
- }
|
|
|
+ return response.InfosData[Info]{
|
|
|
+ Infos: retInfos,
|
|
|
+ TotalCount: output.TotalCount,
|
|
|
+ PageNo: output.PageNo,
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ }), entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[Info]{
|
|
|
+ OnSuccessReturn: func(e entity.Entity, i *infrastructure.Infrastructure, output Info) (Info, error) {
|
|
|
+ if strutils.IsStringNotEmpty(output.Name) {
|
|
|
+ encryptedName, err := encoding.AESEncrypt(output.Name, simple.AESKey)
|
|
|
+ if err != nil {
|
|
|
+ return Info{}, errors.New(err.Error())
|
|
|
+ }
|
|
|
|
|
|
- if strutils.IsStringNotEmpty(output.Email) {
|
|
|
- encryptedEmail, err := encoding.AESEncrypt(output.Email, simple.AESKey)
|
|
|
- if err != nil {
|
|
|
- return Info{}, errors.New(err.Error())
|
|
|
+ output.Name = encryptedName
|
|
|
}
|
|
|
|
|
|
- output.Email = encryptedEmail
|
|
|
- }
|
|
|
+ if strutils.IsStringNotEmpty(output.Email) {
|
|
|
+ encryptedEmail, err := encoding.AESEncrypt(output.Email, simple.AESKey)
|
|
|
+ if err != nil {
|
|
|
+ return Info{}, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ output.Email = encryptedEmail
|
|
|
+ }
|
|
|
|
|
|
- return output, nil
|
|
|
- },
|
|
|
- }))
|
|
|
+ return output, nil
|
|
|
+ },
|
|
|
+ }))
|
|
|
}
|