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. "SCRM/service/sms_service"
  10. "time"
  11. "github.com/astaxie/beego"
  12. )
  13. func AdminCtlRegistRouters() {
  14. beego.Router("/api/adminmain", &AdminAPIController{}, "get:AdminMainView")
  15. beego.Router("/api/admins", &AdminAPIController{}, "get:Admins")
  16. beego.Router("/api/admin/addinit", &AdminAPIController{}, "get:AddAdminInitData")
  17. beego.Router("/api/admin/add", &AdminAPIController{}, "post:AddAdmin")
  18. beego.Router("/api/admin/editinit", &AdminAPIController{}, "get:EditAdminInitData")
  19. beego.Router("/api/admin/edit", &AdminAPIController{}, "post:EditAdmin")
  20. beego.Router("/api/admin/setstatus", &AdminAPIController{}, "post:AdminSetStatus")
  21. }
  22. type AdminAPIController struct {
  23. base_ctl.BaseAuthAPIController
  24. }
  25. // /api/adminmain [get]
  26. func (this *AdminAPIController) AdminMainView() {
  27. adminUserInfo := this.GetAdminUserInfo()
  28. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  29. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  30. return
  31. }
  32. viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 10)
  33. if getAdminsErr != nil {
  34. this.ErrorLog("获取管理员列表失败:", getAdminsErr)
  35. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  36. return
  37. }
  38. existRoleCount, _ := role_service.GetValidRoleCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  39. this.ServeSuccessJSON(map[string]interface{}{
  40. "admins": viewModels,
  41. "total_count": total,
  42. "is_exist_role": existRoleCount > 0,
  43. })
  44. }
  45. // /api/admins [get]
  46. // @param page?:int
  47. func (this *AdminAPIController) Admins() {
  48. adminUserInfo := this.GetAdminUserInfo()
  49. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  50. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  51. return
  52. }
  53. page, _ := this.GetInt("page")
  54. viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10)
  55. if getAdminsErr != nil {
  56. this.ErrorLog("获取管理员列表失败:", getAdminsErr)
  57. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  58. } else {
  59. this.ServeSuccessJSON(map[string]interface{}{
  60. "admins": viewModels,
  61. "total_count": total,
  62. })
  63. }
  64. }
  65. // /api/admin/addinit [get]
  66. func (this *AdminAPIController) AddAdminInitData() {
  67. adminUserInfo := this.GetAdminUserInfo()
  68. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  69. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  70. return
  71. }
  72. roles, getRoleErr := role_service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  73. if getRoleErr != nil {
  74. this.ErrorLog("获取所有角色失败:", getRoleErr)
  75. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  76. return
  77. }
  78. redisClient := service.RedisClient()
  79. defer redisClient.Close()
  80. qntoken, _ := redisClient.Get("qn_token").Result()
  81. this.ServeSuccessJSON(map[string]interface{}{
  82. "roles": roles,
  83. "qntoken": qntoken,
  84. })
  85. }
  86. // /api/admin/add [post]
  87. // @param mobile:string
  88. // @param name:string
  89. // @param type:int 管理员类型:2.医生 3.护士 4.运营
  90. // @param title:int 用户职称(1.医士;2.医师;3.住院医师;4.主治医师;5.副主任医师;6.主任医师;7.护士;8.护师;9.主管护师;10.副主任护师;11.主任护师;12.运营专员;13.运营主管)
  91. // @param role:int
  92. // @param intro?:string
  93. func (this *AdminAPIController) AddAdmin() {
  94. adminUserInfo := this.GetAdminUserInfo()
  95. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  96. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  97. return
  98. }
  99. mobile := this.GetString("mobile")
  100. name := this.GetString("name")
  101. userType, _ := this.GetInt("type")
  102. userTitle, _ := this.GetInt("title")
  103. roleId, _ := this.GetInt64("role")
  104. intro := this.GetString("intro")
  105. _, titleExist := models.UserTitle[userTitle]
  106. if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
  107. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  108. return
  109. }
  110. isRoleExist, getRoleErr := role_service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
  111. if getRoleErr != nil {
  112. this.ErrorLog("查询角色是否存在时失败:", getRoleErr)
  113. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  114. return
  115. }
  116. if !isRoleExist {
  117. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  118. return
  119. }
  120. // 判断该应用是否已存在该手机号
  121. if isMobileDidUsed, err := role_service.IsMobileDidUsedAtApp(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile); err != nil {
  122. this.ErrorLog("查询用户是否已被添加为管理员时失败:", err)
  123. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  124. return
  125. } else {
  126. if isMobileDidUsed {
  127. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileDidUsedInApp)
  128. return
  129. }
  130. }
  131. if isSuperAdmin, err := role_service.IsUserSuperAdminWithMobile(mobile); err != nil {
  132. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileNotExit)
  133. return
  134. } else {
  135. if isSuperAdmin {
  136. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleMobileIsSuperAdmin)
  137. return
  138. }
  139. }
  140. _, password, createErr := role_service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, userType, userTitle, intro, roleId)
  141. if createErr != nil {
  142. this.ErrorLog("创建管理员失败:", createErr)
  143. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  144. return
  145. } else {
  146. this.TraceLog("%v", password)
  147. // 发送短信通知这个手机号
  148. sendSMSErr := sms_service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
  149. if sendSMSErr != nil {
  150. this.ErrorLog("发送邀请短信失败:%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. this.ErrorLog("获取管理员信息失败:", 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. this.ErrorLog("获取所有角色失败:", 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. this.ErrorLog("查询管理员信息时失败:", 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. this.ErrorLog("查询角色是否存在时失败:", 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. this.ErrorLog("修改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. this.ErrorLog("查询管理员信息失败:", 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. this.ErrorLog("保存AppRole失败:", saveErr)
  291. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  292. } else {
  293. this.ServeSuccessJSON(nil)
  294. }
  295. }