scrm-go

admin_controller.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package role
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/models"
  6. "SCRM/service"
  7. base_service "SCRM/service"
  8. "SCRM/service/role_service"
  9. "time"
  10. "github.com/astaxie/beego"
  11. )
  12. func AdminCtlRegistRouters() {
  13. beego.Router("/api/adminmain", &AdminAPIController{}, "get:AdminMainView")
  14. beego.Router("/api/admins", &AdminAPIController{}, "get:Admins")
  15. beego.Router("/api/admin/addinit", &AdminAPIController{}, "get:AddAdminInitData")
  16. beego.Router("/api/admin/add", &AdminAPIController{}, "post:AddAdmin")
  17. beego.Router("/api/admin/editinit", &AdminAPIController{}, "get:EditAdminInitData")
  18. beego.Router("/api/admin/edit", &AdminAPIController{}, "post:EditAdmin")
  19. beego.Router("/api/admin/setstatus", &AdminAPIController{}, "post:AdminSetStatus")
  20. }
  21. type AdminAPIController struct {
  22. base_ctl.BaseAuthAPIController
  23. }
  24. // /api/adminmain [get]
  25. func (this *AdminAPIController) AdminMainView() {
  26. adminUserInfo := this.GetAdminUserInfo()
  27. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  28. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  29. return
  30. }
  31. viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 10)
  32. if getAdminsErr != nil {
  33. //beego.Error("获取管理员列表失败:", getAdminsErr)
  34. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  35. return
  36. }
  37. existRoleCount, _ := role_service.GetValidRoleCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  38. this.ServeSuccessJSON(map[string]interface{}{
  39. "admins": viewModels,
  40. "total_count": total,
  41. "is_exist_role": existRoleCount > 0,
  42. })
  43. }
  44. // /api/admins [get]
  45. // @param page?:int
  46. func (this *AdminAPIController) Admins() {
  47. adminUserInfo := this.GetAdminUserInfo()
  48. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  49. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  50. return
  51. }
  52. page, _ := this.GetInt("page")
  53. viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10)
  54. if getAdminsErr != nil {
  55. //beego.Error("获取管理员列表失败:", getAdminsErr)
  56. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  57. } else {
  58. this.ServeSuccessJSON(map[string]interface{}{
  59. "admins": viewModels,
  60. "total_count": total,
  61. })
  62. }
  63. }
  64. // /api/admin/addinit [get]
  65. func (this *AdminAPIController) AddAdminInitData() {
  66. adminUserInfo := this.GetAdminUserInfo()
  67. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  68. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  69. return
  70. }
  71. roles, getRoleErr := role_service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  72. if getRoleErr != nil {
  73. //beego.Error("获取所有角色失败:", getRoleErr)
  74. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  75. return
  76. }
  77. redisClient := service.RedisClient()
  78. defer redisClient.Close()
  79. qntoken, _ := redisClient.Get("qn_token").Result()
  80. this.ServeSuccessJSON(map[string]interface{}{
  81. "roles": roles,
  82. "qntoken": qntoken,
  83. })
  84. }
  85. // /api/admin/add [post]
  86. // @param mobile:string
  87. // @param name:string
  88. // @param type:int 管理员类型:2.医生 3.护士 4.运营
  89. // @param title:int 用户职称(1.医士;2.医师;3.住院医师;4.主治医师;5.副主任医师;6.主任医师;7.护士;8.护师;9.主管护师;10.副主任护师;11.主任护师;12.运营专员;13.运营主管)
  90. // @param role:int
  91. // @param intro?:string
  92. func (this *AdminAPIController) AddAdmin() {
  93. adminUserInfo := this.GetAdminUserInfo()
  94. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  95. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  96. return
  97. }
  98. mobile := this.GetString("mobile")
  99. name := this.GetString("name")
  100. userType, _ := this.GetInt("type")
  101. userTitle, _ := this.GetInt("title")
  102. roleId, _ := this.GetInt64("role")
  103. intro := this.GetString("intro")
  104. _, titleExist := models.UserTitle[userTitle]
  105. if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
  106. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  107. return
  108. }
  109. isRoleExist, getRoleErr := role_service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
  110. if getRoleErr != nil {
  111. //beego.Error("查询角色是否存在时失败:", getRoleErr)
  112. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  113. return
  114. }
  115. if !isRoleExist {
  116. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  117. return
  118. }
  119. // 判断该应用是否已存在该手机号
  120. if isMobileDidUsed, err := role_service.IsMobileDidUsedAtApp(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile); err != nil {
  121. //beego.Error("查询用户是否已被添加为管理员时失败:", err)
  122. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  123. return
  124. } else {
  125. if isMobileDidUsed {
  126. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileDidUsedInApp)
  127. return
  128. }
  129. }
  130. if isSuperAdmin, err := role_service.IsUserSuperAdminWithMobile(mobile); err != nil {
  131. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileNotExit)
  132. return
  133. } else {
  134. if isSuperAdmin {
  135. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleMobileIsSuperAdmin)
  136. return
  137. }
  138. }
  139. _, password, createErr := role_service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, userType, userTitle, intro, roleId)
  140. if createErr != nil {
  141. //beego.Error("创建管理员失败:", createErr)
  142. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  143. return
  144. } else {
  145. this.TraceLog("%v", password)
  146. //beego.Trace("用户密码:", password)
  147. // 发送短信通知这个手机号
  148. // sendSMSErr := role_service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
  149. // if sendSMSErr != nil {
  150. // //beego.Error("发送邀请短信失败:%v", sendSMSErr)
  151. // }
  152. this.ServeSuccessJSON(nil)
  153. return
  154. }
  155. }
  156. // /api/admin/editinit [get]
  157. // @param uid:int
  158. func (this *AdminAPIController) EditAdminInitData() {
  159. adminUserInfo := this.GetAdminUserInfo()
  160. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  161. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  162. return
  163. }
  164. admin_user_id, _ := this.GetInt64("uid")
  165. if admin_user_id <= 0 {
  166. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  167. return
  168. }
  169. adminUserViewModel, getInfoErr := role_service.GetGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id)
  170. if getInfoErr != nil {
  171. //beego.Error("获取管理员信息失败:", getInfoErr)
  172. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  173. return
  174. }
  175. if adminUserViewModel == nil {
  176. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  177. return
  178. }
  179. roles, getRoleErr := role_service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  180. if getRoleErr != nil {
  181. //beego.Error("获取所有角色失败:", getRoleErr)
  182. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  183. return
  184. }
  185. redisClient := base_service.RedisClient()
  186. defer redisClient.Close()
  187. qntoken, _ := redisClient.Get("qn_token").Result()
  188. this.ServeSuccessJSON(map[string]interface{}{
  189. "admin": adminUserViewModel,
  190. "roles": roles,
  191. "qntoken": qntoken,
  192. })
  193. }
  194. // /api/admin/edit [post]
  195. // @param uid:int
  196. // @param name:string
  197. // @param type:int
  198. // @param title:int
  199. // @param role:int
  200. // @param intro?:string
  201. func (this *AdminAPIController) EditAdmin() {
  202. adminUserInfo := this.GetAdminUserInfo()
  203. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  204. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  205. return
  206. }
  207. adminUserId, _ := this.GetInt64("uid")
  208. name := this.GetString("name")
  209. userType, _ := this.GetInt("type")
  210. userTitle, _ := this.GetInt("title")
  211. roleId, _ := this.GetInt64("role")
  212. intro := this.GetString("intro")
  213. _, titleExist := models.UserTitle[userTitle]
  214. if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
  215. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  216. return
  217. }
  218. appRole, getAppRoleErr := role_service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId)
  219. if getAppRoleErr != nil {
  220. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  221. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  222. return
  223. }
  224. if appRole == nil {
  225. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  226. return
  227. }
  228. isRoleExist, getRoleErr := role_service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
  229. if getRoleErr != nil {
  230. //beego.Error("查询角色是否存在时失败:", getRoleErr)
  231. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  232. return
  233. }
  234. if !isRoleExist {
  235. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  236. return
  237. }
  238. appRole.UserName = name
  239. appRole.UserType = int8(userType)
  240. appRole.UserTitle = int8(userTitle)
  241. appRole.RoleId = roleId
  242. appRole.Intro = intro
  243. appRole.ModifyTime = time.Now().Unix()
  244. saveErr := role_service.SaveAppRole(appRole)
  245. if saveErr != nil {
  246. //beego.Error("修改App_Role失败:", saveErr)
  247. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  248. } else {
  249. this.ServeSuccessJSON(nil)
  250. }
  251. }
  252. // /api/admin/setstatus [post]
  253. // @param uid:int
  254. // @param enable:bool
  255. func (this *AdminAPIController) AdminSetStatus() {
  256. adminUserInfo := this.GetAdminUserInfo()
  257. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  258. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  259. return
  260. }
  261. userID, _ := this.GetInt64("uid")
  262. if userID <= 0 {
  263. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  264. return
  265. }
  266. appRole, getAppRoleErr := role_service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID)
  267. if getAppRoleErr != nil {
  268. //beego.Error("查询管理员信息失败:", getAppRoleErr)
  269. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  270. return
  271. } else if appRole == nil {
  272. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  273. return
  274. }
  275. enable, _ := this.GetBool("enable")
  276. if enable == true {
  277. if roleEnable, _ := role_service.IsRoleExist(appRole.OrgId, appRole.AppId, appRole.RoleId); roleEnable == false {
  278. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  279. return
  280. }
  281. }
  282. if enable {
  283. appRole.Status = 1
  284. } else {
  285. appRole.Status = 0
  286. }
  287. appRole.ModifyTime = time.Now().Unix()
  288. saveErr := role_service.SaveAppRole(appRole)
  289. if saveErr != nil {
  290. //beego.Error("保存AppRole失败:", saveErr)
  291. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  292. } else {
  293. this.ServeSuccessJSON(nil)
  294. }
  295. }