admin_user_controller.go
6.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package controllers
import (
"errors"
"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"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
type AdminUserController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *AdminUserController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMINSSION_ADMIN_USER); !ok {
return
}
}
type adminDetailParam struct {
Id int64 `json:"id"`
PermissionType []int `json:"permissionType"`
Status int `json:"status"`
Account string `json:"account"`
IsAdmin int `json:"isAdmin"`
}
func (c *AdminUserController) SaveAdminUser() {
//用与适配前端定义的数据结构
var (
param adminDetailParam
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
cmd := &adminuserCmd.SaveAdminUserCommand{
Id: param.Id,
Name: param.Account,
Account: param.Account,
PermissionId: param.PermissionType,
IsUsable: false,
}
if param.Status == 1 {
cmd.IsUsable = true
}
_, err = newAdminUserService.SaveAdminUser(cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
func (c *AdminUserController) GetAdminUser() {
//用与适配前端定义的数据结构
type Paramter struct {
Id int64 `json:"id"`
}
var (
param Paramter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
adminuser, err := newAdminUserService.GetAdminUser(&adminuserquery.GetAdminUserQuery{
Id: param.Id,
})
if err != nil {
c.ResponseError(err)
return
}
rspData := adminDetailParam{
Id: adminuser.Id,
Account: adminuser.Account,
Status: 0,
IsAdmin: 0,
}
for _, v := range adminuser.Permission {
rspData.PermissionType = append(rspData.PermissionType, v.Id)
}
if adminuser.IsUsable {
rspData.Status = 1
}
if adminuser.IsDefault {
rspData.IsAdmin = 1
}
c.ResponseData(rspData)
return
}
func (c *AdminUserController) ListAdminUser() {
//用与适配前端定义的数据结构
type Paramter struct {
SearchText string `json:"searchText"`
PageSize int `json::"pageSize"`
PageNumber int `json:"pageNumber"`
}
var (
param Paramter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.PageSize == 0 {
param.PageSize = 20
}
if param.PageNumber == 0 {
param.PageNumber = 1
}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
queryOption := &adminuserquery.ListAdminUserQuery{
AdminAccountMatch: param.SearchText,
Limit: param.PageSize,
Offset: param.PageSize * (param.PageNumber - 1),
}
adminusers, cnt, err := newAdminUserService.PageListAdminUser(queryOption)
if err != nil {
c.ResponseError(err)
return
}
newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{
ParentId: 0,
})
if err != nil {
logs.Error("获取权限数据失败:%s", err)
c.ResponseError(errors.New("服务异常"))
return
}
permissionMap := map[int]domain.AdminPermission{}
for i := range allPermission {
permissionMap[allPermission[i].Id] = allPermission[i]
}
listData := []map[string]interface{}{}
//前端数据格式适配
for i := range adminusers {
permissionTypes := []string{}
for _, vv := range adminusers[i].Permission {
if pm, ok := permissionMap[vv.Id]; ok {
permissionTypes = append(permissionTypes, pm.Name)
}
}
m := map[string]interface{}{
"id": adminusers[i].Id,
"account": adminusers[i].Account,
"permission": permissionTypes,
"statue": 0,
"isAdmin": 0,
}
if adminusers[i].IsUsable {
m["statue"] = 1
}
if adminusers[i].IsDefault {
m["statue"] = 1
}
listData = append(listData, m)
}
c.ResponsePageList(listData, cnt, param.PageNumber)
return
}
func (c *AdminUserController) ForbiddenAdminUser() {
//用与适配前端定义的数据结构
type Paramter struct {
Id int64 `json:"id"`
Statue int `json:"statue"`
}
var (
param Paramter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
var isUsable bool
if param.Statue == 1 {
isUsable = true
} else if param.Statue == 0 {
isUsable = false
} else {
c.ResponseError(errors.New("参数错误"))
return
}
err = newAdminUserService.UpdateAdminIsUsable(param.Id, isUsable)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//BeforeEditAdminUser 编辑管理员操作的前置接口
func (c *AdminUserController) BeforeEditAdminUser() {
newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{
ParentId: 0,
NotCode: []string{domain.PERMINSSION_ADMIN_USER},
})
if err != nil {
logs.Error("获取权限数据失败:%s", err)
c.ResponseError(errors.New("服务异常"))
return
}
var rspData []map[string]interface{}
for i := range allPermission {
m := map[string]interface{}{
"id": allPermission[i].Id,
"permissionName": allPermission[i].Name,
}
rspData = append(rspData, m)
}
c.ResponseData(rspData)
return
}