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/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" "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql" "git.sxidc.com/go-framework/baize/framework/core/tag/check" "git.sxidc.com/go-tools/utils/encoding" "git.sxidc.com/go-tools/utils/strutils" ) // Simple Bind参数 type Simple struct { // schema Schema string // AES加密用到的Key AESKey string } func (simple *Simple) Bind(binder *binding.Binder) { // TODO 定制需要加密的字段逻辑,Name赋值默认值 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.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 err } 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 err } userEntity.Phone = encryptedPhone encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey) if err != nil { return err } 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 } if strutils.IsStringNotEmpty(userEntity.Password) { encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey) if err != nil { return err } 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 err } userEntity.Phone = encryptedPhone } if strutils.IsStringNotEmpty(userEntity.Email) { encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey) if err != nil { return err } userEntity.Email = encryptedEmail } checkResult := check.Struct(userEntity, fieldMap) if strutils.IsStringNotEmpty(userEntity.Password) { err := domain.CheckField(checkResult, e.DomainCNName(), FieldPassword) if err != nil { return err } } if strutils.IsStringNotEmpty(userEntity.Name) { err := domain.CheckField(checkResult, e.DomainCNName(), FieldName) if err != nil { return err } } 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) { switch fieldName { case FieldPassword: case FieldName: case FieldPhone: case FieldEmail: default: return false } })) }