看下流程圖(由flowchart.js繪制)
表設(shè)計
- name 關(guān)聯(lián)用戶表,表內(nèi)唯一
- uuid 隨機uuid抑诸,并通過sha1 加密
- etime 過期時間
type ForGetPwd struct {
Id int
Name *User `orm:"rel(fk);unique"`
Uuid string
Etime time.Time
}
發(fā)送找回密碼郵件代碼
用戶輸入郵箱,填寫驗證碼,信息發(fā)送到服務(wù)端
系統(tǒng)判斷前端提交信息,驗證是否通過亚再,郵箱是否存在
func (self *UserController) ForGetPwd() {
email, vercode, captcha_id := self.Input().Get("email"), self.Input().Get("vercode"), self.Input().Get("captcha_id")
if !CheckCode(vercode, captcha_id) {
msg := map[string]interface{}{"code": 1, "msg": "驗證碼錯誤"}
self.Data["json"] = &msg
self.ServeJSON()
return
}
//通過郵箱判斷用戶是否存在
if models.IsUserExitByEmail(email) {
_, user := models.FindUserByEmail(email)
uuid := Encrypt(email + Getuuid())
//當(dāng)前時間
now := time.Now()
//設(shè)置過期時間,這里設(shè)置1小時后過期
h, _ := time.ParseDuration("1h")
//添加時間
m := now.Add(h)
//是否第一次找回密碼,不是則更新表記錄的uuid,過期時間,否則添加
if models.IsExitForGetPwdByuser(user.Id) {
forgetpwd := models.FindForGetPwdByuser(user.Id)
forgetpwd.Uuid = uuid
forgetpwd.Etime = m
models.UpdateForGetPwd(&forgetpwd)
} else {
forgetpwd := models.ForGetPwd{Uuid: uuid, Name: &models.User{Id: user.Id}, Etime: m}
models.AddForGetPwd(&forgetpwd)
}
//發(fā)送找回密碼郵件
url := "http://192.168.1.12:8080/forgetpwd/?uuid=" + uuid
SendMail(email, "<h2>請點擊以下鏈接重置密碼,如非本人操作請忽略:</h2><p><a href="+url+">"+url+"</a>", "重置密碼")
msg := map[string]interface{}{"code": 0, "msg": "success"}
self.Data["json"] = &msg
self.ServeJSON()
} else {
msg := map[string]interface{}{"code": 1, "msg": "郵箱不存在"}
self.Data["json"] = &msg
self.ServeJSON()
}
}
用戶收到郵件,點擊鏈接重置密碼
把uuid回傳到重設(shè)密碼頁面撮珠,以便前端發(fā)送uuid
func (self *UserController) ForGetPwdPage() {
//用戶點擊重置密碼鏈接,需要把uuid回傳
uuid := self.Input().Get("uuid")
self.Data["uuid"] = uuid
self.TplName = "user/forgetpwd.html"
}
//更新密碼
func (self *UserController) SetNewPwd() {
uuid, password := self.Input().Get("uuid"), self.Input().Get("password")
now := time.Now()
//檢測uuid是否有效,有效便更新密碼,否則直接返回
if models.CheckForGet(uuid, now) {
//通過uuid查找對應(yīng)要修改密碼的用戶
u := models.FindForGetPwdByUuid(uuid)
user := models.FindUserDetialById(u.Name.Id)
user.Password = password
models.UpdateUser(&user)
msg := map[string]interface{}{"code": 0, "msg": "success"}
self.Data["json"] = &msg
self.ServeJSON()
}
msg := map[string]interface{}{"code": 1, "msg": "invalid token"}
self.Data["json"] = &msg
self.ServeJSON()
}
檢測uuid是否有效
//檢測uuid是否過期
func CheckForGet(uuid string, t time.Time) bool {
o := orm.NewOrm()
var forgetpwd ForGetPwd
return o.QueryTable(forgetpwd).Filter("UUID", uuid).Filter("Etime__gte", t).Exist()
}
js代碼片段
layui.define(['layer', 'form'], function(exports) {
var layer = layui.layer
var form = layui.form()
var $ = layui.jquery
form.verify({
password: [/(.+){6,12}$/, '密碼必須6到12位'],
});
form.on('submit(forgetpwd)', function(data) {
if (data.field.password != data.field.repassword) {
layer.msg("兩次密碼輸入不一致!")
return false;
}
$.ajax({
async: false,
url: "/user/setnewpwd",
data: {
"password": data.field.password,
"repassword":data.field.password,
"uuid":data.field.uuid,
},
type: 'POST',
success: function(text) {
if (text.msg == 'success') {
location.href = '/'
} else if (text.code != 0) {
layer.msg(text.msg)
}
}
});
return false;
});
exports('forgetpwd', {});
});