role_controller.go 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. package controllers
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "XT_New/enums"
  7. "XT_New/models"
  8. "XT_New/service"
  9. "github.com/astaxie/beego"
  10. )
  11. func RoleAPIControllerRegistRouters() {
  12. beego.Router("/api/roles", &RoleAPIController{}, "get:GetRoles")
  13. beego.Router("/api/role/create", &RoleAPIController{}, "post:CreateRole")
  14. beego.Router("/api/role/modify", &RoleAPIController{}, "post:ModifyRole")
  15. beego.Router("/api/role/setstatus", &RoleAPIController{}, "post:ModifyRoleStatus")
  16. beego.Router("/role/purview/editinit", &RoleAPIController{}, "get:EditPurviewInitData")
  17. beego.Router("/role/purview/edit", &RoleAPIController{}, "post:EditPurview")
  18. beego.Router("/api/adminmain", &RoleAPIController{}, "get:AdminMainView")
  19. beego.Router("/api/admins", &RoleAPIController{}, "get:Admins")
  20. beego.Router("/api/admin/addinit", &RoleAPIController{}, "get:AddAdminInitData")
  21. beego.Router("/api/admin/add", &RoleAPIController{}, "post:AddAdmin")
  22. beego.Router("/api/admin/editinit", &RoleAPIController{}, "get:EditAdminInitData")
  23. beego.Router("/api/admin/edit", &RoleAPIController{}, "post:EditAdmin")
  24. beego.Router("/api/admin/setstatus", &RoleAPIController{}, "post:AdminSetStatus")
  25. beego.Router("/api/admin/specialpermission/initdata", &RoleAPIController{}, "get:SpecialPermissionInitData")
  26. beego.Router("/api/admin/specialpermission/dialysisrecord/submit", &RoleAPIController{}, "post:SubmitDialysisRecordPermission")
  27. beego.Router("/api/roles/list", &RoleAPIController{}, "get:GetAllOrgRole")
  28. beego.Router("/api/staff", &RoleAPIController{}, "get:GetAllOrgUser")
  29. beego.Router("/api/role/addStaff", &RoleAPIController{}, "post:AddRoleStaff")
  30. beego.Router("/api/role/staff", &RoleAPIController{}, "get:GetRoleStaff")
  31. }
  32. type RoleAPIController struct {
  33. BaseAuthAPIController
  34. }
  35. // /api/roles [get]
  36. // @param page?:int
  37. func (this *RoleAPIController) GetRoles() {
  38. page, _ := this.GetInt("page")
  39. adminUserInfo := this.GetAdminUserInfo()
  40. //beego.Alert(adminUserInfo.AdminUser)
  41. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  42. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  43. // return
  44. //}
  45. if page <= 0 {
  46. page = 1
  47. }
  48. roles, total, getRoleErr := service.GetRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 100)
  49. if getRoleErr != nil {
  50. //beego.Error("获取角色列表失败:", getRoleErr)
  51. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  52. } else {
  53. this.ServeSuccessJSON(map[string]interface{}{
  54. "roles": roles,
  55. "total_count": total,
  56. })
  57. }
  58. }
  59. // /api/role/create [post]
  60. // @param name:string
  61. // @param intro:string
  62. func (this *RoleAPIController) CreateRole() {
  63. name := this.GetString("name")
  64. intro := this.GetString("intro")
  65. if len(name) == 0 || len(intro) == 0 {
  66. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  67. return
  68. }
  69. adminUserInfo := this.GetAdminUserInfo()
  70. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  71. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  72. // return
  73. //}
  74. total := service.FindRoleRecordByRoleName(name, adminUserInfo.CurrentOrgId)
  75. if total > 0 {
  76. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNameIsExist)
  77. return
  78. }
  79. role, createErr := service.CreateRole(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, intro)
  80. if createErr != nil {
  81. //beego.Error("创建角色失败:", createErr)
  82. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  83. } else {
  84. this.ServeSuccessJSON(map[string]interface{}{
  85. "id": role.Id,
  86. "name": role.RoleName,
  87. "intro": role.RoleIntro,
  88. "status": role.Status,
  89. })
  90. }
  91. }
  92. // /api/role/modify
  93. // @param role_id:int
  94. // @param name:string
  95. // @param intro:string
  96. func (this *RoleAPIController) ModifyRole() {
  97. roleID, _ := this.GetInt64("role_id")
  98. name := this.GetString("name")
  99. intro := this.GetString("intro")
  100. if roleID <= 0 || len(name) == 0 || len(intro) == 0 {
  101. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  102. return
  103. }
  104. //adminUserInfo := this.GetAdminUserInfo()
  105. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  106. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  107. // return
  108. //}
  109. role, getRoleErr := service.GetRoleByRoleID(roleID)
  110. if getRoleErr != nil {
  111. //beego.Error("获取角色失败:", getRoleErr)
  112. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  113. return
  114. } else if role == nil {
  115. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  116. return
  117. }
  118. role.RoleName = name
  119. role.RoleIntro = intro
  120. role.ModifyTime = time.Now().Unix()
  121. saveErr := service.ModifyRole(role)
  122. if saveErr != nil {
  123. //beego.Error("修改角色失败:", role.Id, saveErr)
  124. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  125. } else {
  126. this.ServeSuccessJSON(nil)
  127. }
  128. }
  129. // /api/role/setstatus
  130. // @param role_id:int
  131. // @param enable:bool
  132. func (this *RoleAPIController) ModifyRoleStatus() {
  133. roleID, _ := this.GetInt64("role_id")
  134. enable, _ := this.GetBool("enable")
  135. if roleID <= 0 {
  136. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  137. return
  138. }
  139. adminUserInfo := this.GetAdminUserInfo()
  140. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  141. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  142. // return
  143. //}
  144. role, getRoleErr := service.GetRoleByRoleID(roleID)
  145. if getRoleErr != nil {
  146. //beego.Error("获取角色失败:", getRoleErr)
  147. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  148. return
  149. } else if role == nil {
  150. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  151. return
  152. }
  153. if enable == false {
  154. if count, _ := service.RoleAdminUserCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleID); count != 0 {
  155. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCannotRemoveRole)
  156. return
  157. }
  158. }
  159. if enable {
  160. role.Status = 1
  161. } else {
  162. role.Status = 2
  163. }
  164. role.ModifyTime = time.Now().Unix()
  165. saveErr := service.ModifyRole(role)
  166. if saveErr != nil {
  167. //beego.Error("修改角色失败:", role.Id, saveErr)
  168. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  169. } else {
  170. this.ServeSuccessJSON(nil)
  171. }
  172. }
  173. // /role/purview/editinit [get]
  174. // @param role_id:int
  175. func (this *RoleAPIController) EditPurviewInitData() {
  176. //adminUserInfo := this.GetAdminUserInfo()
  177. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  178. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  179. // return
  180. //}
  181. roleId, _ := this.GetInt64("role_id")
  182. if roleId <= 0 {
  183. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  184. return
  185. }
  186. purviews, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed()
  187. if getPurviewsErr != nil {
  188. //beego.Error("获取所有权限时出错:", getPurviewsErr)
  189. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  190. return
  191. }
  192. rolePurviewIdStr, getRPIdsErr := service.GetRolePurviewIds(roleId)
  193. if getRPIdsErr != nil {
  194. //beego.Error("获取角色的权限时出错:", getRPIdsErr)
  195. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  196. return
  197. }
  198. this.ServeSuccessJSON(map[string]interface{}{
  199. "purviews": purviews,
  200. "role_purview_ids": rolePurviewIdStr,
  201. })
  202. }
  203. // /role/purview/edit [post]
  204. // @param role_id:int
  205. // @param purview_ids:string
  206. func (this *RoleAPIController) EditPurview() {
  207. adminUserInfo := this.GetAdminUserInfo()
  208. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  209. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  210. // return
  211. //}
  212. roleId, _ := this.GetInt64("role_id")
  213. purviewIds := this.GetString("purview_ids")
  214. if roleId <= 0 {
  215. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  216. return
  217. }
  218. err := service.SaveRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, purviewIds)
  219. if err != nil {
  220. //beego.Error("设置角色的权限时出错:", err)
  221. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  222. } else {
  223. this.ServeSuccessJSON(nil)
  224. }
  225. }
  226. // func (this *RoleAPIController) doesUserHaveAccess(userID int64) bool {
  227. // adminUser, getAdminUserErr := service.GetAdminUserByUserID(userID)
  228. // if getAdminUserErr != nil {
  229. // beego.Error("获取用户信息失败:%v", getAdminUserErr)
  230. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  231. // return false
  232. // } else if adminUser == nil {
  233. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  234. // return false
  235. // } else if adminUser.Status == 2 {
  236. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeUserWasForbidden)
  237. // return false
  238. // } else if adminUser.IsSuperAdmin == false {
  239. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  240. // return false
  241. // }
  242. // return true
  243. // }
  244. // func (this *RoleAPIController) isAppRoleExist(orgID int64, appID int64, userID int64) bool {
  245. // appRole, getAppRoleErr := service.GetAppRole(orgID, appID, userID)
  246. // if getAppRoleErr != nil {
  247. // beego.Error("检查用户和机构应用对应关系时失败:%v", getAppRoleErr)
  248. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  249. // return false
  250. // } else if appRole == nil {
  251. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  252. // return false
  253. // }
  254. // return true
  255. // }
  256. // /api/adminmain [get]
  257. func (this *RoleAPIController) AdminMainView() {
  258. adminUserInfo := this.GetAdminUserInfo()
  259. //org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  260. var isSubSuperAdmin bool = false
  261. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  262. //app_role, _ := service.GetAppRoleById(adminUserInfo.)
  263. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  264. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  265. if adminUserInfo.AdminUser.Id != org.Creator {
  266. for _, item := range role_ids {
  267. id, _ := strconv.ParseInt(item, 10, 64)
  268. role, _ := service.GetRoleByRoleID(id)
  269. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  270. isSubSuperAdmin = true
  271. }
  272. }
  273. }
  274. viewModels, _, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
  275. if getAdminsErr != nil {
  276. //beego.Error("获取管理员列表失败:", getAdminsErr)
  277. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  278. return
  279. }
  280. this.ServeSuccessJSON(map[string]interface{}{
  281. "admins": viewModels,
  282. "org": org,
  283. "isSubSuperAdmin": isSubSuperAdmin,
  284. })
  285. }
  286. // /api/admins [get]
  287. // @param page?:int
  288. func (this *RoleAPIController) Admins() {
  289. adminUserInfo := this.GetAdminUserInfo()
  290. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  291. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  292. // return
  293. //}
  294. page, _ := this.GetInt("page")
  295. viewModels, total, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 100)
  296. if getAdminsErr != nil {
  297. //beego.Error("获取管理员列表失败:", getAdminsErr)
  298. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  299. } else {
  300. this.ServeSuccessJSON(map[string]interface{}{
  301. "admins": viewModels,
  302. "total_count": total,
  303. })
  304. }
  305. }
  306. // /api/admin/addinit [get]
  307. func (this *RoleAPIController) AddAdminInitData() {
  308. adminUserInfo := this.GetAdminUserInfo()
  309. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  310. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  311. // return
  312. //}
  313. var isSubSuperAdmin bool = false
  314. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  315. //app_role, _ := service.GetAppRoleById(adminUserInfo.)
  316. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  317. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  318. if adminUserInfo.AdminUser.Id != org.Creator {
  319. for _, item := range role_ids {
  320. id, _ := strconv.ParseInt(item, 10, 64)
  321. role, _ := service.GetRoleByRoleID(id)
  322. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  323. isSubSuperAdmin = true
  324. }
  325. }
  326. }
  327. roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  328. if getRoleErr != nil {
  329. //beego.Error("获取所有角色失败:", getRoleErr)
  330. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  331. return
  332. }
  333. redisClient := service.RedisClient()
  334. defer redisClient.Close()
  335. qntoken, _ := redisClient.Get("qn_token").Result()
  336. this.ServeSuccessJSON(map[string]interface{}{
  337. "roles": roles,
  338. "qntoken": qntoken,
  339. "isSubSuperAdmin": isSubSuperAdmin,
  340. "org": org,
  341. })
  342. }
  343. // /api/admin/add [post]
  344. // @param mobile:string
  345. // @param name:string
  346. // @param type:int 管理员类型:2.医生 3.护士 4.运营
  347. // @param title:int 用户职称(1.医士;2.医师;3.住院医师;4.主治医师;5.副主任医师;6.主任医师;7.护士;8.护师;9.主管护师;10.副主任护师;11.主任护师;12.运营专员;13.运营主管)
  348. // @param role:int
  349. // @param intro?:string
  350. func (this *RoleAPIController) AddAdmin() {
  351. adminUserInfo := this.GetAdminUserInfo()
  352. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  353. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  354. // return
  355. //}
  356. mobile := this.GetString("mobile")
  357. name := this.GetString("name")
  358. userType, _ := this.GetInt("type")
  359. userTitle, _ := this.GetInt("title")
  360. roleIds := this.GetString("role")
  361. user_title_name := this.GetString("user_title_name")
  362. if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || len(roleIds) <= 0 {
  363. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  364. return
  365. }
  366. // 判断是否已存在该手机号
  367. if adminUser, err := service.GetValidAdminUserByMobileReturnErr(mobile); err != nil {
  368. //beego.Error("查询用户是否已被添加为管理员时失败:", err)
  369. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  370. return
  371. } else {
  372. if adminUser == nil { //新增账号和用户
  373. _, password, createErr := service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, user_title_name, roleIds, userType, userTitle)
  374. if createErr != nil {
  375. //beego.Error("创建管理员失败:", createErr)
  376. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  377. return
  378. } else {
  379. sendSMSErr := service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
  380. if sendSMSErr != nil {
  381. }
  382. this.ServeSuccessJSON(nil)
  383. return
  384. }
  385. } else {
  386. total, _ := service.FindAdminUserByID(adminUser.Id, adminUserInfo.CurrentOrgId)
  387. if total <= 0 {
  388. //新增用户
  389. app_role := &models.App_Role{
  390. AdminUserId: adminUser.Id,
  391. OrgId: adminUserInfo.CurrentOrgId,
  392. AppId: adminUserInfo.CurrentAppId,
  393. Avatar: "",
  394. UserName: name,
  395. UserTitleName: user_title_name,
  396. Status: 1,
  397. UserType: int8(userType),
  398. UserTitle: int8(userTitle),
  399. CreateTime: time.Now().Unix(),
  400. ModifyTime: time.Now().Unix(),
  401. RoleIds: roleIds,
  402. }
  403. err := service.CreateUserRole(app_role)
  404. if err != nil {
  405. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  406. return
  407. }
  408. this.ServeSuccessJSON(nil)
  409. } else {
  410. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateStaffException)
  411. return
  412. }
  413. return
  414. }
  415. }
  416. }
  417. // /api/admin/editinit [get]
  418. // @param uid:int
  419. func (this *RoleAPIController) EditAdminInitData() {
  420. adminUserInfo := this.GetAdminUserInfo()
  421. admin_user_id, _ := this.GetInt64("uid")
  422. if admin_user_id <= 0 {
  423. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  424. return
  425. }
  426. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id)
  427. if getAppRoleErr != nil {
  428. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  429. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  430. return
  431. }
  432. if appRole == nil {
  433. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  434. return
  435. }
  436. roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  437. if getRoleErr != nil {
  438. //beego.Error("获取所有角色失败:", getRoleErr)
  439. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  440. return
  441. }
  442. var isSubSuperAdmin bool = false
  443. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  444. //app_role, _ := service.GetAppRoleById(adminUserInfo.)
  445. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  446. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  447. if adminUserInfo.AdminUser.Id != org.Creator {
  448. for _, item := range role_ids {
  449. id, _ := strconv.ParseInt(item, 10, 64)
  450. role, _ := service.GetRoleByRoleID(id)
  451. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  452. isSubSuperAdmin = true
  453. }
  454. }
  455. }
  456. redisClient := service.RedisClient()
  457. defer redisClient.Close()
  458. qntoken, _ := redisClient.Get("qn_token").Result()
  459. this.ServeSuccessJSON(map[string]interface{}{
  460. "admin": appRole,
  461. "roles": roles,
  462. "qntoken": qntoken,
  463. "isSubSuperAdmin": isSubSuperAdmin,
  464. })
  465. }
  466. // /api/admin/edit [post]
  467. // @param uid:int
  468. // @param name:string
  469. // @param type:int
  470. // @param title:int
  471. // @param role:int
  472. // @param intro?:string
  473. func (this *RoleAPIController) EditAdmin() {
  474. adminUserInfo := this.GetAdminUserInfo()
  475. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  476. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  477. // return
  478. //}
  479. adminUserId, _ := this.GetInt64("uid")
  480. name := this.GetString("name")
  481. userType, _ := this.GetInt("type")
  482. userTitle, _ := this.GetInt("title")
  483. roleIds := this.GetString("role")
  484. intro := this.GetString("intro")
  485. user_title_name := this.GetString("user_title_name")
  486. _, titleExist := models.UserTitle[userTitle]
  487. if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || len(roleIds) <= 0 {
  488. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  489. return
  490. }
  491. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId)
  492. if getAppRoleErr != nil {
  493. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  494. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  495. return
  496. }
  497. if appRole == nil {
  498. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  499. return
  500. }
  501. appRole.UserName = name
  502. appRole.UserType = int8(userType)
  503. appRole.UserTitle = int8(userTitle)
  504. appRole.RoleIds = roleIds
  505. appRole.Intro = intro
  506. appRole.UserTitleName = user_title_name
  507. appRole.ModifyTime = time.Now().Unix()
  508. saveErr := service.SaveAppRole(appRole)
  509. if saveErr != nil {
  510. //beego.Error("修改App_Role失败:", saveErr)
  511. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  512. } else {
  513. this.ServeSuccessJSON(nil)
  514. }
  515. }
  516. // /api/admin/setstatus [post]
  517. // @param uid:int
  518. // @param enable:bool
  519. func (this *RoleAPIController) AdminSetStatus() {
  520. adminUserInfo := this.GetAdminUserInfo()
  521. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  522. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  523. // return
  524. //}
  525. userID, _ := this.GetInt64("uid")
  526. if userID <= 0 {
  527. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  528. return
  529. }
  530. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID)
  531. if getAppRoleErr != nil {
  532. //beego.Error("查询管理员信息失败:", getAppRoleErr)
  533. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  534. return
  535. } else if appRole == nil {
  536. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  537. return
  538. }
  539. enable, _ := this.GetBool("enable")
  540. if enable {
  541. appRole.Status = 1
  542. } else {
  543. appRole.Status = 0
  544. }
  545. appRole.ModifyTime = time.Now().Unix()
  546. saveErr := service.SaveAppRole(appRole)
  547. if saveErr != nil {
  548. //beego.Error("保存AppRole失败:", saveErr)
  549. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  550. } else {
  551. this.ServeSuccessJSON(nil)
  552. }
  553. }
  554. // /api/admin/specialpermission/initdata [get]
  555. func (this *RoleAPIController) SpecialPermissionInitData() {
  556. adminUserInfo := this.GetAdminUserInfo()
  557. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  558. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  559. // return
  560. //}
  561. adminUsers, getAdminUsersErr := service.GetAllGeneralAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  562. if getAdminUsersErr != nil {
  563. this.ErrorLog("获取所有普通用户失败:%v", getAdminUsersErr)
  564. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  565. return
  566. }
  567. headNurses, getAllHeadNursesErr := service.GetAllValidAdminUsersWithSpecialPermission(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  568. if getAllHeadNursesErr != nil {
  569. this.ErrorLog("获取所有拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  570. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  571. return
  572. }
  573. this.ServeSuccessJSON(map[string]interface{}{
  574. "users": adminUsers,
  575. "head_nurses": headNurses,
  576. })
  577. }
  578. // /api/admin/specialpermission/dialysisrecord/submit [post]
  579. // @param ids:string ("1,2,5")
  580. func (this *RoleAPIController) SubmitDialysisRecordPermission() {
  581. adminUserInfo := this.GetAdminUserInfo()
  582. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  583. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  584. // return
  585. //}
  586. idsString := this.GetString("ids")
  587. if len(idsString) == 0 {
  588. // 取消所有用户的护士长权限
  589. cancelErr := service.CancelAllSpecialPermissionAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  590. if cancelErr != nil {
  591. this.ErrorLog("取消所有用户的护士长权限失败:%v", cancelErr)
  592. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  593. return
  594. } else {
  595. this.ServeSuccessJSON(nil)
  596. return
  597. }
  598. } else {
  599. ids := make([]int64, 0)
  600. idStrs := strings.Split(idsString, ",")
  601. for _, idStr := range idStrs {
  602. id, parseErr := strconv.Atoi(idStr)
  603. if parseErr != nil {
  604. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  605. return
  606. }
  607. ids = append(ids, int64(id))
  608. }
  609. headNurses, getAllHeadNursesErr := service.GetAllSpecialPermissionAdminUsersWithoutStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  610. if getAllHeadNursesErr != nil {
  611. this.ErrorLog("获取所有拥有或曾拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  612. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  613. return
  614. }
  615. cancelList := make([]*models.AdminUserSpecialPermission, 0)
  616. addList := make([]*models.AdminUserSpecialPermission, 0)
  617. for _, id := range ids {
  618. exit := false
  619. for _, headNurse := range headNurses {
  620. if headNurse.AdminUserID == id {
  621. exit = true
  622. if headNurse.Status != 1 {
  623. headNurse.Status = 1
  624. headNurse.ModifyTime = time.Now().Unix()
  625. addList = append(addList, headNurse)
  626. }
  627. break
  628. }
  629. }
  630. if exit == false {
  631. newHeadNurse := &models.AdminUserSpecialPermission{
  632. OrgID: adminUserInfo.CurrentOrgId,
  633. AppID: adminUserInfo.CurrentAppId,
  634. AdminUserID: id,
  635. Permission: int64(models.SpecialPermissionTypeHeadNurse),
  636. Status: 1,
  637. CreateTime: time.Now().Unix(),
  638. ModifyTime: time.Now().Unix(),
  639. }
  640. addList = append(addList, newHeadNurse)
  641. }
  642. }
  643. for _, headNurse := range headNurses {
  644. cancel := true
  645. for _, willAdd := range addList {
  646. if willAdd.AdminUserID == headNurse.AdminUserID {
  647. cancel = false
  648. break
  649. }
  650. }
  651. if cancel {
  652. headNurse.Status = 0
  653. headNurse.ModifyTime = time.Now().Unix()
  654. cancelList = append(cancelList, headNurse)
  655. }
  656. }
  657. addErr := service.BatchSaveSpecialPermissionAdminUsers(addList)
  658. if addErr != nil {
  659. this.ErrorLog("授权失败:%v", addErr)
  660. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  661. return
  662. }
  663. cancelErr := service.BatchSaveSpecialPermissionAdminUsers(cancelList)
  664. if cancelErr != nil {
  665. this.ErrorLog("取消授权失败:%v", cancelErr)
  666. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  667. return
  668. }
  669. this.ServeSuccessJSON(nil)
  670. }
  671. }
  672. func (this *RoleAPIController) GetAllOrgRole() {
  673. adminUserInfo := this.GetAdminUserInfo()
  674. var isSubSuperAdmin bool = false
  675. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  676. //app_role, _ := service.GetAppRoleById(adminUserInfo.)
  677. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  678. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  679. if adminUserInfo.AdminUser.Id != org.Creator {
  680. for _, item := range role_ids {
  681. id, _ := strconv.ParseInt(item, 10, 64)
  682. role, _ := service.GetRoleByRoleID(id)
  683. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  684. isSubSuperAdmin = true
  685. }
  686. }
  687. }
  688. roles, err := service.GetAllOrgValidRoles(adminUserInfo.CurrentOrgId, isSubSuperAdmin)
  689. if err != nil {
  690. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  691. return
  692. } else {
  693. this.ServeSuccessJSON(map[string]interface{}{
  694. "roles": roles,
  695. })
  696. }
  697. }
  698. func (this *RoleAPIController) GetAllOrgUser() {
  699. adminUserInfo := this.GetAdminUserInfo()
  700. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  701. viewModels, _, _ := service.GetAllAdminUsersAndRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
  702. this.ServeSuccessJSON(map[string]interface{}{
  703. "admins": viewModels,
  704. "org": org,
  705. })
  706. }
  707. func (this *RoleAPIController) AddRoleStaff() {
  708. //adminUserInfo := this.GetMobileAdminUserInfo()
  709. role_id, _ := this.GetInt64("id", 0)
  710. staff_ids := this.GetString("ids")
  711. ids := strings.Split(staff_ids, ",")
  712. for _, item := range ids {
  713. id, _ := strconv.ParseInt(item, 10, 64)
  714. role, _ := service.FindAdminUserID(id)
  715. role.RoleIds = role.RoleIds + "," + strconv.FormatInt(role_id, 10)
  716. service.SaveAdminUser(&role)
  717. }
  718. this.ServeSuccessJSON(map[string]interface{}{
  719. "msg": "添加成功",
  720. })
  721. }
  722. func (this *RoleAPIController) GetRoleStaff() {
  723. adminUserInfo := this.GetAdminUserInfo()
  724. viewModels, _, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
  725. if getAdminsErr != nil {
  726. //beego.Error("获取管理员列表失败:", getAdminsErr)
  727. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  728. return
  729. }
  730. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  731. this.ServeSuccessJSON(map[string]interface{}{
  732. "admins": viewModels,
  733. "org": org,
  734. })
  735. }