auth.go
4.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package controllers
import (
"encoding/json"
"fmt"
"oppmg/common/log"
"oppmg/protocol"
serveauth "oppmg/services/auth"
"oppmg/storage/redisdata"
"strconv"
"time"
"github.com/GeeTeam/gt3-golang-sdk/geetest"
)
type AuthController struct {
BaseController
}
//URLMapping 实现ControllerInterface中的URLMapping
func (c *AuthController) URLMapping() {
c.Mapping("RefreshToken", c.RefreshToken)
}
// RefreshToken ....
// @router /refresh_token [get]
func (c *AuthController) RefreshToken() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
companyid := c.GetCompanyId()
userid := c.GetUserId()
logintoken, err := serveauth.ChangeLoginToken(userid, companyid)
if err != nil {
msg = protocol.NewReturnResponse(nil, err)
return
}
err = serveauth.ResetLoginTokenRedis(logintoken)
msg = protocol.NewReturnResponse(logintoken, err)
return
}
// Login 登录
// @router /login [post]
func (c *AuthController) Login() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestLogin
if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
if len(param.Account) == 0 || len(param.Password) == 0 {
msg = protocol.BadRequestParam("10021")
return
}
logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)
if err != nil {
msg = protocol.NewReturnResponse(nil, err)
return
}
err = serveauth.ResetLoginToken(logintoken)
if err != nil {
log.Error("token 信息记录数据库失败")
}
err = serveauth.ResetLoginTokenRedis(logintoken)
if err != nil {
log.Error("token 信息记录redis失败")
}
data := map[string]interface{}{
"access": logintoken,
}
msg = protocol.NewReturnResponse(data, nil)
return
}
//ChangeCompany 切换公司
//@Router /change_company [post]
func (c *AuthController) ChangeCompany() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestSwapCompany
if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
userid := c.GetUserId()
if param.CompanyId <= 0 {
msg = protocol.BadRequestParam("1")
return
}
logintoken, err := serveauth.ChangeLoginToken(userid, param.CompanyId)
if err != nil {
msg = protocol.NewReturnResponse(nil, err)
return
}
err = redisdata.SetLoginToken(logintoken, userid, param.CompanyId)
if err != nil {
log.Error("redisdata.SetLoginToken err:%s", err)
}
msg = protocol.NewReturnResponse(logintoken, err)
return
}
//Me
//@router /me [get]
func (c *AuthController) Me() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
userid := c.GetUserId()
companyid := c.GetCompanyId()
userinfo, err := serveauth.UserBaseInfo(userid, companyid)
if err != nil {
log.Error("获取用户数据失败")
}
menus, err := serveauth.GetUserHasMenu(userid, companyid)
if err != nil {
log.Error("获取用户菜单")
}
companys, err := serveauth.UserHasCompanys(userid)
if err != nil {
log.Error("获取用户的公司")
}
data := map[string]interface{}{
"user": userinfo,
"menus": menus,
"companys": companys,
}
msg = protocol.NewReturnResponse(data, nil)
return
}
const (
captchaID = "48a6ebac4ebc6642d68c217fca33eb4d"
privateKey = "4f1c085290bec5afdc54df73535fc361"
)
func (c *AuthController) RegisterGeetest() {
geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
status, responseBt := geetest.PreProcess("", "")
c.SetSession("geetest_status", status)
c.Ctx.Output.Body(responseBt)
return
}
func (c *AuthController) ValidateGeetest() {
type Parameter struct {
GeetestChallenge string `form:"geetest_challenge"`
GeetestValidate string `form:"geetest_validate"`
GeetestSeccode string `form:"geetest_seccode"`
}
var (
param Parameter
geetestRes bool
status int
)
err := c.ParseForm(¶m)
if err != nil {
log.Error("解析表单数据失败;%s", err)
}
val := c.GetSession("geetest_status")
status, _ = strconv.Atoi(fmt.Sprint(val))
geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
if status == 1 {
geetestRes = geetest.SuccessValidate(param.GeetestChallenge, param.GeetestValidate, param.GeetestSeccode, "", "")
} else {
geetestRes = geetest.FailbackValidate(param.GeetestChallenge, param.GeetestValidate, param.GeetestSeccode)
}
res := make(map[string]interface{})
if geetestRes {
res["code"] = 0
res["msg"] = "Success"
} else {
res["code"] = -100
res["msg"] = "Failed"
}
responseBt, _ := json.Marshal(res)
c.Ctx.Output.Body(responseBt)
return
}