forked from TruthHun/DocHub
/
UserController.go
116 lines (92 loc) · 2.59 KB
/
UserController.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package WeixinControllers
import (
"fmt"
// "path/filepath"
// "github.com/astaxie/beego"
// "strings"
// "time"
// "os"
"github.com/hawkwang/DocHub/helper"
// "github.com/hawkwang/DocHub/helper/conv"
"github.com/hawkwang/DocHub/models"
// "github.com/astaxie/beego/orm"
"github.com/astaxie/beego/validation"
)
type UserController struct {
BaseController
}
func (this *UserController) Prepare() {
this.BaseController.Prepare()
this.Xsrf()
}
func (this *UserController) GetToken() {
var (
currenttoken string
tokens []interface{}
)
currenttoken = this.XSRFToken()
tokens = append(tokens, currenttoken)
fmt.Println(tokens)
this.ResponseJson(true, "获得token", tokens)
}
//用户登录
func (this *UserController) Login() {
if this.IsLogin > 0 {
this.Redirect("/user", 302)
return
}
// GET 请求
if this.Ctx.Request.Method == "GET" {
this.Data["Seo"] = models.NewSeo().GetByPage("PC-Login", "会员登录", "会员登录", "会员登录", this.Sys.Site)
this.Data["IsUser"] = true
this.Data["PageId"] = "wenku-reg"
this.TplName = "login.html"
return
}
type Post struct {
Email, Password string
}
var post struct {
Email, Password string
}
this.ParseForm(&post)
valid := validation.Validation{}
res := valid.Email(post.Email, "Email")
if !res.Ok {
this.ResponseJson(false, "登录失败,邮箱格式不正确")
}
ModelUser := models.NewUser()
users, rows, err := ModelUser.UserList(1, 1, "", "", "u.`email`=? and u.`password`=?", post.Email, helper.MD5Crypt(post.Password))
if rows == 0 || err != nil {
if err != nil {
helper.Logger.Error(err.Error())
}
this.ResponseJson(false, "登录失败,邮箱或密码不正确")
}
user := users[0]
this.IsLogin = helper.Interface2Int(user["Id"])
if this.IsLogin > 0 {
//查询用户有没有被封禁
if info := ModelUser.UserInfo(this.IsLogin); info.Status == false { //被封禁了
this.ResponseJson(false, "登录失败,您的账号已被管理员禁用")
}
this.BaseController.SetCookieLogin(this.IsLogin)
this.ResponseJson(true, "登录成功")
}
this.ResponseJson(false, "登录失败,未知错误!")
}
//用户退出登录
func (this *UserController) Logout() {
this.ResetCookie()
if v, ok := this.Ctx.Request.Header["X-Requested-With"]; ok && v[0] == "XMLHttpRequest" {
this.ResponseJson(true, "退出登录成功")
}
this.Redirect("/", 302)
}
// 检测用户是否已登录
func (this *UserController) CheckLogin() {
if this.BaseController.IsLogin > 0 {
this.ResponseJson(true, "已登录")
}
this.ResponseJson(false, "您当前处于未登录状态,请先登录")
}