-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
51 lines (47 loc) · 1.24 KB
/
login.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
package controllers
import (
"github.com/astaxie/beego"
"encoding/json"
"github.com/astaxie/beego/orm"
"golang.org/x/crypto/bcrypt"
"library/common"
"models"
"time"
)
type LoginController struct {
BaseController
}
func (c *LoginController) Post() {
//哈希校验成功后 更新 auth_key
beego.Info(string(c.Ctx.Input.RequestBody))
postData := map[string]string{"user_password": "", "user_name": ""}
err := json.Unmarshal(c.Ctx.Input.RequestBody, &postData)
if err != nil {
c.SetJson(1, nil, "数据格式错误")
return
}
password := postData["user_password"]
userName := postData["user_name"]
if userName == "" || password == "" {
c.SetJson(1, nil, "用户名或密码不存在")
return
}
var user models.User
o := orm.NewOrm()
err = o.Raw("SELECT * FROM `user` WHERE username= ?", userName).QueryRow(&user)
beego.Info(user)
err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password))
if err != nil {
c.SetJson(1, nil, "用户名或密码错误")
return
} else {
if user.AuthKey == "" {
userAuth := common.Md5String(user.Username + common.GetString(time.Now().Unix()))
user.AuthKey = userAuth
models.UpdateUserById(&user)
}
user.PasswordHash = ""
c.SetJson(0, user, "")
return
}
}