Browse Source

完成用户接口定制

yjp 1 year ago
parent
commit
76ec427400
1 changed files with 58 additions and 4 deletions
  1. 58 4
      convenient/domain/auth/user/api.go

+ 58 - 4
convenient/domain/auth/user/api.go

@@ -11,6 +11,7 @@ import (
 	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
 	"git.sxidc.com/go-tools/utils/encoding"
 	"git.sxidc.com/go-tools/utils/strutils"
+	"github.com/pkg/errors"
 )
 
 // Simple Bind参数
@@ -23,7 +24,6 @@ type Simple struct {
 }
 
 func (simple *Simple) Bind(binder *binding.Binder) {
-	// TODO 定制需要加密的字段逻辑,Name赋值默认值
 	entity_crud.BindSimple(binder, &entity_crud.Simple[Info]{
 		Entity:             &Entity{},
 		Schema:             simple.Schema,
@@ -158,14 +158,68 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 
 			return nil
 		},
-	}), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool) {
+	}), 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 FieldPassword:
+		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) {
+				encryptedPassword, err := encoding.AESEncrypt(phone, simple.AESKey)
+				if err != nil {
+					return false, err
+				}
+
+				conditions.Equal(ColumnPassword, encryptedPassword)
+			}
+
+			return true, nil
 		case FieldEmail:
+			email, ok := value.(string)
+			if !ok {
+				return false, errors.New("邮箱字段类型不是string")
+			}
+
+			if strutils.IsStringNotEmpty(email) {
+				encryptedPassword, err := encoding.AESEncrypt(email, simple.AESKey)
+				if err != nil {
+					return false, err
+				}
+
+				conditions.Equal(ColumnPassword, encryptedPassword)
+			}
+
+			return true, nil
 		default:
-			return false
+			return false, nil
 		}
 	}))
 }