-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathuser.go
139 lines (119 loc) · 3.45 KB
/
user.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2019 aimerforreimu. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
// Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
// Everyone is permitted to copy and distribute verbatim copies
// of this license document, but changing it is not allowed.
//
// repo: https://github.com/aimerforreimu/auxpi
package controllers
import (
"strconv"
"github.com/auxpi/auxpiAll"
"github.com/auxpi/auxpiAll/e"
"github.com/auxpi/bootstrap"
"github.com/auxpi/log"
"github.com/auxpi/models"
"github.com/auxpi/utils"
"github.com/astaxie/beego"
"github.com/astaxie/beego/validation"
)
type UsersController struct {
beego.Controller
}
func (u *UsersController) commonStyle() {
u.LayoutSections = make(map[string]string)
u.LayoutSections["Script"] = "user/user_script.tpl"
u.LayoutSections["Header"] = "user/user_header.tpl"
u.LayoutSections["SiderBar"] = "user/user_sider_bar.tpl"
u.LayoutSections["Content"] = "user/content/images.tpl"
u.Data["xsrf_token"] = u.XSRFToken()
r, _ := u.Ctx.GetSecureCookie(bootstrap.SiteConfig.AuxpiSalt, "r")
if r == "admin" {
u.Data["IsAdmin"] = true
}
}
func (u *UsersController) Show() {
u.commonStyle()
uname, _ := u.Ctx.GetSecureCookie(bootstrap.SiteConfig.AuxpiSalt, "uname")
page, size := utils.GetStringPage(u.Input().Get("page"), u.Input().Get("limit"))
user, images, count := models.GetUserImagesByUserName(uname, size, page)
tPage, _ := strconv.Atoi(u.Input().Get("page"))
tplPage := utils.PageHtml(tPage, count, size)
u.Data["User"] = &user
u.Data["Images"] = &images
u.Data["Page"] = tplPage
u.Layout = "user/user_app.tpl"
u.TplName = "user/user_app.tpl"
}
func (u *UsersController) Edit() {
u.commonStyle()
u.LayoutSections["Content"] = "user/content/edit.tpl"
uname, _ := u.Ctx.GetSecureCookie(bootstrap.SiteConfig.AuxpiSalt, "uname")
user := models.GetUserInfo(uname)
u.Data["User"] = user
u.Layout = "user/user_app.tpl"
u.TplName = "user/user_app.tpl"
}
type UserResetPass struct {
OldPass string `form:"old_password" valid:"Required;MinSize(6);MaxSize(32)"`
NewPass string `form:"new_password" valid:"Required;MinSize(6);MaxSize(32)"`
RePass string `form:"re_password" valid:"Required;MinSize(6);MaxSize(32)"`
}
//重置密码
func (u *UsersController) ResetPass() {
uid, _ := strconv.Atoi(u.Ctx.Input.Param(":id"))
passInfo := &UserResetPass{}
if err := u.ParseForm(passInfo); err != nil {
auxpiLog.SetAWarningLog("USER_LOGIN", err)
u.ajaxErrorResp()
return
}
valid := validation.Validation{}
ok, _ := valid.Valid(passInfo)
if !ok {
u.ajaxErrorResp()
return
}
if passInfo.RePass != passInfo.NewPass {
u.Data["json"] = &auxpi.RespJson{
Code: 200,
Msg: "两次密码不一致",
}
u.ServeJSON()
return
}
ok = models.ResetUserPassWithOld(uid,
utils.GetSha256CodeWithSalt(passInfo.OldPass),
utils.GetSha256CodeWithSalt(passInfo.NewPass))
if !ok {
u.Data["json"] = &auxpi.RespJson{
Code: 500,
Msg: e.GetMsg(500),
}
u.ServeJSON()
return
}
u.Data["json"] = &auxpi.RespJson{
Code: 200,
Msg: "修改成功",
}
u.ServeJSON()
return
}
//ajax 错误相应封装
func (u *UsersController) ajaxErrorResp() bool {
if u.IsAjax() {
u.Data["json"] = &auxpi.RespJson{
Code: e.INVALID_PARAMS,
Msg: e.GetMsg(e.INVALID_PARAMS),
}
u.ServeJSON()
return false
}
return true
}