From 9a7b53c031f08f84d1b4a3ad078797e4be8563ed Mon Sep 17 00:00:00 2001 From: ouqiang Date: Sun, 30 Apr 2017 20:50:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AF=86=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/user.go | 7 +++ routers/user/user.go | 31 ++++++++++++++ templates/common/header.html | 3 +- templates/user/editPassword.html | 73 ++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 templates/user/editPassword.html diff --git a/models/user.go b/models/user.go index 5ae94e2..64dd24a 100644 --- a/models/user.go +++ b/models/user.go @@ -41,6 +41,13 @@ func (user *User) Update(id int, data CommonMap) (int64, error) { return Db.Table(user).ID(id).Update(data) } +func (user *User) UpdatePassword(id int, password string) (int64, error) { + salt := user.generateSalt() + safePassword := user.encryptPassword(password, salt) + + return user.Update(id, CommonMap{"password": safePassword, "salt": salt}) +} + // 删除 func (user *User) Delete(id int) (int64, error) { return Db.Id(id).Delete(user) diff --git a/routers/user/user.go b/routers/user/user.go index 9bfd146..d00fef0 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -16,6 +16,37 @@ func Login(ctx *macaron.Context) { ctx.HTML(200, "user/login") } +func EditPassword(ctx *macaron.Context) { + ctx.Data["Title"] = "修改密码" + ctx.HTML(200, "user/editPassword") +} + +func UpdatePassword(ctx *macaron.Context, sess session.Store) string { + oldPassword := ctx.QueryTrim("old_password") + newPassword := ctx.QueryTrim("new_password") + confirmNewPassword := ctx.QueryTrim("confirm_new_password") + json := utils.JsonResponse{} + if oldPassword == "" || newPassword == "" || confirmNewPassword == "" { + return json.CommonFailure("原密码和新密码均不能为空") + } + if newPassword != confirmNewPassword { + return json.CommonFailure("两次输入密码不一致") + } + if oldPassword == newPassword { + return json.CommonFailure("原密码与新密码不能相同") + } + userModel := new(models.User) + if !userModel.Match(Username(sess), oldPassword) { + return json.CommonFailure("原密码输入错误") + } + _, err := userModel.UpdatePassword(Uid(sess), newPassword) + if err != nil { + return json.CommonFailure("修改失败") + } + + return json.Success("修改成功", nil) +} + func ValidateLogin(ctx *macaron.Context, sess session.Store) string { username := ctx.QueryTrim("username") password := ctx.QueryTrim("password") diff --git a/templates/common/header.html b/templates/common/header.html index 16c660d..80ca88a 100644 --- a/templates/common/header.html +++ b/templates/common/header.html @@ -46,6 +46,7 @@ 你好,{{{.LoginUsername}}} {{{end}}} @@ -61,7 +62,7 @@ 主机 {{{if gt .LoginUid 0}}} - 管理 + 配置 {{{end}}} diff --git a/templates/user/editPassword.html b/templates/user/editPassword.html new file mode 100644 index 0000000..a9714aa --- /dev/null +++ b/templates/user/editPassword.html @@ -0,0 +1,73 @@ +{{{ template "common/header" . }}} + + + +{{{ template "common/footer" . }}} \ No newline at end of file