role_controller.go 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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_ky_mall, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(8)
  194. //purviews_func/**/, getPurviewsErr := service.GetAllFuncPurview()
  195. //门诊
  196. outpatientRegistration, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/outpatientRegistration/manage")
  197. //电子病历
  198. ele, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/patinets/ele")
  199. //透析管理
  200. dialysis, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/dialysis/manage")
  201. //库房管理
  202. stock, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/stock/manage")
  203. //配置管理
  204. config, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/config/manage")
  205. //sub_outpatientRegistration,_ := service.GetOtherAllGeneralPurviewVMsProcessed("/outpatientRegistration/index")
  206. //GetNewAllPurview("/outpatientRegistration/index")
  207. //ele,_ := service.GetOtherNewAllPurview("/patinets/ele")
  208. //dialysis,_ := service.GetOtherNewAllPurview("/dialysis/manage")
  209. //stock,_ := service.GetOtherNewAllPurview("/stock/manage")
  210. //config,_ := service.GetOtherNewAllPurview("/config/manage")
  211. //outpatientRegistration,_ := service.GetOtherNewAllPurview("/outpatientRegistration/manage")
  212. //
  213. //for _, item := range dialysis {
  214. // if item.Name == "透析记录" {
  215. // item.Childs = purviews_func
  216. // }
  217. //}
  218. //
  219. //for _, item := range outpatientRegistration {
  220. // if item.Name == "门诊挂号" {
  221. // fmt.Println("111111")
  222. // item.Childs = purviews_func
  223. //
  224. // }
  225. //}
  226. purviews := []*service.PurviewTreeViewModel{}
  227. menzhen := &service.PurviewTreeViewModel{
  228. ID: 0,
  229. PID: 0,
  230. Name: "门诊管理",
  231. Number: 3,
  232. Childs: outpatientRegistration,
  233. }
  234. purviews = append(purviews, menzhen)
  235. //purviews =
  236. dianzi := &service.PurviewTreeViewModel{
  237. ID: 0,
  238. PID: 0,
  239. Name: "电子病历",
  240. Number: 3,
  241. Childs: ele,
  242. }
  243. purviews = append(purviews, dianzi)
  244. touxi := &service.PurviewTreeViewModel{
  245. ID: 0,
  246. PID: 0,
  247. Name: "透析管理",
  248. Number: 3,
  249. Childs: dialysis,
  250. }
  251. purviews = append(purviews, touxi)
  252. kufang := &service.PurviewTreeViewModel{
  253. ID: 0,
  254. PID: 0,
  255. Name: "库房管理",
  256. Number: 3,
  257. Childs: stock,
  258. }
  259. purviews = append(purviews, kufang)
  260. ky_mall := &service.PurviewTreeViewModel{
  261. ID: 0,
  262. PID: 0,
  263. Name: "酷医商城",
  264. Number: 3,
  265. Childs: purviews_ky_mall,
  266. }
  267. purviews = append(purviews, ky_mall)
  268. cdm := &service.PurviewTreeViewModel{
  269. ID: 0,
  270. PID: 0,
  271. Number: 3,
  272. Name: "慢病管理",
  273. Childs: purviews_cdm,
  274. }
  275. purviews = append(purviews, cdm)
  276. scrm := &service.PurviewTreeViewModel{
  277. ID: 0,
  278. PID: 0,
  279. Name: "营销管理",
  280. Number: 3,
  281. Childs: purviews_scrm,
  282. }
  283. purviews = append(purviews, scrm)
  284. mall := &service.PurviewTreeViewModel{
  285. ID: 0,
  286. PID: 0,
  287. Name: "分销商城",
  288. Number: 3,
  289. Childs: purviews_mall,
  290. }
  291. purviews = append(purviews, mall)
  292. peizhi := &service.PurviewTreeViewModel{
  293. ID: 0,
  294. PID: 0,
  295. Name: "配置管理",
  296. Number: 3,
  297. Childs: config,
  298. }
  299. purviews = append(purviews, peizhi)
  300. if getPurviewsErr != nil {
  301. //beego.Error("获取所有权限时出错:", getPurviewsErr)
  302. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  303. return
  304. }
  305. rolePurviewIdStr, getRPIdsErr := service.GetRolePurviewIds(roleId)
  306. if getRPIdsErr != nil {
  307. //beego.Error("获取角色的权限时出错:", getRPIdsErr)
  308. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  309. return
  310. }
  311. roleFuncPurview, getFuncRPIdsErr := service.GetRoleFuncPurview(roleId)
  312. if getFuncRPIdsErr == gorm.ErrRecordNotFound {
  313. //beego.Error("获取角色的权限时出错:", getRPIdsErr)
  314. if roleFuncPurview.ID == 0 {
  315. rolePurviewIdStr = rolePurviewIdStr
  316. } else {
  317. rolePurviewIdStr = rolePurviewIdStr + "," + roleFuncPurview.PurviewIds
  318. }
  319. } else if getFuncRPIdsErr != nil {
  320. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  321. return
  322. } else {
  323. if roleFuncPurview.ID == 0 {
  324. rolePurviewIdStr = rolePurviewIdStr
  325. } else {
  326. rolePurviewIdStr = rolePurviewIdStr + "," + roleFuncPurview.PurviewIds
  327. }
  328. }
  329. this.ServeSuccessJSON(map[string]interface{}{
  330. "purviews": purviews,
  331. "role": role,
  332. "role_purview_ids": rolePurviewIdStr + "," + roleFuncPurview.PurviewIds,
  333. })
  334. }
  335. // /role/purview/edit [post]
  336. // @param role_id:int
  337. // @param purview_ids:string
  338. func (this *RoleAPIController) EditPurview() {
  339. adminUserInfo := this.GetAdminUserInfo()
  340. roleId, _ := this.GetInt64("role_id")
  341. purviewIds := this.GetString("purview_ids")
  342. funcPurviewIds := this.GetString("func_purview_ids")
  343. if roleId <= 0 {
  344. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  345. return
  346. }
  347. redis := service.RedisClient()
  348. defer redis.Close()
  349. users := service.GetAllUserRole(adminUserInfo.CurrentOrgId)
  350. for _, item := range users {
  351. key := "purviews_" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + strconv.FormatInt(item.AdminUserId, 10)
  352. redis.Set(key, "", time.Second)
  353. }
  354. err := service.SaveRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, purviewIds)
  355. err = service.SaveFuncRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, funcPurviewIds)
  356. if err != nil {
  357. //beego.Error("设置角色的权限时出错:", err)
  358. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  359. } else {
  360. this.ServeSuccessJSON(nil)
  361. }
  362. }
  363. // func (this *RoleAPIController) doesUserHaveAccess(userID int64) bool {
  364. // adminUser, getAdminUserErr := service.GetAdminUserByUserID(userID)
  365. // if getAdminUserErr != nil {
  366. // beego.Error("获取用户信息失败:%v", getAdminUserErr)
  367. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  368. // return false
  369. // } else if adminUser == nil {
  370. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  371. // return false
  372. // } else if adminUser.Status == 2 {
  373. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeUserWasForbidden)
  374. // return false
  375. // } else if adminUser.IsSuperAdmin == false {
  376. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  377. // return false
  378. // }
  379. // return true
  380. // }
  381. // func (this *RoleAPIController) isAppRoleExist(orgID int64, appID int64, userID int64) bool {
  382. // appRole, getAppRoleErr := service.GetAppRole(orgID, appID, userID)
  383. // if getAppRoleErr != nil {
  384. // beego.Error("检查用户和机构应用对应关系时失败:%v", getAppRoleErr)
  385. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  386. // return false
  387. // } else if appRole == nil {
  388. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  389. // return false
  390. // }
  391. // return true
  392. // }
  393. // /api/adminmain [get]
  394. func (this *RoleAPIController) AdminMainView() {
  395. adminUserInfo := this.GetAdminUserInfo()
  396. var isSubSuperAdmin bool = false
  397. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  398. if len(adminUserRole.RoleIds) > 0 {
  399. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  400. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  401. if adminUserInfo.AdminUser.Id != org.Creator {
  402. for _, item := range role_ids {
  403. id, _ := strconv.ParseInt(item, 10, 64)
  404. if id != 0 {
  405. role, _ := service.GetRoleByRoleID(id)
  406. if role != nil {
  407. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  408. isSubSuperAdmin = true
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  416. viewModels, _, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
  417. if getAdminsErr != nil {
  418. //beego.Error("获取管理员列表失败:", getAdminsErr)
  419. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  420. return
  421. }
  422. this.ServeSuccessJSON(map[string]interface{}{
  423. "admins": viewModels,
  424. "org": org,
  425. "isSubSuperAdmin": isSubSuperAdmin,
  426. })
  427. }
  428. // /api/admins [get]
  429. // @param page?:int
  430. func (this *RoleAPIController) Admins() {
  431. adminUserInfo := this.GetAdminUserInfo()
  432. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  433. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  434. // return
  435. //}
  436. page, _ := this.GetInt("page")
  437. viewModels, total, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 100)
  438. if getAdminsErr != nil {
  439. //beego.Error("获取管理员列表失败:", getAdminsErr)
  440. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  441. } else {
  442. this.ServeSuccessJSON(map[string]interface{}{
  443. "admins": viewModels,
  444. "total_count": total,
  445. })
  446. }
  447. }
  448. // /api/admin/addinit [get]
  449. func (this *RoleAPIController) AddAdminInitData() {
  450. adminUserInfo := this.GetAdminUserInfo()
  451. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  452. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  453. // return
  454. //}
  455. var isSubSuperAdmin bool = false
  456. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  457. if len(adminUserRole.RoleIds) > 0 {
  458. //app_role, _ := service.GetAppRoleById(adminUserInfo.)
  459. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  460. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  461. if adminUserInfo.AdminUser.Id != org.Creator {
  462. for _, item := range role_ids {
  463. id, _ := strconv.ParseInt(item, 10, 64)
  464. if id != 0 {
  465. role, _ := service.GetRoleByRoleID(id)
  466. if role != nil {
  467. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  468. isSubSuperAdmin = true
  469. }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  476. roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  477. if getRoleErr != nil {
  478. //beego.Error("获取所有角色失败:", getRoleErr)
  479. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  480. return
  481. }
  482. redisClient := service.RedisClient()
  483. defer redisClient.Close()
  484. qntoken, _ := redisClient.Get("qn_token").Result()
  485. this.ServeSuccessJSON(map[string]interface{}{
  486. "roles": roles,
  487. "qntoken": qntoken,
  488. "isSubSuperAdmin": isSubSuperAdmin,
  489. "org": org,
  490. })
  491. }
  492. // /api/admin/add [post]
  493. // @param mobile:string
  494. // @param name:string
  495. // @param type:int 管理员类型:2.医生 3.护士 4.运营
  496. // @param title:int 用户职称(1.医士;2.医师;3.住院医师;4.主治医师;5.副主任医师;6.主任医师;7.护士;8.护师;9.主管护师;10.副主任护师;11.主任护师;12.运营专员;13.运营主管)
  497. // @param role:int
  498. // @param intro?:string
  499. func (this *RoleAPIController) AddAdmin() {
  500. adminUserInfo := this.GetAdminUserInfo()
  501. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  502. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  503. // return
  504. //}
  505. mobile := this.GetString("mobile")
  506. name := this.GetString("name")
  507. userType, _ := this.GetInt("type")
  508. userTitle, _ := this.GetInt("title")
  509. roleIds := this.GetString("role")
  510. user_title_name := this.GetString("user_title_name")
  511. if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || len(roleIds) <= 0 {
  512. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  513. return
  514. }
  515. // 判断是否已存在该手机号
  516. if adminUser, err := service.GetValidAdminUserByMobileReturnErr(mobile); err != nil {
  517. //beego.Error("查询用户是否已被添加为管理员时失败:", err)
  518. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  519. return
  520. } else {
  521. if adminUser == nil { //新增账号和用户
  522. _, password, createErr := service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, user_title_name, roleIds, userType, userTitle)
  523. if createErr != nil {
  524. //beego.Error("创建管理员失败:", createErr)
  525. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  526. return
  527. } else {
  528. sendSMSErr := service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
  529. if sendSMSErr != nil {
  530. }
  531. this.ServeSuccessJSON(nil)
  532. return
  533. }
  534. } else {
  535. total, _ := service.FindAdminUserByID(adminUser.Id, adminUserInfo.CurrentOrgId)
  536. if total <= 0 {
  537. //新增用户
  538. app_role := &models.App_Role{
  539. AdminUserId: adminUser.Id,
  540. OrgId: adminUserInfo.CurrentOrgId,
  541. AppId: adminUserInfo.CurrentAppId,
  542. Avatar: "",
  543. UserName: name,
  544. UserTitleName: user_title_name,
  545. Status: 1,
  546. UserType: int8(userType),
  547. UserTitle: int8(userTitle),
  548. CreateTime: time.Now().Unix(),
  549. ModifyTime: time.Now().Unix(),
  550. RoleIds: roleIds,
  551. }
  552. err := service.CreateUserRole(app_role)
  553. if err != nil {
  554. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  555. return
  556. }
  557. this.ServeSuccessJSON(nil)
  558. } else {
  559. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateStaffException)
  560. return
  561. }
  562. return
  563. }
  564. }
  565. }
  566. // /api/admin/editinit [get]
  567. // @param uid:int
  568. func (this *RoleAPIController) EditAdminInitData() {
  569. adminUserInfo := this.GetAdminUserInfo()
  570. admin_user_id, _ := this.GetInt64("uid")
  571. if admin_user_id <= 0 {
  572. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  573. return
  574. }
  575. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id)
  576. if getAppRoleErr != nil {
  577. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  578. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  579. return
  580. }
  581. if appRole == nil {
  582. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  583. return
  584. }
  585. roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  586. if getRoleErr != nil {
  587. //beego.Error("获取所有角色失败:", getRoleErr)
  588. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  589. return
  590. }
  591. var isSubSuperAdmin bool = false
  592. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  593. if len(adminUserRole.RoleIds) > 0 {
  594. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  595. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  596. if adminUserInfo.AdminUser.Id != org.Creator {
  597. for _, item := range role_ids {
  598. id, _ := strconv.ParseInt(item, 10, 64)
  599. if id != 0 {
  600. role, _ := service.GetRoleByRoleID(id)
  601. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  602. isSubSuperAdmin = true
  603. }
  604. }
  605. }
  606. }
  607. }
  608. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  609. redisClient := service.RedisClient()
  610. defer redisClient.Close()
  611. qntoken, _ := redisClient.Get("qn_token").Result()
  612. this.ServeSuccessJSON(map[string]interface{}{
  613. "admin": appRole,
  614. "roles": roles,
  615. "qntoken": qntoken,
  616. "isSubSuperAdmin": isSubSuperAdmin,
  617. "org": org,
  618. })
  619. }
  620. // /api/admin/edit [post]
  621. // @param uid:int
  622. // @param name:string
  623. // @param type:int
  624. // @param title:int
  625. // @param role:int
  626. // @param intro?:string
  627. func (this *RoleAPIController) EditAdmin() {
  628. adminUserInfo := this.GetAdminUserInfo()
  629. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  630. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  631. // return
  632. //}
  633. adminUserId, _ := this.GetInt64("uid")
  634. name := this.GetString("name")
  635. userType, _ := this.GetInt("type")
  636. userTitle, _ := this.GetInt("title")
  637. roleIds := this.GetString("role")
  638. intro := this.GetString("intro")
  639. user_title_name := this.GetString("user_title_name")
  640. sort, _ := this.GetInt64("sort")
  641. _, titleExist := models.UserTitle[userTitle]
  642. if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || len(roleIds) <= 0 {
  643. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  644. return
  645. }
  646. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId)
  647. if getAppRoleErr != nil {
  648. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  649. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  650. return
  651. }
  652. if appRole == nil {
  653. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  654. return
  655. }
  656. appRole.UserName = name
  657. appRole.UserType = int8(userType)
  658. appRole.UserTitle = int8(userTitle)
  659. appRole.RoleIds = roleIds
  660. appRole.Intro = intro
  661. appRole.UserTitleName = user_title_name
  662. appRole.ModifyTime = time.Now().Unix()
  663. appRole.Sort = sort
  664. saveErr := service.SaveAppRole(appRole)
  665. if saveErr != nil {
  666. //beego.Error("修改App_Role失败:", saveErr)
  667. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  668. } else {
  669. this.ServeSuccessJSON(nil)
  670. }
  671. }
  672. // /api/admin/setstatus [post]
  673. // @param uid:int
  674. // @param enable:bool
  675. func (this *RoleAPIController) AdminSetStatus() {
  676. adminUserInfo := this.GetAdminUserInfo()
  677. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  678. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  679. // return
  680. //}
  681. userID, _ := this.GetInt64("uid")
  682. if userID <= 0 {
  683. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  684. return
  685. }
  686. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID)
  687. if getAppRoleErr != nil {
  688. //beego.Error("查询管理员信息失败:", getAppRoleErr)
  689. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  690. return
  691. } else if appRole == nil {
  692. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  693. return
  694. }
  695. enable, _ := this.GetBool("enable")
  696. if enable {
  697. appRole.Status = 1
  698. } else {
  699. appRole.Status = 0
  700. }
  701. appRole.ModifyTime = time.Now().Unix()
  702. saveErr := service.SaveAppRole(appRole)
  703. if saveErr != nil {
  704. //beego.Error("保存AppRole失败:", saveErr)
  705. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  706. } else {
  707. this.ServeSuccessJSON(nil)
  708. }
  709. }
  710. // /api/admin/specialpermission/initdata [get]
  711. func (this *RoleAPIController) SpecialPermissionInitData() {
  712. adminUserInfo := this.GetAdminUserInfo()
  713. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  714. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  715. // return
  716. //}
  717. adminUsers, getAdminUsersErr := service.GetAllGeneralAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  718. if getAdminUsersErr != nil {
  719. this.ErrorLog("获取所有普通用户失败:%v", getAdminUsersErr)
  720. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  721. return
  722. }
  723. headNurses, getAllHeadNursesErr := service.GetAllValidAdminUsersWithSpecialPermission(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  724. if getAllHeadNursesErr != nil {
  725. this.ErrorLog("获取所有拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  726. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  727. return
  728. }
  729. this.ServeSuccessJSON(map[string]interface{}{
  730. "users": adminUsers,
  731. "head_nurses": headNurses,
  732. })
  733. }
  734. // /api/admin/specialpermission/dialysisrecord/submit [post]
  735. // @param ids:string ("1,2,5")
  736. func (this *RoleAPIController) SubmitDialysisRecordPermission() {
  737. adminUserInfo := this.GetAdminUserInfo()
  738. //if adminUserInfo.AdminUser.IsSuperAdmin == false {
  739. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  740. // return
  741. //}
  742. idsString := this.GetString("ids")
  743. if len(idsString) == 0 {
  744. // 取消所有用户的护士长权限
  745. cancelErr := service.CancelAllSpecialPermissionAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  746. if cancelErr != nil {
  747. this.ErrorLog("取消所有用户的护士长权限失败:%v", cancelErr)
  748. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  749. return
  750. } else {
  751. this.ServeSuccessJSON(nil)
  752. return
  753. }
  754. } else {
  755. ids := make([]int64, 0)
  756. idStrs := strings.Split(idsString, ",")
  757. for _, idStr := range idStrs {
  758. id, parseErr := strconv.Atoi(idStr)
  759. if parseErr != nil {
  760. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  761. return
  762. }
  763. ids = append(ids, int64(id))
  764. }
  765. headNurses, getAllHeadNursesErr := service.GetAllSpecialPermissionAdminUsersWithoutStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  766. if getAllHeadNursesErr != nil {
  767. this.ErrorLog("获取所有拥有或曾拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  768. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  769. return
  770. }
  771. cancelList := make([]*models.AdminUserSpecialPermission, 0)
  772. addList := make([]*models.AdminUserSpecialPermission, 0)
  773. for _, id := range ids {
  774. exit := false
  775. for _, headNurse := range headNurses {
  776. if headNurse.AdminUserID == id {
  777. exit = true
  778. if headNurse.Status != 1 {
  779. headNurse.Status = 1
  780. headNurse.ModifyTime = time.Now().Unix()
  781. addList = append(addList, headNurse)
  782. }
  783. break
  784. }
  785. }
  786. if exit == false {
  787. newHeadNurse := &models.AdminUserSpecialPermission{
  788. OrgID: adminUserInfo.CurrentOrgId,
  789. AppID: adminUserInfo.CurrentAppId,
  790. AdminUserID: id,
  791. Permission: int64(models.SpecialPermissionTypeHeadNurse),
  792. Status: 1,
  793. CreateTime: time.Now().Unix(),
  794. ModifyTime: time.Now().Unix(),
  795. }
  796. addList = append(addList, newHeadNurse)
  797. }
  798. }
  799. for _, headNurse := range headNurses {
  800. cancel := true
  801. for _, willAdd := range addList {
  802. if willAdd.AdminUserID == headNurse.AdminUserID {
  803. cancel = false
  804. break
  805. }
  806. }
  807. if cancel {
  808. headNurse.Status = 0
  809. headNurse.ModifyTime = time.Now().Unix()
  810. cancelList = append(cancelList, headNurse)
  811. }
  812. }
  813. addErr := service.BatchSaveSpecialPermissionAdminUsers(addList)
  814. if addErr != nil {
  815. this.ErrorLog("授权失败:%v", addErr)
  816. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  817. return
  818. }
  819. cancelErr := service.BatchSaveSpecialPermissionAdminUsers(cancelList)
  820. if cancelErr != nil {
  821. this.ErrorLog("取消授权失败:%v", cancelErr)
  822. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  823. return
  824. }
  825. this.ServeSuccessJSON(nil)
  826. }
  827. }
  828. func (this *RoleAPIController) GetAllOrgRole() {
  829. adminUserInfo := this.GetAdminUserInfo()
  830. var isSubSuperAdmin bool = false
  831. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  832. if len(adminUserRole.RoleIds) > 0 {
  833. //app_role, _ := service.GetAppRoleById(adminUserInfo.)
  834. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  835. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  836. if adminUserInfo.AdminUser.Id != org.Creator {
  837. for _, item := range role_ids {
  838. id, _ := strconv.ParseInt(item, 10, 64)
  839. if id != 0 {
  840. role, _ := service.GetRoleByRoleID(id)
  841. if role != nil {
  842. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  843. isSubSuperAdmin = true
  844. }
  845. }
  846. }
  847. }
  848. }
  849. }
  850. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  851. roles, err := service.GetAllOrgValidRoles(adminUserInfo.CurrentOrgId, isSubSuperAdmin)
  852. if err != nil {
  853. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  854. return
  855. } else {
  856. this.ServeSuccessJSON(map[string]interface{}{
  857. "roles": roles,
  858. "isSubSuperAdmin": isSubSuperAdmin,
  859. "org": org,
  860. })
  861. }
  862. }
  863. func (this *RoleAPIController) GetAllOrgUser() {
  864. adminUserInfo := this.GetAdminUserInfo()
  865. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  866. var isSubSuperAdmin bool = false
  867. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  868. if len(adminUserRole.RoleIds) > 0 {
  869. //app_role, _ := service.GetAppRoleById(adminUserInfo.)
  870. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  871. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  872. if adminUserInfo.AdminUser.Id != org.Creator {
  873. for _, item := range role_ids {
  874. id, _ := strconv.ParseInt(item, 10, 64)
  875. if id != 0 {
  876. role, _ := service.GetRoleByRoleID(id)
  877. if role != nil {
  878. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  879. isSubSuperAdmin = true
  880. }
  881. }
  882. }
  883. }
  884. }
  885. }
  886. viewModels, _, _ := service.GetAllAdminUsersAndRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
  887. this.ServeSuccessJSON(map[string]interface{}{
  888. "admins": viewModels,
  889. "org": org,
  890. "isSubSuperAdmin": isSubSuperAdmin,
  891. })
  892. }
  893. func (this *RoleAPIController) AddRoleStaff() {
  894. //adminUserInfo := this.GetMobileAdminUserInfo()
  895. role_id, _ := this.GetInt64("id", 0)
  896. staff_ids := this.GetString("ids")
  897. ids := strings.Split(staff_ids, ",")
  898. for _, item := range ids {
  899. id, _ := strconv.ParseInt(item, 10, 64)
  900. role, _ := service.FindAdminUserID(id)
  901. role.RoleIds = role.RoleIds + "," + strconv.FormatInt(role_id, 10)
  902. service.SaveAdminUser(&role)
  903. }
  904. this.ServeSuccessJSON(map[string]interface{}{
  905. "msg": "添加成功",
  906. })
  907. }
  908. func (this *RoleAPIController) GetRoleStaff() {
  909. adminUserInfo := this.GetAdminUserInfo()
  910. var isSubSuperAdmin bool = false
  911. adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  912. if len(adminUserRole.RoleIds) > 0 {
  913. role_ids := strings.Split(adminUserRole.RoleIds, ",")
  914. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  915. if adminUserInfo.AdminUser.Id != org.Creator {
  916. for _, item := range role_ids {
  917. id, _ := strconv.ParseInt(item, 10, 64)
  918. if id != 0 {
  919. role, _ := service.GetRoleByRoleID(id)
  920. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  921. isSubSuperAdmin = true
  922. }
  923. }
  924. }
  925. }
  926. }
  927. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  928. viewModels, _, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
  929. if getAdminsErr != nil {
  930. //beego.Error("获取管理员列表失败:", getAdminsErr)
  931. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  932. return
  933. }
  934. this.ServeSuccessJSON(map[string]interface{}{
  935. "admins": viewModels,
  936. "org": org,
  937. "isSubSuperAdmin": isSubSuperAdmin,
  938. })
  939. }
  940. func (this *RoleAPIController) GetRoleInfo() {
  941. roleId, _ := this.GetInt64("role_id")
  942. if roleId <= 0 {
  943. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  944. return
  945. }
  946. role, _ := service.GetRoleByRoleID(roleId)
  947. this.ServeSuccessJSON(map[string]interface{}{
  948. "role": role,
  949. })
  950. }