Browse Source

feat: 编辑个人资料

runningwater 3 months ago
parent
commit
105e3923c3
2 changed files with 77 additions and 31 deletions
  1. 1 1
      .air.toml
  2. 76 30
      app/requests/user_request.go

+ 1 - 1
.air.toml

@@ -7,7 +7,7 @@ tmp_dir = "tmp"
   bin = "./tmp/gohub"
   cmd = "go build -o ./tmp/gohub ."
   delay = 1000
-  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
+  exclude_dir = ["assets", "tmp", "vendor", "testdata","storage"]
   exclude_file = []
   exclude_regex = ["_test.go"]
   exclude_unchanged = false

+ 76 - 30
app/requests/user_request.go

@@ -1,41 +1,87 @@
 package requests
 
 import (
-	"github.com/gin-gonic/gin"
-	"github.com/thedevsaddam/govalidator"
+    "github.com/gin-gonic/gin"
+    "github.com/thedevsaddam/govalidator"
 
-	"github.com/runningwater/gohub/pkg/auth"
+    "github.com/runningwater/gohub/app/requests/validators"
+    "github.com/runningwater/gohub/pkg/auth"
 )
 
 type UserUpdateProfileRequest struct {
-	Name         string `valid:"name" json:"name"`
-	City         string `valid:"city" json:"city"`
-	Introduction string `valid:"introduction" json:"introduction"`
+    Name         string `valid:"name" json:"name"`
+    City         string `valid:"city" json:"city"`
+    Introduction string `valid:"introduction" json:"introduction"`
 }
 
 func UserUpdateProfile(data any, c *gin.Context) map[string][]string {
-	// 查询用户名重复时,过滤当前用户ID
-	uid := auth.CurrentUID(c)
-	rules := govalidator.MapData{
-		"name":         []string{"required", "alpha_num", "between:3,20", "not_exists:users,name," + uid},
-		"city":         []string{"min_cn:2", "max_cn:20"},
-		"introduction": []string{"min_cn:4", "max_cn:240"},
-	}
-	messages := govalidator.MapData{
-		"name": []string{
-			"required:用户名不能为空",
-			"alpha_num:用户名格式错误",
-			"between:3,20:用户名长度需在 3-20 之间",
-			"not_exists:用户名已存在",
-		},
-		"city": []string{
-			"min_cn:城市长度需至少 2 个字符",
-			"max_cn:城市长度不能超过 20 个字符",
-		},
-		"introduction": []string{
-			"min_cn:简介长度需至少 4 个字符",
-			"max_cn:简介长度不能超过 240 个字符",
-		},
-	}
-	return validate(data, rules, messages)
+    // 查询用户名重复时,过滤当前用户ID
+    uid := auth.CurrentUID(c)
+    rules := govalidator.MapData{
+        "name":         []string{"required", "alpha_num", "between:3,20", "not_exists:users,name," + uid},
+        "city":         []string{"min_cn:2", "max_cn:20"},
+        "introduction": []string{"min_cn:4", "max_cn:240"},
+    }
+    messages := govalidator.MapData{
+        "name": []string{
+            "required:用户名不能为空",
+            "alpha_num:用户名格式错误",
+            "between:3,20:用户名长度需在 3-20 之间",
+            "not_exists:用户名已存在",
+        },
+        "city": []string{
+            "min_cn:城市长度需至少 2 个字符",
+            "max_cn:城市长度不能超过 20 个字符",
+        },
+        "introduction": []string{
+            "min_cn:简介长度需至少 4 个字符",
+            "max_cn:简介长度不能超过 240 个字符",
+        },
+    }
+    return validate(data, rules, messages)
+}
+
+type UserUpdateEmailRequest struct {
+    Email      string `valid:"email" json:"email"`
+    VerifyCode string `valid:"verify_code" json:"verify_code,omitempty"`
+}
+
+// UserUpdateEmail 处理用户邮箱更新请求
+// 参数:
+//   data - 请求数据,应为 UserUpdateEmailRequest 类型
+//   c - Gin 上下文对象
+// 返回值:
+//   map[string][]string - 包含验证错误的映射
+func UserUpdateEmail(data any, c *gin.Context) map[string][]string {
+    currentUser := auth.CurrentUser(c)
+    rules := govalidator.MapData{
+        "email": []string{
+            "required",
+            "min:4",
+            "max:30",
+            "email",
+            "not_exists:users,email," + currentUser.GetStringID(),
+            "not_in:" + currentUser.Email,
+        },
+        "verify_code": []string{"required", "digits:6"},
+    }
+    messages := govalidator.MapData{
+        "email": []string{
+            "required:邮箱不能为空",
+            "min:邮箱长度需至少 4 个字符",
+            "max:邮箱长度不能超过 30 个字符",
+            "email:邮箱格式错误",
+            "not_exists:邮箱已存在",
+            "not_in:新邮箱与当前邮箱相同",
+        },
+        "verify_code": []string{
+            "required:验证码不能为空",
+            "digits:6:验证码长度必须为 6 位数字",
+        },
+    }
+    errs := validate(data, rules, messages)
+    _data := data.(*UserUpdateEmailRequest)
+    errs = validators.ValidateVerifyCode(_data.Email, _data.VerifyCode, errs)
+
+    return errs
 }