1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078 |
- package controllers
-
- import (
- "github.com/jinzhu/gorm"
- "strconv"
- "strings"
- "time"
-
- "XT_New/enums"
- "XT_New/models"
- "XT_New/service"
-
- "github.com/astaxie/beego"
- )
-
- func RoleAPIControllerRegistRouters() {
- beego.Router("/api/roles", &RoleAPIController{}, "get:GetRoles")
- beego.Router("/api/role/create", &RoleAPIController{}, "post:CreateRole")
- beego.Router("/api/role/modify", &RoleAPIController{}, "post:ModifyRole")
- beego.Router("/api/role/setstatus", &RoleAPIController{}, "post:ModifyRoleStatus")
-
- beego.Router("/role/purview/editinit", &RoleAPIController{}, "get:EditPurviewInitData")
- beego.Router("/role/purview/edit", &RoleAPIController{}, "post:EditPurview")
-
- beego.Router("/api/adminmain", &RoleAPIController{}, "get:AdminMainView")
- beego.Router("/api/admins", &RoleAPIController{}, "get:Admins")
- beego.Router("/api/admin/addinit", &RoleAPIController{}, "get:AddAdminInitData")
- beego.Router("/api/admin/add", &RoleAPIController{}, "post:AddAdmin")
- beego.Router("/api/admin/editinit", &RoleAPIController{}, "get:EditAdminInitData")
- beego.Router("/api/admin/edit", &RoleAPIController{}, "post:EditAdmin")
- beego.Router("/api/admin/setstatus", &RoleAPIController{}, "post:AdminSetStatus")
-
- beego.Router("/api/admin/specialpermission/initdata", &RoleAPIController{}, "get:SpecialPermissionInitData")
- beego.Router("/api/admin/specialpermission/dialysisrecord/submit", &RoleAPIController{}, "post:SubmitDialysisRecordPermission")
-
- beego.Router("/api/roles/list", &RoleAPIController{}, "get:GetAllOrgRole")
- beego.Router("/api/staff", &RoleAPIController{}, "get:GetAllOrgUser")
- beego.Router("/api/role/addStaff", &RoleAPIController{}, "post:AddRoleStaff")
-
- beego.Router("/api/role/staff", &RoleAPIController{}, "get:GetRoleStaff")
-
- beego.Router("/api/role", &RoleAPIController{}, "get:GetRoleInfo")
-
- }
-
- type RoleAPIController struct {
- BaseAuthAPIController
- }
-
- // /api/roles [get]
- // @param page?:int
- func (this *RoleAPIController) GetRoles() {
- page, _ := this.GetInt("page")
- adminUserInfo := this.GetAdminUserInfo()
- //beego.Alert(adminUserInfo.AdminUser)
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
- if page <= 0 {
- page = 1
- }
- roles, total, getRoleErr := service.GetRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 100)
- if getRoleErr != nil {
- //beego.Error("获取角色列表失败:", getRoleErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- } else {
- this.ServeSuccessJSON(map[string]interface{}{
- "roles": roles,
- "total_count": total,
- })
- }
- }
-
- // /api/role/create [post]
- // @param name:string
- // @param intro:string
- func (this *RoleAPIController) CreateRole() {
- name := this.GetString("name")
- intro := this.GetString("intro")
- if len(name) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- total := service.FindRoleRecordByRoleName(name, adminUserInfo.CurrentOrgId)
- if total > 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNameIsExist)
- return
- }
- role, createErr := service.CreateRole(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, intro)
- if createErr != nil {
- //beego.Error("创建角色失败:", createErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
- } else {
- this.ServeSuccessJSON(map[string]interface{}{
- "id": role.Id,
- "name": role.RoleName,
- "intro": role.RoleIntro,
- "status": role.Status,
- })
- }
- }
-
- // /api/role/modify
- // @param role_id:int
- // @param name:string
- // @param intro:string
- func (this *RoleAPIController) ModifyRole() {
- roleID, _ := this.GetInt64("role_id")
- name := this.GetString("name")
- intro := this.GetString("intro")
- if roleID <= 0 || len(name) == 0 || len(intro) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- //adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- role, getRoleErr := service.GetRoleByRoleID(roleID)
- if getRoleErr != nil {
- //beego.Error("获取角色失败:", getRoleErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else if role == nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
- return
- }
-
- role.RoleName = name
- role.RoleIntro = intro
- role.ModifyTime = time.Now().Unix()
- saveErr := service.ModifyRole(role)
- if saveErr != nil {
- //beego.Error("修改角色失败:", role.Id, saveErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
- } else {
- this.ServeSuccessJSON(nil)
- }
- }
-
- // /api/role/setstatus
- // @param role_id:int
- // @param enable:bool
- func (this *RoleAPIController) ModifyRoleStatus() {
- roleID, _ := this.GetInt64("role_id")
- enable, _ := this.GetBool("enable")
- if roleID <= 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- role, getRoleErr := service.GetRoleByRoleID(roleID)
- if getRoleErr != nil {
- //beego.Error("获取角色失败:", getRoleErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else if role == nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
- return
- }
-
- if enable == false {
- if count, _ := service.RoleAdminUserCountTwo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleID); count != 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCannotRemoveRole)
- return
- }
- }
-
- if enable {
- role.Status = 1
- } else {
- role.Status = 2
- }
- role.ModifyTime = time.Now().Unix()
- saveErr := service.ModifyRole(role)
- if saveErr != nil {
- //beego.Error("修改角色失败:", role.Id, saveErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
- } else {
- this.ServeSuccessJSON(nil)
- }
- }
-
- // /role/purview/editinit [get]
- // @param role_id:int
- func (this *RoleAPIController) EditPurviewInitData() {
- //adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- roleId, _ := this.GetInt64("role_id")
- if roleId <= 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- role, _ := service.GetRoleByRoleID(roleId)
-
- //purviews_xt, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(3)
- purviews_scrm, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(6)
- purviews_cdm, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(4)
- purviews_mall, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(7)
- purviews_ky_mall, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(8)
-
- //purviews_func/**/, getPurviewsErr := service.GetAllFuncPurview()
-
- //门诊
- outpatientRegistration, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/outpatientRegistration/manage")
- //电子病历
- ele, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/patinets/ele")
- //透析管理
- dialysis, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/dialysis/manage")
- //库房管理
- stock, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/stock/manage")
- //配置管理
- config, _ := service.GetOtherAllGeneralPurviewVMsProcessed("/config/manage")
-
- //sub_outpatientRegistration,_ := service.GetOtherAllGeneralPurviewVMsProcessed("/outpatientRegistration/index")
-
- //GetNewAllPurview("/outpatientRegistration/index")
-
- //ele,_ := service.GetOtherNewAllPurview("/patinets/ele")
- //dialysis,_ := service.GetOtherNewAllPurview("/dialysis/manage")
- //stock,_ := service.GetOtherNewAllPurview("/stock/manage")
- //config,_ := service.GetOtherNewAllPurview("/config/manage")
- //outpatientRegistration,_ := service.GetOtherNewAllPurview("/outpatientRegistration/manage")
-
- //
- //for _, item := range dialysis {
- // if item.Name == "透析记录" {
- // item.Childs = purviews_func
- // }
- //}
- //
- //for _, item := range outpatientRegistration {
- // if item.Name == "门诊挂号" {
- // fmt.Println("111111")
- // item.Childs = purviews_func
- //
- // }
- //}
-
- purviews := []*service.PurviewTreeViewModel{}
-
- menzhen := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "门诊管理",
- Number: 3,
- Childs: outpatientRegistration,
- }
- purviews = append(purviews, menzhen)
-
- //purviews =
-
- dianzi := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "电子病历",
- Number: 3,
- Childs: ele,
- }
- purviews = append(purviews, dianzi)
-
- touxi := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "透析管理",
- Number: 3,
- Childs: dialysis,
- }
- purviews = append(purviews, touxi)
-
- kufang := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "库房管理",
- Number: 3,
- Childs: stock,
- }
- purviews = append(purviews, kufang)
-
- ky_mall := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "酷医商城",
- Number: 3,
- Childs: purviews_ky_mall,
- }
- purviews = append(purviews, ky_mall)
-
- cdm := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Number: 3,
- Name: "慢病管理",
- Childs: purviews_cdm,
- }
-
- purviews = append(purviews, cdm)
-
- scrm := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "营销管理",
- Number: 3,
- Childs: purviews_scrm,
- }
-
- purviews = append(purviews, scrm)
-
- mall := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "分销商城",
- Number: 3,
- Childs: purviews_mall,
- }
-
- purviews = append(purviews, mall)
-
- peizhi := &service.PurviewTreeViewModel{
- ID: 0,
- PID: 0,
- Name: "配置管理",
- Number: 3,
- Childs: config,
- }
- purviews = append(purviews, peizhi)
-
- if getPurviewsErr != nil {
- //beego.Error("获取所有权限时出错:", getPurviewsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- rolePurviewIdStr, getRPIdsErr := service.GetRolePurviewIds(roleId)
- if getRPIdsErr != nil {
- //beego.Error("获取角色的权限时出错:", getRPIdsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- roleFuncPurview, getFuncRPIdsErr := service.GetRoleFuncPurview(roleId)
- if getFuncRPIdsErr == gorm.ErrRecordNotFound {
- //beego.Error("获取角色的权限时出错:", getRPIdsErr)
- if roleFuncPurview.ID == 0 {
- rolePurviewIdStr = rolePurviewIdStr
-
- } else {
- rolePurviewIdStr = rolePurviewIdStr + "," + roleFuncPurview.PurviewIds
- }
- } else if getFuncRPIdsErr != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
-
- } else {
- if roleFuncPurview.ID == 0 {
- rolePurviewIdStr = rolePurviewIdStr
- } else {
- rolePurviewIdStr = rolePurviewIdStr + "," + roleFuncPurview.PurviewIds
- }
- }
-
- this.ServeSuccessJSON(map[string]interface{}{
- "purviews": purviews,
- "role": role,
- "role_purview_ids": rolePurviewIdStr + "," + roleFuncPurview.PurviewIds,
- })
- }
-
- // /role/purview/edit [post]
- // @param role_id:int
- // @param purview_ids:string
- func (this *RoleAPIController) EditPurview() {
- adminUserInfo := this.GetAdminUserInfo()
- roleId, _ := this.GetInt64("role_id")
- purviewIds := this.GetString("purview_ids")
- funcPurviewIds := this.GetString("func_purview_ids")
-
- if roleId <= 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- redis := service.RedisClient()
- defer redis.Close()
-
- users := service.GetAllUserRole(adminUserInfo.CurrentOrgId)
- for _, item := range users {
- key := "purviews_" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + strconv.FormatInt(item.AdminUserId, 10)
- redis.Set(key, "", time.Second)
- }
-
- err := service.SaveRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, purviewIds)
- err = service.SaveFuncRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, funcPurviewIds)
-
- if err != nil {
- //beego.Error("设置角色的权限时出错:", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
- } else {
- this.ServeSuccessJSON(nil)
- }
- }
-
- // func (this *RoleAPIController) doesUserHaveAccess(userID int64) bool {
- // adminUser, getAdminUserErr := service.GetAdminUserByUserID(userID)
- // if getAdminUserErr != nil {
- // beego.Error("获取用户信息失败:%v", getAdminUserErr)
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // return false
- // } else if adminUser == nil {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
- // return false
- // } else if adminUser.Status == 2 {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeUserWasForbidden)
- // return false
- // } else if adminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return false
- // }
- // return true
- // }
-
- // func (this *RoleAPIController) isAppRoleExist(orgID int64, appID int64, userID int64) bool {
- // appRole, getAppRoleErr := service.GetAppRole(orgID, appID, userID)
- // if getAppRoleErr != nil {
- // beego.Error("检查用户和机构应用对应关系时失败:%v", getAppRoleErr)
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // return false
- // } else if appRole == nil {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return false
- // }
- // return true
- // }
-
- // /api/adminmain [get]
- func (this *RoleAPIController) AdminMainView() {
- adminUserInfo := this.GetAdminUserInfo()
- var isSubSuperAdmin bool = false
- adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
- if len(adminUserRole.RoleIds) > 0 {
- role_ids := strings.Split(adminUserRole.RoleIds, ",")
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- if adminUserInfo.AdminUser.Id != org.Creator {
- for _, item := range role_ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- if id != 0 {
- role, _ := service.GetRoleByRoleID(id)
- if role != nil {
- if role.IsSystem == 1 && role.RoleName == "子管理员" {
- isSubSuperAdmin = true
- }
- }
- }
- }
- }
- }
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
-
- viewModels, _, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
- if getAdminsErr != nil {
- //beego.Error("获取管理员列表失败:", getAdminsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- this.ServeSuccessJSON(map[string]interface{}{
- "admins": viewModels,
- "org": org,
- "isSubSuperAdmin": isSubSuperAdmin,
- })
- }
-
- // /api/admins [get]
- // @param page?:int
- func (this *RoleAPIController) Admins() {
- adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- page, _ := this.GetInt("page")
- viewModels, total, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 100)
- if getAdminsErr != nil {
- //beego.Error("获取管理员列表失败:", getAdminsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- } else {
- this.ServeSuccessJSON(map[string]interface{}{
- "admins": viewModels,
- "total_count": total,
- })
- }
- }
-
- // /api/admin/addinit [get]
- func (this *RoleAPIController) AddAdminInitData() {
- adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- var isSubSuperAdmin bool = false
- adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
-
- if len(adminUserRole.RoleIds) > 0 {
- //app_role, _ := service.GetAppRoleById(adminUserInfo.)
- role_ids := strings.Split(adminUserRole.RoleIds, ",")
-
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
-
- if adminUserInfo.AdminUser.Id != org.Creator {
- for _, item := range role_ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- if id != 0 {
- role, _ := service.GetRoleByRoleID(id)
- if role != nil {
- if role.IsSystem == 1 && role.RoleName == "子管理员" {
- isSubSuperAdmin = true
- }
- }
- }
- }
- }
- }
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
-
- roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
- if getRoleErr != nil {
- //beego.Error("获取所有角色失败:", 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,
- "isSubSuperAdmin": isSubSuperAdmin,
- "org": org,
- })
- }
-
- // /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 *RoleAPIController) 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")
- roleIds := this.GetString("role")
- user_title_name := this.GetString("user_title_name")
- if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || len(roleIds) <= 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- // 判断是否已存在该手机号
- if adminUser, err := service.GetValidAdminUserByMobileReturnErr(mobile); err != nil {
- //beego.Error("查询用户是否已被添加为管理员时失败:", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else {
- if adminUser == nil { //新增账号和用户
- _, password, createErr := service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, user_title_name, roleIds, userType, userTitle)
- if createErr != nil {
- //beego.Error("创建管理员失败:", createErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
- return
-
- } else {
- sendSMSErr := service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
- if sendSMSErr != nil {
- }
-
- this.ServeSuccessJSON(nil)
- return
- }
- } else {
-
- total, _ := service.FindAdminUserByID(adminUser.Id, adminUserInfo.CurrentOrgId)
-
- if total <= 0 {
- //新增用户
- app_role := &models.App_Role{
- AdminUserId: adminUser.Id,
- OrgId: adminUserInfo.CurrentOrgId,
- AppId: adminUserInfo.CurrentAppId,
- Avatar: "",
- UserName: name,
- UserTitleName: user_title_name,
- Status: 1,
- UserType: int8(userType),
- UserTitle: int8(userTitle),
- CreateTime: time.Now().Unix(),
- ModifyTime: time.Now().Unix(),
- RoleIds: roleIds,
- }
- err := service.CreateUserRole(app_role)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
- return
- }
- this.ServeSuccessJSON(nil)
- } else {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateStaffException)
- return
- }
-
- return
- }
- }
-
- }
-
- // /api/admin/editinit [get]
- // @param uid:int
- func (this *RoleAPIController) EditAdminInitData() {
- adminUserInfo := this.GetAdminUserInfo()
-
- admin_user_id, _ := this.GetInt64("uid")
- if admin_user_id <= 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id)
- if getAppRoleErr != nil {
- //beego.Error("查询管理员信息时失败:", getAppRoleErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- if appRole == nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
- return
- }
-
- roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
- if getRoleErr != nil {
- //beego.Error("获取所有角色失败:", getRoleErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- var isSubSuperAdmin bool = false
- adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
- if len(adminUserRole.RoleIds) > 0 {
- role_ids := strings.Split(adminUserRole.RoleIds, ",")
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- if adminUserInfo.AdminUser.Id != org.Creator {
- for _, item := range role_ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- if id != 0 {
- role, _ := service.GetRoleByRoleID(id)
- if role.IsSystem == 1 && role.RoleName == "子管理员" {
- isSubSuperAdmin = true
- }
- }
- }
- }
- }
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
-
- redisClient := service.RedisClient()
- defer redisClient.Close()
- qntoken, _ := redisClient.Get("qn_token").Result()
-
- this.ServeSuccessJSON(map[string]interface{}{
- "admin": appRole,
- "roles": roles,
- "qntoken": qntoken,
- "isSubSuperAdmin": isSubSuperAdmin,
- "org": org,
- })
- }
-
- // /api/admin/edit [post]
- // @param uid:int
- // @param name:string
- // @param type:int
- // @param title:int
- // @param role:int
- // @param intro?:string
- func (this *RoleAPIController) 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")
- roleIds := this.GetString("role")
- intro := this.GetString("intro")
- user_title_name := this.GetString("user_title_name")
- sort, _ := this.GetInt64("sort")
- _, titleExist := models.UserTitle[userTitle]
- if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || len(roleIds) <= 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId)
- if getAppRoleErr != nil {
- //beego.Error("查询管理员信息时失败:", getAppRoleErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- if appRole == nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
- return
- }
-
- appRole.UserName = name
- appRole.UserType = int8(userType)
- appRole.UserTitle = int8(userTitle)
- appRole.RoleIds = roleIds
- appRole.Intro = intro
- appRole.UserTitleName = user_title_name
- appRole.ModifyTime = time.Now().Unix()
- appRole.Sort = sort
- saveErr := service.SaveAppRole(appRole)
- if saveErr != nil {
- //beego.Error("修改App_Role失败:", saveErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
-
- } else {
- this.ServeSuccessJSON(nil)
- }
- }
-
- // /api/admin/setstatus [post]
- // @param uid:int
- // @param enable:bool
- func (this *RoleAPIController) 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 := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID)
- if getAppRoleErr != nil {
- //beego.Error("查询管理员信息失败:", getAppRoleErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else if appRole == nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
- return
- }
-
- enable, _ := this.GetBool("enable")
-
- if enable {
- appRole.Status = 1
- } else {
- appRole.Status = 0
- }
- appRole.ModifyTime = time.Now().Unix()
- saveErr := service.SaveAppRole(appRole)
- if saveErr != nil {
- //beego.Error("保存AppRole失败:", saveErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
-
- } else {
- this.ServeSuccessJSON(nil)
- }
- }
-
- // /api/admin/specialpermission/initdata [get]
- func (this *RoleAPIController) SpecialPermissionInitData() {
- adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- adminUsers, getAdminUsersErr := service.GetAllGeneralAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
- if getAdminUsersErr != nil {
- this.ErrorLog("获取所有普通用户失败:%v", getAdminUsersErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- headNurses, getAllHeadNursesErr := service.GetAllValidAdminUsersWithSpecialPermission(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
- if getAllHeadNursesErr != nil {
- this.ErrorLog("获取所有拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- this.ServeSuccessJSON(map[string]interface{}{
- "users": adminUsers,
- "head_nurses": headNurses,
- })
- }
-
- // /api/admin/specialpermission/dialysisrecord/submit [post]
- // @param ids:string ("1,2,5")
- func (this *RoleAPIController) SubmitDialysisRecordPermission() {
- adminUserInfo := this.GetAdminUserInfo()
- //if adminUserInfo.AdminUser.IsSuperAdmin == false {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
- // return
- //}
-
- idsString := this.GetString("ids")
- if len(idsString) == 0 {
- // 取消所有用户的护士长权限
- cancelErr := service.CancelAllSpecialPermissionAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
- if cancelErr != nil {
- this.ErrorLog("取消所有用户的护士长权限失败:%v", cancelErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else {
- this.ServeSuccessJSON(nil)
- return
- }
-
- } else {
- ids := make([]int64, 0)
- idStrs := strings.Split(idsString, ",")
- for _, idStr := range idStrs {
- id, parseErr := strconv.Atoi(idStr)
- if parseErr != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- ids = append(ids, int64(id))
- }
-
- headNurses, getAllHeadNursesErr := service.GetAllSpecialPermissionAdminUsersWithoutStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
- if getAllHeadNursesErr != nil {
- this.ErrorLog("获取所有拥有或曾拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- cancelList := make([]*models.AdminUserSpecialPermission, 0)
- addList := make([]*models.AdminUserSpecialPermission, 0)
- for _, id := range ids {
- exit := false
- for _, headNurse := range headNurses {
- if headNurse.AdminUserID == id {
- exit = true
- if headNurse.Status != 1 {
- headNurse.Status = 1
- headNurse.ModifyTime = time.Now().Unix()
- addList = append(addList, headNurse)
- }
- break
- }
- }
- if exit == false {
- newHeadNurse := &models.AdminUserSpecialPermission{
- OrgID: adminUserInfo.CurrentOrgId,
- AppID: adminUserInfo.CurrentAppId,
- AdminUserID: id,
- Permission: int64(models.SpecialPermissionTypeHeadNurse),
- Status: 1,
- CreateTime: time.Now().Unix(),
- ModifyTime: time.Now().Unix(),
- }
- addList = append(addList, newHeadNurse)
- }
- }
-
- for _, headNurse := range headNurses {
- cancel := true
- for _, willAdd := range addList {
- if willAdd.AdminUserID == headNurse.AdminUserID {
- cancel = false
- break
- }
- }
- if cancel {
- headNurse.Status = 0
- headNurse.ModifyTime = time.Now().Unix()
- cancelList = append(cancelList, headNurse)
- }
- }
-
- addErr := service.BatchSaveSpecialPermissionAdminUsers(addList)
- if addErr != nil {
- this.ErrorLog("授权失败:%v", addErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- cancelErr := service.BatchSaveSpecialPermissionAdminUsers(cancelList)
- if cancelErr != nil {
- this.ErrorLog("取消授权失败:%v", cancelErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- this.ServeSuccessJSON(nil)
- }
-
- }
-
- func (this *RoleAPIController) GetAllOrgRole() {
- adminUserInfo := this.GetAdminUserInfo()
-
- var isSubSuperAdmin bool = false
- adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
-
- if len(adminUserRole.RoleIds) > 0 {
- //app_role, _ := service.GetAppRoleById(adminUserInfo.)
- role_ids := strings.Split(adminUserRole.RoleIds, ",")
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- if adminUserInfo.AdminUser.Id != org.Creator {
- for _, item := range role_ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- if id != 0 {
- role, _ := service.GetRoleByRoleID(id)
- if role != nil {
- if role.IsSystem == 1 && role.RoleName == "子管理员" {
- isSubSuperAdmin = true
- }
- }
- }
- }
- }
- }
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
-
- roles, err := service.GetAllOrgValidRoles(adminUserInfo.CurrentOrgId, isSubSuperAdmin)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else {
- this.ServeSuccessJSON(map[string]interface{}{
- "roles": roles,
- "isSubSuperAdmin": isSubSuperAdmin,
- "org": org,
- })
- }
-
- }
-
- func (this *RoleAPIController) GetAllOrgUser() {
- adminUserInfo := this.GetAdminUserInfo()
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- var isSubSuperAdmin bool = false
- adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
- if len(adminUserRole.RoleIds) > 0 {
- //app_role, _ := service.GetAppRoleById(adminUserInfo.)
- role_ids := strings.Split(adminUserRole.RoleIds, ",")
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- if adminUserInfo.AdminUser.Id != org.Creator {
- for _, item := range role_ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- if id != 0 {
- role, _ := service.GetRoleByRoleID(id)
- if role != nil {
- if role.IsSystem == 1 && role.RoleName == "子管理员" {
- isSubSuperAdmin = true
- }
- }
- }
- }
- }
- }
- viewModels, _, _ := service.GetAllAdminUsersAndRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
- this.ServeSuccessJSON(map[string]interface{}{
- "admins": viewModels,
- "org": org,
- "isSubSuperAdmin": isSubSuperAdmin,
- })
- }
-
- func (this *RoleAPIController) AddRoleStaff() {
- //adminUserInfo := this.GetMobileAdminUserInfo()
- role_id, _ := this.GetInt64("id", 0)
- staff_ids := this.GetString("ids")
- ids := strings.Split(staff_ids, ",")
- for _, item := range ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- role, _ := service.FindAdminUserID(id)
- role.RoleIds = role.RoleIds + "," + strconv.FormatInt(role_id, 10)
- service.SaveAdminUser(&role)
- }
- this.ServeSuccessJSON(map[string]interface{}{
- "msg": "添加成功",
- })
-
- }
-
- func (this *RoleAPIController) GetRoleStaff() {
- adminUserInfo := this.GetAdminUserInfo()
-
- var isSubSuperAdmin bool = false
- adminUserRole, _ := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
- if len(adminUserRole.RoleIds) > 0 {
- role_ids := strings.Split(adminUserRole.RoleIds, ",")
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- if adminUserInfo.AdminUser.Id != org.Creator {
- for _, item := range role_ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- if id != 0 {
- role, _ := service.GetRoleByRoleID(id)
- if role.IsSystem == 1 && role.RoleName == "子管理员" {
- isSubSuperAdmin = true
- }
- }
- }
- }
- }
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- viewModels, _, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 100)
- if getAdminsErr != nil {
- //beego.Error("获取管理员列表失败:", getAdminsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- this.ServeSuccessJSON(map[string]interface{}{
- "admins": viewModels,
- "org": org,
- "isSubSuperAdmin": isSubSuperAdmin,
- })
-
- }
-
- func (this *RoleAPIController) GetRoleInfo() {
- roleId, _ := this.GetInt64("role_id")
- if roleId <= 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- role, _ := service.GetRoleByRoleID(roleId)
- this.ServeSuccessJSON(map[string]interface{}{
- "role": role,
- })
-
- }
|