Skip to content

Commit cb3b38f

Browse files
committed
修改密码
1 parent f83a198 commit cb3b38f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

app/http/controllers/api/v1/users_controller.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,25 @@ func (ctrl *UsersController) UpdatePhone(c *gin.Context) {
8888
response.Abort500(c, "更新失败,请稍后尝试~")
8989
}
9090
}
91+
92+
func (ctrl *UsersController) UpdatePassword(c *gin.Context) {
93+
94+
request := requests.UserUpdatePasswordRequest{}
95+
if ok := requests.Validate(c, &request, requests.UserUpdatePassword); !ok {
96+
return
97+
}
98+
99+
currentUser := auth.CurrentUser(c)
100+
// 验证原始密码是否正确
101+
_, err := auth.Attempt(currentUser.Name, request.Password)
102+
if err != nil {
103+
// 失败,显示错误提示
104+
response.Unauthorized(c, "原密码不正确")
105+
} else {
106+
// 更新密码为新密码
107+
currentUser.Password = request.NewPassword
108+
currentUser.Save()
109+
110+
response.Success(c)
111+
}
112+
}

app/requests/user_request.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,38 @@ func UserUpdatePhone(data interface{}, c *gin.Context) map[string][]string {
120120

121121
return errs
122122
}
123+
124+
type UserUpdatePasswordRequest struct {
125+
Password string `valid:"password" json:"password,omitempty"`
126+
NewPassword string `valid:"new_password" json:"new_password,omitempty"`
127+
NewPasswordConfirm string `valid:"new_password_confirm" json:"new_password_confirm,omitempty"`
128+
}
129+
130+
func UserUpdatePassword(data interface{}, c *gin.Context) map[string][]string {
131+
rules := govalidator.MapData{
132+
"password": []string{"required", "min:6"},
133+
"new_password": []string{"required", "min:6"},
134+
"new_password_confirm": []string{"required", "min:6"},
135+
}
136+
messages := govalidator.MapData{
137+
"password": []string{
138+
"required:密码为必填项",
139+
"min:密码长度需大于 6",
140+
},
141+
"new_password": []string{
142+
"required:密码为必填项",
143+
"min:密码长度需大于 6",
144+
},
145+
"new_password_confirm": []string{
146+
"required:确认密码框为必填项",
147+
"min:确认密码长度需大于 6",
148+
},
149+
}
150+
151+
// 确保 comfirm 密码正确
152+
errs := validate(data, rules, messages)
153+
_data := data.(*UserUpdatePasswordRequest)
154+
errs = validators.ValidatePasswordConfirm(_data.NewPassword, _data.NewPasswordConfirm, errs)
155+
156+
return errs
157+
}

routes/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func RegisterAPIRoutes(r *gin.Engine) {
6262
usersGroup.PUT("", middlewares.AuthJWT(), uc.UpdateProfile)
6363
usersGroup.PUT("/email", middlewares.AuthJWT(), uc.UpdateEmail)
6464
usersGroup.PUT("/phone", middlewares.AuthJWT(), uc.UpdatePhone)
65+
usersGroup.PUT("/password", middlewares.AuthJWT(), uc.UpdatePassword)
6566
}
6667

6768
cgc := new(controllers.CategoriesController)

0 commit comments

Comments
 (0)