role_controller.go 30KB

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