|
@@ -0,0 +1,336 @@
|
|
1
|
+package role
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ base_ctl "SCRM/controllers"
|
|
5
|
+ "SCRM/enums"
|
|
6
|
+ "SCRM/models"
|
|
7
|
+ "SCRM/service"
|
|
8
|
+ base_service "SCRM/service"
|
|
9
|
+ "SCRM/service/role_service"
|
|
10
|
+ "time"
|
|
11
|
+
|
|
12
|
+ "github.com/astaxie/beego"
|
|
13
|
+)
|
|
14
|
+
|
|
15
|
+func AdminCtlRegistRouters() {
|
|
16
|
+ beego.Router("/api/adminmain", &AdminAPIController{}, "get:AdminMainView")
|
|
17
|
+ beego.Router("/api/admins", &AdminAPIController{}, "get:Admins")
|
|
18
|
+ beego.Router("/api/admin/addinit", &AdminAPIController{}, "get:AddAdminInitData")
|
|
19
|
+ beego.Router("/api/admin/add", &AdminAPIController{}, "post:AddAdmin")
|
|
20
|
+ beego.Router("/api/admin/editinit", &AdminAPIController{}, "get:EditAdminInitData")
|
|
21
|
+ beego.Router("/api/admin/edit", &AdminAPIController{}, "post:EditAdmin")
|
|
22
|
+ beego.Router("/api/admin/setstatus", &AdminAPIController{}, "post:AdminSetStatus")
|
|
23
|
+}
|
|
24
|
+
|
|
25
|
+type AdminAPIController struct {
|
|
26
|
+ base_ctl.BaseAuthAPIController
|
|
27
|
+}
|
|
28
|
+
|
|
29
|
+// /api/adminmain [get]
|
|
30
|
+func (this *AdminAPIController) AdminMainView() {
|
|
31
|
+ adminUserInfo := this.GetAdminUserInfo()
|
|
32
|
+ if adminUserInfo.AdminUser.IsSuperAdmin == false {
|
|
33
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
|
|
34
|
+ return
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 10)
|
|
38
|
+ if getAdminsErr != nil {
|
|
39
|
+ //beego.Error("获取管理员列表失败:", getAdminsErr)
|
|
40
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
41
|
+ return
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ existRoleCount, _ := role_service.GetValidRoleCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
|
|
45
|
+
|
|
46
|
+ this.ServeSuccessJSON(map[string]interface{}{
|
|
47
|
+ "admins": viewModels,
|
|
48
|
+ "total_count": total,
|
|
49
|
+ "is_exist_role": existRoleCount > 0,
|
|
50
|
+ })
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+// /api/admins [get]
|
|
54
|
+// @param page?:int
|
|
55
|
+func (this *AdminAPIController) Admins() {
|
|
56
|
+ adminUserInfo := this.GetAdminUserInfo()
|
|
57
|
+ if adminUserInfo.AdminUser.IsSuperAdmin == false {
|
|
58
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
|
|
59
|
+ return
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ page, _ := this.GetInt("page")
|
|
63
|
+ viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10)
|
|
64
|
+ if getAdminsErr != nil {
|
|
65
|
+ //beego.Error("获取管理员列表失败:", getAdminsErr)
|
|
66
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
67
|
+ } else {
|
|
68
|
+ this.ServeSuccessJSON(map[string]interface{}{
|
|
69
|
+ "admins": viewModels,
|
|
70
|
+ "total_count": total,
|
|
71
|
+ })
|
|
72
|
+ }
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+// /api/admin/addinit [get]
|
|
76
|
+func (this *AdminAPIController) AddAdminInitData() {
|
|
77
|
+ adminUserInfo := this.GetAdminUserInfo()
|
|
78
|
+ if adminUserInfo.AdminUser.IsSuperAdmin == false {
|
|
79
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
|
|
80
|
+ return
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ roles, getRoleErr := role_service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
|
|
84
|
+ if getRoleErr != nil {
|
|
85
|
+ //beego.Error("获取所有角色失败:", getRoleErr)
|
|
86
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
87
|
+ return
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ redisClient := service.RedisClient()
|
|
91
|
+ defer redisClient.Close()
|
|
92
|
+ qntoken, _ := redisClient.Get("qn_token").Result()
|
|
93
|
+
|
|
94
|
+ this.ServeSuccessJSON(map[string]interface{}{
|
|
95
|
+ "roles": roles,
|
|
96
|
+ "qntoken": qntoken,
|
|
97
|
+ })
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+// /api/admin/add [post]
|
|
101
|
+// @param mobile:string
|
|
102
|
+// @param name:string
|
|
103
|
+// @param type:int 管理员类型:2.医生 3.护士 4.运营
|
|
104
|
+// @param title:int 用户职称(1.医士;2.医师;3.住院医师;4.主治医师;5.副主任医师;6.主任医师;7.护士;8.护师;9.主管护师;10.副主任护师;11.主任护师;12.运营专员;13.运营主管)
|
|
105
|
+// @param role:int
|
|
106
|
+// @param intro?:string
|
|
107
|
+func (this *AdminAPIController) AddAdmin() {
|
|
108
|
+ adminUserInfo := this.GetAdminUserInfo()
|
|
109
|
+ if adminUserInfo.AdminUser.IsSuperAdmin == false {
|
|
110
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
|
|
111
|
+ return
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ mobile := this.GetString("mobile")
|
|
115
|
+ name := this.GetString("name")
|
|
116
|
+ userType, _ := this.GetInt("type")
|
|
117
|
+ userTitle, _ := this.GetInt("title")
|
|
118
|
+ roleId, _ := this.GetInt64("role")
|
|
119
|
+ intro := this.GetString("intro")
|
|
120
|
+
|
|
121
|
+ _, titleExist := models.UserTitle[userTitle]
|
|
122
|
+ if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
|
|
123
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
|
|
124
|
+ return
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ isRoleExist, getRoleErr := role_service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
|
|
128
|
+ if getRoleErr != nil {
|
|
129
|
+ //beego.Error("查询角色是否存在时失败:", getRoleErr)
|
|
130
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
131
|
+ return
|
|
132
|
+ }
|
|
133
|
+ if !isRoleExist {
|
|
134
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
|
|
135
|
+ return
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ // 判断该应用是否已存在该手机号
|
|
139
|
+ if isMobileDidUsed, err := role_service.IsMobileDidUsedAtApp(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile); err != nil {
|
|
140
|
+ //beego.Error("查询用户是否已被添加为管理员时失败:", err)
|
|
141
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
142
|
+ return
|
|
143
|
+ } else {
|
|
144
|
+ if isMobileDidUsed {
|
|
145
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileDidUsedInApp)
|
|
146
|
+ return
|
|
147
|
+ }
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ if isSuperAdmin, err := role_service.IsUserSuperAdminWithMobile(mobile); err != nil {
|
|
151
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileNotExit)
|
|
152
|
+ return
|
|
153
|
+ } else {
|
|
154
|
+ if isSuperAdmin {
|
|
155
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleMobileIsSuperAdmin)
|
|
156
|
+ return
|
|
157
|
+ }
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ _, password, createErr := role_service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, userType, userTitle, intro, roleId)
|
|
161
|
+ if createErr != nil {
|
|
162
|
+ //beego.Error("创建管理员失败:", createErr)
|
|
163
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
|
|
164
|
+ return
|
|
165
|
+
|
|
166
|
+ } else {
|
|
167
|
+ this.TraceLog("%v", password)
|
|
168
|
+ //beego.Trace("用户密码:", password)
|
|
169
|
+ // 发送短信通知这个手机号
|
|
170
|
+ // sendSMSErr := role_service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
|
|
171
|
+ // if sendSMSErr != nil {
|
|
172
|
+ // //beego.Error("发送邀请短信失败:%v", sendSMSErr)
|
|
173
|
+ // }
|
|
174
|
+
|
|
175
|
+ this.ServeSuccessJSON(nil)
|
|
176
|
+ return
|
|
177
|
+ }
|
|
178
|
+}
|
|
179
|
+
|
|
180
|
+// /api/admin/editinit [get]
|
|
181
|
+// @param uid:int
|
|
182
|
+func (this *AdminAPIController) EditAdminInitData() {
|
|
183
|
+ adminUserInfo := this.GetAdminUserInfo()
|
|
184
|
+ if adminUserInfo.AdminUser.IsSuperAdmin == false {
|
|
185
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
|
|
186
|
+ return
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ admin_user_id, _ := this.GetInt64("uid")
|
|
190
|
+ if admin_user_id <= 0 {
|
|
191
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
|
|
192
|
+ return
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ adminUserViewModel, getInfoErr := role_service.GetGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id)
|
|
196
|
+ if getInfoErr != nil {
|
|
197
|
+ //beego.Error("获取管理员信息失败:", getInfoErr)
|
|
198
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
199
|
+ return
|
|
200
|
+ }
|
|
201
|
+ if adminUserViewModel == nil {
|
|
202
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
|
|
203
|
+ return
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ roles, getRoleErr := role_service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
|
|
207
|
+ if getRoleErr != nil {
|
|
208
|
+ //beego.Error("获取所有角色失败:", getRoleErr)
|
|
209
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
210
|
+ return
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ redisClient := base_service.RedisClient()
|
|
214
|
+ defer redisClient.Close()
|
|
215
|
+ qntoken, _ := redisClient.Get("qn_token").Result()
|
|
216
|
+
|
|
217
|
+ this.ServeSuccessJSON(map[string]interface{}{
|
|
218
|
+ "admin": adminUserViewModel,
|
|
219
|
+ "roles": roles,
|
|
220
|
+ "qntoken": qntoken,
|
|
221
|
+ })
|
|
222
|
+}
|
|
223
|
+
|
|
224
|
+// /api/admin/edit [post]
|
|
225
|
+// @param uid:int
|
|
226
|
+// @param name:string
|
|
227
|
+// @param type:int
|
|
228
|
+// @param title:int
|
|
229
|
+// @param role:int
|
|
230
|
+// @param intro?:string
|
|
231
|
+func (this *AdminAPIController) EditAdmin() {
|
|
232
|
+ adminUserInfo := this.GetAdminUserInfo()
|
|
233
|
+ if adminUserInfo.AdminUser.IsSuperAdmin == false {
|
|
234
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
|
|
235
|
+ return
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ adminUserId, _ := this.GetInt64("uid")
|
|
239
|
+ name := this.GetString("name")
|
|
240
|
+ userType, _ := this.GetInt("type")
|
|
241
|
+ userTitle, _ := this.GetInt("title")
|
|
242
|
+ roleId, _ := this.GetInt64("role")
|
|
243
|
+ intro := this.GetString("intro")
|
|
244
|
+
|
|
245
|
+ _, titleExist := models.UserTitle[userTitle]
|
|
246
|
+ if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
|
|
247
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
|
|
248
|
+ return
|
|
249
|
+ }
|
|
250
|
+
|
|
251
|
+ appRole, getAppRoleErr := role_service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId)
|
|
252
|
+ if getAppRoleErr != nil {
|
|
253
|
+ //beego.Error("查询管理员信息时失败:", getAppRoleErr)
|
|
254
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
255
|
+ return
|
|
256
|
+ }
|
|
257
|
+ if appRole == nil {
|
|
258
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
|
|
259
|
+ return
|
|
260
|
+ }
|
|
261
|
+
|
|
262
|
+ isRoleExist, getRoleErr := role_service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
|
|
263
|
+ if getRoleErr != nil {
|
|
264
|
+ //beego.Error("查询角色是否存在时失败:", getRoleErr)
|
|
265
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
266
|
+ return
|
|
267
|
+ }
|
|
268
|
+ if !isRoleExist {
|
|
269
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
|
|
270
|
+ return
|
|
271
|
+ }
|
|
272
|
+
|
|
273
|
+ appRole.UserName = name
|
|
274
|
+ appRole.UserType = int8(userType)
|
|
275
|
+ appRole.UserTitle = int8(userTitle)
|
|
276
|
+ appRole.RoleId = roleId
|
|
277
|
+ appRole.Intro = intro
|
|
278
|
+ appRole.ModifyTime = time.Now().Unix()
|
|
279
|
+ saveErr := role_service.SaveAppRole(appRole)
|
|
280
|
+ if saveErr != nil {
|
|
281
|
+ //beego.Error("修改App_Role失败:", saveErr)
|
|
282
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
|
|
283
|
+
|
|
284
|
+ } else {
|
|
285
|
+ this.ServeSuccessJSON(nil)
|
|
286
|
+ }
|
|
287
|
+}
|
|
288
|
+
|
|
289
|
+// /api/admin/setstatus [post]
|
|
290
|
+// @param uid:int
|
|
291
|
+// @param enable:bool
|
|
292
|
+func (this *AdminAPIController) AdminSetStatus() {
|
|
293
|
+ adminUserInfo := this.GetAdminUserInfo()
|
|
294
|
+ if adminUserInfo.AdminUser.IsSuperAdmin == false {
|
|
295
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
|
|
296
|
+ return
|
|
297
|
+ }
|
|
298
|
+
|
|
299
|
+ userID, _ := this.GetInt64("uid")
|
|
300
|
+ if userID <= 0 {
|
|
301
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
|
|
302
|
+ return
|
|
303
|
+ }
|
|
304
|
+ appRole, getAppRoleErr := role_service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID)
|
|
305
|
+ if getAppRoleErr != nil {
|
|
306
|
+ //beego.Error("查询管理员信息失败:", getAppRoleErr)
|
|
307
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
|
|
308
|
+ return
|
|
309
|
+ } else if appRole == nil {
|
|
310
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
|
|
311
|
+ return
|
|
312
|
+ }
|
|
313
|
+
|
|
314
|
+ enable, _ := this.GetBool("enable")
|
|
315
|
+ if enable == true {
|
|
316
|
+ if roleEnable, _ := role_service.IsRoleExist(appRole.OrgId, appRole.AppId, appRole.RoleId); roleEnable == false {
|
|
317
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
|
|
318
|
+ return
|
|
319
|
+ }
|
|
320
|
+ }
|
|
321
|
+
|
|
322
|
+ if enable {
|
|
323
|
+ appRole.Status = 1
|
|
324
|
+ } else {
|
|
325
|
+ appRole.Status = 0
|
|
326
|
+ }
|
|
327
|
+ appRole.ModifyTime = time.Now().Unix()
|
|
328
|
+ saveErr := role_service.SaveAppRole(appRole)
|
|
329
|
+ if saveErr != nil {
|
|
330
|
+ //beego.Error("保存AppRole失败:", saveErr)
|
|
331
|
+ this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
|
|
332
|
+
|
|
333
|
+ } else {
|
|
334
|
+ this.ServeSuccessJSON(nil)
|
|
335
|
+ }
|
|
336
|
+}
|