package role import ( base_ctl "SCRM/controllers" "SCRM/enums" "SCRM/models" "SCRM/service" base_service "SCRM/service" "SCRM/service/role_service" "SCRM/service/sms_service" "time" "github.com/astaxie/beego" ) func AdminCtlRegistRouters() { beego.Router("/api/adminmain", &AdminAPIController{}, "get:AdminMainView") beego.Router("/api/admins", &AdminAPIController{}, "get:Admins") beego.Router("/api/admin/addinit", &AdminAPIController{}, "get:AddAdminInitData") beego.Router("/api/admin/add", &AdminAPIController{}, "post:AddAdmin") beego.Router("/api/admin/editinit", &AdminAPIController{}, "get:EditAdminInitData") beego.Router("/api/admin/edit", &AdminAPIController{}, "post:EditAdmin") beego.Router("/api/admin/setstatus", &AdminAPIController{}, "post:AdminSetStatus") } type AdminAPIController struct { base_ctl.BaseAuthAPIController } // /api/adminmain [get] func (this *AdminAPIController) AdminMainView() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 10) if getAdminsErr != nil { this.ErrorLog("获取管理员列表失败:", getAdminsErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } existRoleCount, _ := role_service.GetValidRoleCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id) this.ServeSuccessJSON(map[string]interface{}{ "admins": viewModels, "total_count": total, "is_exist_role": existRoleCount > 0, }) } // /api/admins [get] // @param page?:int func (this *AdminAPIController) Admins() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } page, _ := this.GetInt("page") viewModels, total, getAdminsErr := role_service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10) if getAdminsErr != nil { this.ErrorLog("获取管理员列表失败:", getAdminsErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) } else { this.ServeSuccessJSON(map[string]interface{}{ "admins": viewModels, "total_count": total, }) } } // /api/admin/addinit [get] func (this *AdminAPIController) AddAdminInitData() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } roles, getRoleErr := role_service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId) if getRoleErr != nil { this.ErrorLog("获取所有角色失败:", getRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } redisClient := service.RedisClient() defer redisClient.Close() qntoken, _ := redisClient.Get("qn_token").Result() this.ServeSuccessJSON(map[string]interface{}{ "roles": roles, "qntoken": qntoken, }) } // /api/admin/add [post] // @param mobile:string // @param name:string // @param type:int 管理员类型:2.医生 3.护士 4.运营 // @param title:int 用户职称(1.医士;2.医师;3.住院医师;4.主治医师;5.副主任医师;6.主任医师;7.护士;8.护师;9.主管护师;10.副主任护师;11.主任护师;12.运营专员;13.运营主管) // @param role:int // @param intro?:string func (this *AdminAPIController) AddAdmin() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } mobile := this.GetString("mobile") name := this.GetString("name") userType, _ := this.GetInt("type") userTitle, _ := this.GetInt("title") roleId, _ := this.GetInt64("role") intro := this.GetString("intro") _, titleExist := models.UserTitle[userTitle] if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } isRoleExist, getRoleErr := role_service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId) if getRoleErr != nil { this.ErrorLog("查询角色是否存在时失败:", getRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if !isRoleExist { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist) return } // 判断该应用是否已存在该手机号 if isMobileDidUsed, err := role_service.IsMobileDidUsedAtApp(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile); err != nil { this.ErrorLog("查询用户是否已被添加为管理员时失败:", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else { if isMobileDidUsed { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileDidUsedInApp) return } } if isSuperAdmin, err := role_service.IsUserSuperAdminWithMobile(mobile); err != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileNotExit) return } else { if isSuperAdmin { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleMobileIsSuperAdmin) return } } _, password, createErr := role_service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, userType, userTitle, intro, roleId) if createErr != nil { this.ErrorLog("创建管理员失败:", createErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate) return } else { this.TraceLog("%v", password) // 发送短信通知这个手机号 sendSMSErr := sms_service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password) if sendSMSErr != nil { this.ErrorLog("发送邀请短信失败:%v", sendSMSErr) } this.ServeSuccessJSON(nil) return } } // /api/admin/editinit [get] // @param uid:int func (this *AdminAPIController) EditAdminInitData() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } admin_user_id, _ := this.GetInt64("uid") if admin_user_id <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserViewModel, getInfoErr := role_service.GetGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id) if getInfoErr != nil { this.ErrorLog("获取管理员信息失败:", getInfoErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if adminUserViewModel == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist) return } roles, getRoleErr := role_service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId) if getRoleErr != nil { this.ErrorLog("获取所有角色失败:", getRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } redisClient := base_service.RedisClient() defer redisClient.Close() qntoken, _ := redisClient.Get("qn_token").Result() this.ServeSuccessJSON(map[string]interface{}{ "admin": adminUserViewModel, "roles": roles, "qntoken": qntoken, }) } // /api/admin/edit [post] // @param uid:int // @param name:string // @param type:int // @param title:int // @param role:int // @param intro?:string func (this *AdminAPIController) EditAdmin() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } adminUserId, _ := this.GetInt64("uid") name := this.GetString("name") userType, _ := this.GetInt("type") userTitle, _ := this.GetInt("title") roleId, _ := this.GetInt64("role") intro := this.GetString("intro") _, titleExist := models.UserTitle[userTitle] if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } appRole, getAppRoleErr := role_service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId) if getAppRoleErr != nil { this.ErrorLog("查询管理员信息时失败:", getAppRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if appRole == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist) return } isRoleExist, getRoleErr := role_service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId) if getRoleErr != nil { this.ErrorLog("查询角色是否存在时失败:", getRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if !isRoleExist { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist) return } appRole.UserName = name appRole.UserType = int8(userType) appRole.UserTitle = int8(userTitle) appRole.RoleId = roleId appRole.Intro = intro appRole.ModifyTime = time.Now().Unix() saveErr := role_service.SaveAppRole(appRole) if saveErr != nil { this.ErrorLog("修改App_Role失败:", saveErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate) } else { this.ServeSuccessJSON(nil) } } // /api/admin/setstatus [post] // @param uid:int // @param enable:bool func (this *AdminAPIController) AdminSetStatus() { adminUserInfo := this.GetAdminUserInfo() if adminUserInfo.AdminUser.IsSuperAdmin == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied) return } userID, _ := this.GetInt64("uid") if userID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } appRole, getAppRoleErr := role_service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID) if getAppRoleErr != nil { this.ErrorLog("查询管理员信息失败:", getAppRoleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if appRole == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist) return } enable, _ := this.GetBool("enable") if enable == true { if roleEnable, _ := role_service.IsRoleExist(appRole.OrgId, appRole.AppId, appRole.RoleId); roleEnable == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist) return } } if enable { appRole.Status = 1 } else { appRole.Status = 0 } appRole.ModifyTime = time.Now().Unix() saveErr := role_service.SaveAppRole(appRole) if saveErr != nil { this.ErrorLog("保存AppRole失败:", saveErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate) } else { this.ServeSuccessJSON(nil) } }