package user import ( "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" "git.sxidc.com/go-framework/baize/framework/core/api/request" "git.sxidc.com/go-framework/baize/framework/core/api/response" "git.sxidc.com/go-framework/baize/framework/core/domain" "git.sxidc.com/go-framework/baize/framework/core/domain/entity" "git.sxidc.com/go-framework/baize/framework/core/infrastructure" "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql" "git.sxidc.com/go-tools/utils/encoding" "git.sxidc.com/go-tools/utils/strutils" "github.com/pkg/errors" ) // Simple Bind参数 type Simple struct { // schema Schema string // AES加密用到的Key AESKey string // 鉴权中间件 AuthMiddleware binding.Middleware } func (simple *Simple) Bind(binder *binding.Binder) { entity_crud.BindSimple(binder, &entity_crud.Simple[Info]{ Entity: &Entity{}, Schema: simple.Schema, CreateJsonBody: &CreateUserJsonBody{}, DeleteQueryParams: &DeleteUserQueryParams{}, UpdateJsonBody: &UpdateUserJsonBody{}, QueryQueryParams: &GetUsersQueryParams{}, GetByIDQueryParams: &GetUserQueryParams{}, }, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware), entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{ Prepare: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) { userEntity, err := domain.ToConcrete[*Entity](e) if err != nil { return nil, err } encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey) if err != nil { return nil, 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 nil, errors.New(err.Error()) } userEntity.Phone = encryptedPhone encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey) if err != nil { return nil, errors.New(err.Error()) } userEntity.Email = encryptedEmail return nil, nil }, }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{ Prepare: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) { userEntity, err := domain.ToConcrete[*Entity](e) if err != nil { return nil, err } if strutils.IsStringNotEmpty(userEntity.Password) { encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey) if err != nil { return nil, errors.New(err.Error()) } userEntity.Password = encryptedPassword } 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 nil, errors.New(err.Error()) } userEntity.Phone = encryptedPhone } if strutils.IsStringNotEmpty(userEntity.Email) { encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey) if err != nil { return nil, errors.New(err.Error()) } userEntity.Email = encryptedEmail } return nil, 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") } 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+"%") } return true, nil case FieldPhone: phone, ok := value.(string) if !ok { return false, errors.New("手机号字段类型不是string") } if strutils.IsStringNotEmpty(phone) { encryptedPhone, err := encoding.AESEncrypt(phone, simple.AESKey) if err != nil { return false, errors.New(err.Error()) } conditions.Equal(ColumnPhone, encryptedPhone) } return true, nil case FieldEmail: email, ok := value.(string) if !ok { return false, errors.New("邮箱字段类型不是string") } if strutils.IsStringNotEmpty(email) { encryptedEmail, err := encoding.AESEncrypt(email, simple.AESKey) if err != nil { return false, errors.New(err.Error()) } conditions.Equal(ColumnEmail, encryptedEmail) } return true, nil default: return false, nil } }), entity_crud.WithQueryCallbacks(&entity_crud.QueryCallbacks[Info]{ OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[Info]) (response.InfosData[Info], error) { errResponse := response.InfosData[Info]{ Infos: make([]Info, 0), } retInfos := make([]Info, len(output.Infos)) infos := output.Infos for index, info := range infos { if strutils.IsStringNotEmpty(info.Phone) { decryptedPhone, err := encoding.AESDecrypt(info.Phone, simple.AESKey) if err != nil { return errResponse, errors.New(err.Error()) } info.Phone = decryptedPhone } if strutils.IsStringNotEmpty(info.Email) { decryptedEmail, err := encoding.AESDecrypt(info.Email, simple.AESKey) if err != nil { return errResponse, errors.New(err.Error()) } info.Email = decryptedEmail } retInfos[index] = info } return response.InfosData[Info]{ Infos: retInfos, TotalCount: output.TotalCount, PageNo: output.PageNo, }, nil }, }), entity_crud.WithGetByIDCallbacks(&entity_crud.GetByIDCallbacks[Info]{ OnSuccessReturn: func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output Info) (Info, error) { if strutils.IsStringNotEmpty(output.Phone) { decryptedPhone, err := encoding.AESDecrypt(output.Phone, simple.AESKey) if err != nil { return Info{}, errors.New(err.Error()) } output.Phone = decryptedPhone } if strutils.IsStringNotEmpty(output.Email) { decryptedEmail, err := encoding.AESDecrypt(output.Email, simple.AESKey) if err != nil { return Info{}, errors.New(err.Error()) } output.Email = decryptedEmail } return output, nil }, })) }