|
|
@@ -0,0 +1,115 @@
|
|
|
+package requests
|
|
|
+
|
|
|
+import (
|
|
|
+ "mime/multipart"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/thedevsaddam/govalidator"
|
|
|
+
|
|
|
+ "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"`
|
|
|
+}
|
|
|
+
|
|
|
+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)
|
|
|
+}
|
|
|
+
|
|
|
+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
|
|
|
+}
|
|
|
+
|
|
|
+type UserUpdateAvatarRequest struct {
|
|
|
+ Avatar *multipart.FileHeader `valid:"avatar" form:"avatar"`
|
|
|
+}
|
|
|
+
|
|
|
+func UserUpdateAvatar(data any, c *gin.Context) map[string][]string {
|
|
|
+ // size 的单位为 bytes
|
|
|
+ // - 1024 bytes 为 1 KB
|
|
|
+ // - 1024 * 1024 bytes 为 1 MB
|
|
|
+ // - 1024 * 1024 * 1024 bytes 为 1 GB
|
|
|
+ // - 20971520 bytes 为 20 MB
|
|
|
+ rules := govalidator.MapData{
|
|
|
+ "file:avatar": []string{"ext:png,jpg,jpeg", "size:20971520", "required"},
|
|
|
+ }
|
|
|
+ messages := govalidator.MapData{
|
|
|
+ "file:avatar": []string{
|
|
|
+ "ext:头像格式错误,只能为 png,jpg,jpeg 一种",
|
|
|
+ "siz:头像大小不能超过 20 MB",
|
|
|
+ "required:头像不能为空",
|
|
|
+ },
|
|
|
+ }
|
|
|
+ return validateFile(c, data, rules, messages)
|
|
|
+}
|