role_controller.go 30KB

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