role_controller.go 28KB

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