role_controller.go 31KB

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