forked from kongyuebin1/dongfeng-pay
/
login.go
160 lines (136 loc) · 3.55 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/***************************************************
** @Desc : This file for 用户登录
** @Time : 19.11.29 13:52
** @Author : Joker
** @File : login
** @Last Modified by : Joker
** @Last Modified time: 19.11.29 13:52
** @Software: GoLand
****************************************************/
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"github.com/dchest/captcha"
"dongfeng-pay/jhagent/sys"
"dongfeng-pay/jhagent/sys/enum"
"dongfeng-pay/jhagent/utils"
"dongfeng-pay/service/models"
"strconv"
"strings"
)
var pubMethod = sys.PublicMethod{}
var encrypt = utils.Encrypt{}
type Login struct {
beego.Controller
}
// @router /login.py/?:params [post]
func (c *Login) UserLogin() {
captchaCode := c.GetString("captchaCode")
captchaId := c.GetString("captchaId")
userName := strings.TrimSpace(c.GetString("userName"))
password := c.GetString("Password")
var (
flag = enum.FailedFlag
msg = ""
url = "/"
pwdMd5 string
ran string
ranMd5 string
verify bool
u models.AgentInfo
)
us := c.GetSession(enum.UserSession)
if us != nil {
url = enum.DoMainUrl
flag = enum.SuccessFlag
goto stopRun
}
if userName == "" || password == "" {
msg = "登录账号或密码不能为空!"
goto stopRun
}
verify = captcha.VerifyString(captchaId, captchaCode)
if !verify {
url = strconv.Itoa(enum.FailedFlag)
msg = "验证码不正确!"
goto stopRun
}
u = models.GetAgentInfoByPhone(userName)
if u.AgentPassword == "" {
msg = "账户信息错误,请联系管理人员!"
goto stopRun
}
if strings.Compare(enum.ACTIVE, u.Status) != 0 {
msg = "登录账号或密码错误!"
goto stopRun
}
//验证密码
pwdMd5 = encrypt.EncodeMd5([]byte(password))
if strings.Compare(strings.ToUpper(pwdMd5), u.AgentPassword) != 0 {
msg = "登录账号或密码错误!"
goto stopRun
}
c.SetSession(enum.UserSession, u)
// 设置客户端用户信息有效保存时间
ran = pubMethod.RandomString(46)
ranMd5 = encrypt.EncodeMd5([]byte(ran))
c.Ctx.SetSecureCookie(ranMd5, enum.UserCookie, ranMd5, enum.CookieExpireTime)
c.SetSession(enum.UserCookie, ranMd5)
url = enum.DoMainUrl
flag = enum.SuccessFlag
utils.LogNotice(fmt.Sprintf("【%s】代理商登录成功,请求IP:%s", u.AgentName, c.Ctx.Input.IP()))
stopRun:
c.Data["json"] = pubMethod.JsonFormat(flag, "", msg, url)
c.ServeJSON()
c.StopRun()
}
// @router /
func (c *Login) Index() {
capt := struct {
CaptchaId string
}{
captcha.NewLen(4),
}
c.Data["CaptchaId"] = capt.CaptchaId
c.TplName = "login.html"
}
// 验证输入的验证码
// @router /verifyCaptcha.py/:value/:chaId [get]
func (c *Login) VerifyCaptcha() {
captchaValue := c.Ctx.Input.Param(":value")
captchaId := c.Ctx.Input.Param(":chaId")
verify := captcha.VerifyString(captchaId, captchaValue)
if verify {
c.Data["json"] = pubMethod.JsonFormat(enum.SuccessFlag, "", "", "")
} else {
c.Data["json"] = pubMethod.JsonFormat(enum.FailedFlag, "", "验证码不匹配!", "")
}
c.ServeJSON()
c.StopRun()
}
// 重绘验证码
// @router /flushCaptcha.py/ [get]
func (c *Login) FlushCaptcha() {
capt := struct {
CaptchaId string
}{
captcha.NewLen(4),
}
c.Data["json"] = pubMethod.JsonFormat(enum.SuccessFlag, capt.CaptchaId, "验证码不匹配!", "")
c.ServeJSON()
c.StopRun()
}
// 退出登录
//@router /loginOut.py
func (c *Login) LoginOut() {
c.DelSession(enum.UserSession)
c.Data["json"] = pubMethod.JsonFormat(enum.FailedFlag, "", "", "/")
c.ServeJSON()
c.StopRun()
}
// 对接文档
//@router /pay_doc.py
func (c *Login) PayDoc() {
c.TplName = "api_doc/pay_doc.html"
}