-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
82 lines (76 loc) · 2.32 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package admin
import (
"github.com/royburns/foodarts/models"
"strconv"
"strings"
)
type AccountController struct {
baseController
}
//登录
func (this *AccountController) Login() {
if this.GetString("dosubmit") == "yes" {
account := strings.TrimSpace(this.GetString("account"))
password := strings.TrimSpace(this.GetString("password"))
remember := this.GetString("remember")
if account != "" && password != "" {
var user models.User
user.Username = account
if user.Read("username") != nil || user.Password != models.Md5([]byte(password)) {
this.Data["errmsg"] = "帐号或密码错误"
} else if user.Active == 0 {
this.Data["errmsg"] = "该帐号未激活"
} else {
user.Logincount += 1
user.Lastip = this.getClientIp()
user.Lastlogin = this.getTime()
user.Update()
authkey := models.Md5([]byte(this.getClientIp() + "|" + user.Password))
if remember == "yes" {
this.Ctx.SetCookie("auth", strconv.FormatInt(user.Id, 10)+"|"+authkey, 7*86400)
} else {
this.Ctx.SetCookie("auth", strconv.FormatInt(user.Id, 10)+"|"+authkey)
}
this.Redirect("/admin", 302)
}
}
}
this.TplNames = "admin/account_login.html"
}
//退出登录
func (this *AccountController) Logout() {
this.Ctx.SetCookie("auth", "")
this.Redirect("/admin/login", 302)
}
//资料修改
func (this *AccountController) Profile() {
user := models.User{Id: this.userid}
if err := user.Read(); err != nil {
this.showmsg(err.Error())
}
if this.Ctx.Request.Method == "POST" {
errmsg := make(map[string]string)
password := strings.TrimSpace(this.GetString("password"))
newpassword := strings.TrimSpace(this.GetString("newpassword"))
newpassword2 := strings.TrimSpace(this.GetString("newpassword2"))
updated := false
if newpassword != "" {
if password == "" || models.Md5([]byte(password)) != user.Password {
errmsg["password"] = "当前密码错误"
} else if len(newpassword) < 6 {
errmsg["newpassword"] = "密码长度不能少于6个字符"
} else if newpassword != newpassword2 {
errmsg["newpassword2"] = "两次输入的密码不一致"
}
if len(errmsg) == 0 {
user.Password = models.Md5([]byte(newpassword))
user.Update("password")
updated = true
}
}
this.Data["updated"] = updated
this.Data["errmsg"] = errmsg
}
this.Data["user"] = user
this.display()
}