admin_login_controller.go
5.5 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
193
194
195
196
197
198
199
200
package controllers
import (
"encoding/json"
"errors"
"fmt"
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
"github.com/GeeTeam/gt3-golang-sdk/geetest"
"github.com/astaxie/beego/logs"
adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service"
adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
)
type AdminLoginController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *AdminLoginController) Prepare() {
c.BaseController.Prepare()
//排除登录 以及获取图形验证
urlIn := map[string]int{
"/v1/auth/captcha-init": 1,
"/v1/auth/login": 1,
}
nowUrl := c.Ctx.Input.URL()
if _, ok := urlIn[nowUrl]; ok {
//跳过校验
return
}
if ok := c.ValidJWTToken(); !ok {
return
}
}
//Login 用户登录
func (c *AdminLoginController) Login() {
type Paramter struct {
Username string `json:"username"`
Password string `json:"password"`
}
var (
param Paramter
err error
)
if err = c.BindJsonData(¶m); err != nil {
c.ResponseError(fmt.Errorf("json解析失败:%s", err))
return
}
newAdminuserquery := adminuserquery.GetAdminUserQuery{AdminAccount: param.Username}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
if err != nil {
logs.Error("获取用户数据失败:%s", err)
c.ResponseError(errors.New("用户不存在"))
return
}
if adminuser.Password != param.Password {
c.ResponseError(errors.New("账号或密码错误"))
return
}
if !adminuser.IsUsable {
c.ResponseError(errors.New("用户被禁用"))
}
newJwt := lib.NewMyToken(adminuser.Id)
newToken, err := newJwt.CreateJWTToken()
if err != nil {
logs.Error("生成jwt数据失败:%s", err)
c.ResponseError(errors.New("服务异常"))
return
}
rspdata := map[string]interface{}{
"access": map[string]interface{}{
"accessToken": newToken,
"expiresIn": lib.JWtExpiresSecond,
},
}
c.ResponseData(rspdata)
return
}
//CaptchaInit 极验初始化
func (c *AdminLoginController) CaptchaInit() {
const (
captchaID = "33a2abf9c5df0d6bc3b89fb39280114b"
privateKey = "13320fd2b10199e9a2440a4fbb4d46f7"
)
newGeetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
userip := c.Ctx.Input.IP()
_, responseBt := newGeetest.PreProcess("", userip)
// c.SetSession("geetest_status", status)
var geetestRsp geetest.FailbackRegisterRespnse
json.Unmarshal(responseBt, &geetestRsp)
//对前端定义的数据格式进行适配。。。
rspData := map[string]interface{}{
"success": geetestRsp.Success,
"gt": geetestRsp.GT,
"challenge": geetestRsp.Challenge,
"newCaptcha": geetestRsp.NewCaptcha,
}
c.ResponseData(rspData)
return
}
//AdminpPofile 获取登录用户的权限配置
func (c *AdminLoginController) AdminpPofile() {
adminId := c.GetUserId()
newAdminuserquery := adminuserquery.GetAdminUserQuery{Id: adminId}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
if err != nil {
logs.Error("获取用户数据失败:%s", err)
c.ResponseError(errors.New("用户不存在"))
return
}
newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{})
if err != nil {
logs.Error("获取权限数据失败:%s", err)
c.ResponseError(errors.New("服务异常"))
return
}
//适配前端的数据格式
permissionSlice := make([]map[string]interface{}, 0)
for _, v := range allPermission {
m := map[string]interface{}{
"code": v.Code,
"icon": v.Icon,
"parentId": v.ParentId,
"sort": v.Sort,
"name": v.Name,
"id": v.Id,
"status": 0, //状态 1-启用 0-禁用,前端需要
}
permissionSlice = append(permissionSlice, m)
}
for index := range permissionSlice {
if adminuser.IsDefault {
permissionSlice[index]["status"] = 1
continue
}
for _, p := range adminuser.Permission {
if p == permissionSlice[index]["id"] {
permissionSlice[index]["status"] = 1
}
if p == permissionSlice[index]["parentId"] {
permissionSlice[index]["status"] = 1
}
}
}
userData := map[string]string{
"id": fmt.Sprint(adminuser.Id),
"name": adminuser.AdminName,
"adminType": "2", // 管理员类型 1-超级管理员 2-子管理员
}
if adminuser.IsDefault {
userData["adminType"] = "1"
}
respData := map[string]interface{}{
"user": userData,
"menus": permissionSlice,
}
c.ResponseData(respData)
}
func (c *AdminLoginController) PwdUpdate() {
//用与适配前端定义的数据结构
type Paramater struct {
Pwd string `json:"pwd"`
}
var (
param Paramater
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
uid := c.GetUserId()
newAdminUserService := adminuserservice.NewAdminUserService(nil)
cmd := adminuserCmd.UpdateAdminUserPwdCommand{
Id: uid,
Password: param.Pwd,
}
err = newAdminUserService.UpdateAdminPassword(cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}