123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- package controllers
-
- import (
- "XT_New/enums"
- "XT_New/service"
- "fmt"
- "strconv"
- "strings"
- )
-
- type BaseAPIController struct {
- BaseController
- }
-
- // func (this *BaseAPIController) Prepare() {
- // this.BaseController.Prepare()
- // beego.Trace("============================================================")
- // beego.Trace("session ID: %v", this.Ctx.Input.Cookie("beegosessionID"))
- // beego.Trace("session : %v", this.GetSession("info"))
- // this.SetSession("info", time.Now().Format("2006/01/02 15:04:05"))
- // beego.Trace("============================================================")
- // }
-
- // 输出数据格式化
- /*
- success json:
- {
- "state": 1,
- "code": 0,
- "data": json,
- }
-
- fail json:
- {
- "state": 0,
- "code": int,
- "msg": string,
- }
- */
- func (this *BaseAPIController) ServeSuccessJSON(data map[string]interface{}) {
- this.Data["json"] = enums.MakeSuccessResponseJSON(data)
- this.ServeJSON()
- }
-
- func (this *BaseAPIController) ServeFailJSONWithSGJErrorCode(code int) {
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(code)
- this.ServeJSON()
- }
-
- func (this *BaseAPIController) ServeFailJSONWithSGJError(err *enums.SGJError) {
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
- this.ServeJSON()
- }
-
- func (this *BaseAPIController) ServeFailJsonSend(code int, msg string) {
- this.Data["json"] = enums.MakeFailResponseJSON(msg, code)
- this.ServeJSON()
- }
-
- func (this *BaseAPIController) ServeDynamicFailJsonSend(msg string) {
- this.Data["json"] = enums.MakeDynamicFailResponseJSON(msg)
- this.ServeJSON()
- }
-
- type BaseAuthAPIController struct {
- BaseAPIController
- }
-
- func (this *BaseAuthAPIController) Prepare() {
- this.BaseAPIController.Prepare()
- if this.GetAdminUserInfo() == nil {
- //var userAdmin models.AdminUser
- //userAdmin.Id = 1448
- //userAdmin.Mobile = "13318599895"
- //
- //userAdmin.Id = 597
- //userAdmin.Mobile = "19874122664"
- //userAdmin.IsSuperAdmin = false
- //userAdmin.Status = 1
- //userAdmin.CreateTime = 1530786071
- //userAdmin.ModifyTime = 1530786071
- //var subscibe models.ServeSubscibe
- //subscibe.ID = 1
- //subscibe.OrgId = 3877
- //subscibe.PeriodStart = 1538035409
- //subscibe.PeriodEnd = 1569571409
- //subscibe.State = 1
- //subscibe.Status = 1
- //subscibe.CreatedTime = 1538035409
- //subscibe.UpdatedTime = 1538035409
- //subscibes := make(map[int64]*models.ServeSubscibe, 0)
- //subscibes[4] = &subscibe
- //var adminUserInfo service.AdminUserInfo
- //adminUserInfo.CurrentOrgId = 3877
- //adminUserInfo.CurrentAppId = 4
- //adminUserInfo.AdminUser = &userAdmin
- //adminUserInfo.Subscibes = subscibes
- //this.SetSession("admin_user_info", &adminUserInfo)
-
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
- this.StopRun()
-
- }
-
- adminUserInfo := this.GetAdminUserInfo()
-
- if this.Ctx.Request.Header.Get("Permission") == "2" {
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- if adminUserInfo.AdminUser.Id != org.Creator { //超级管理员不受此限制
-
- isPermission := false
- adminUserInfo := this.GetAdminUserInfo()
- //该机构下该用户有多少个
- role, _ := service.GetUserAllRole(adminUserInfo.CurrentOrgId, adminUserInfo.AdminUser.Id)
- var roles []string
- if len(role.RoleIds) <= 0 { //该用户没有设置角色
-
- } else {
- roles = strings.Split(role.RoleIds, ",")
- }
- fmt.Println(roles)
-
- //获取该用户下所有角色的权限总集
- var userRolePurviews string
- var userRolePurviewsArr []string
- for _, item := range roles {
- role_id, _ := strconv.ParseInt(item, 10, 64)
- purviews, _ := service.GetRoleFuncPurviewIds(role_id)
- if len(userRolePurviews) == 0 {
- userRolePurviews = purviews
- } else {
- userRolePurviews = userRolePurviews + "," + purviews
- }
- }
- //该用户所拥有角色的权限的总集
- userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
- fmt.Println(userRolePurviewsArr)
- //系统所记录的权限列表
- allPermission, _ := service.GetAllFunctionPurview()
-
- for _, item := range allPermission {
- // fmt.Println(len(strings.Split(item.Urlfor, ",")))
-
- // fmt.Println(strings.Split(item.Urlfor, ","))
- //fmt.Println(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
-
- //判断当前路由是否在权限路由列表里面
- if strings.Split(item.Urlfor, ",")[1] == strings.Split(this.Ctx.Request.RequestURI, "?")[0]+"?"+"mode="+this.GetString("mode") {
-
- //获取该角色的所有权限
- for _, items := range userRolePurviewsArr {
- id, _ := strconv.ParseInt(items, 10, 64)
- if id == item.ID {
- isPermission = true
- }
- }
- if !isPermission {
- msg, _ := service.FindErrorMsgByStr(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
- json := make(map[string]interface{})
- json["msg"] = msg
- json["code"] = 0
- json["state"] = 0
- this.Data["json"] = json
- this.ServeJSON()
- this.StopRun()
- }
- }
- }
- }
- }
-
- if this.Ctx.Request.Header.Get("Permission") == "3" {
- org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
- if adminUserInfo.AdminUser.Id != org.Creator { //超级管理员不受此限制
-
- isPermission := false
- adminUserInfo := this.GetAdminUserInfo()
- //该机构下该用户有多少个
- role, _ := service.GetUserAllRole(adminUserInfo.CurrentOrgId, adminUserInfo.AdminUser.Id)
- var roles []string
- if len(role.RoleIds) <= 0 { //该用户没有设置角色
-
- } else {
- roles = strings.Split(role.RoleIds, ",")
- }
- fmt.Println(roles)
-
- //获取该用户下所有角色的权限总集
- var userRolePurviews string
- var userRolePurviewsArr []string
- for _, item := range roles {
- role_id, _ := strconv.ParseInt(item, 10, 64)
- purviews, _ := service.GetRoleFuncPurviewIds(role_id)
- if len(userRolePurviews) == 0 {
- userRolePurviews = purviews
- } else {
- userRolePurviews = userRolePurviews + "," + purviews
- }
- }
- //该用户所拥有角色的权限的总集
- userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
- fmt.Println(userRolePurviewsArr)
- //系统所记录的权限列表
- allPermission, _ := service.GetAllFunctionPurview()
-
- for _, item := range allPermission {
- // fmt.Println(strings.Split(item.Urlfor, ",")[2])
- // fmt.Println(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
-
- //判断当前路由是否在权限路由列表里面
- if strings.Split(item.Urlfor, ",")[2] == strings.Split(this.Ctx.Request.RequestURI, "?")[0]+"?"+"mode="+this.GetString("mode") {
-
- //获取该角色的所有权限
- for _, items := range userRolePurviewsArr {
- id, _ := strconv.ParseInt(items, 10, 64)
- if id == item.ID {
- isPermission = true
- }
- }
- if !isPermission {
- msg, _ := service.FindErrorMsgByStr(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
- json := make(map[string]interface{})
- json["msg"] = msg
- json["code"] = 0
- json["state"] = 0
- this.Data["json"] = json
- this.ServeJSON()
- this.StopRun()
- }
- }
- }
- }
- }
-
- //if this.Ctx.Request.Method != "GET" {
- // adminUserInfo := this.GetAdminUserInfo()
- //
- // err := service.GetOrgSubscibeState(adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId])
- // if err != nil || adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId].State == 3 {
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotSubscibe)
- // this.StopRun()
- // }
- //}
-
- //if this.Ctx.Request.Header.Get("Permission") == "1" {
- // isPermission := false
- //
- // //adminUserInfo := this.GetAdminUserInfo()
- //
- // //service.GetUserAllRole(adminUserInfo.CurrentOrgId)
- //
- //
- //
- //
- // roles := []int64{1,2} //模拟该用户有多少角色
- // var targetRole models.RolePurview
- // var userRolePurview []string
- //
- // //用户角色1对应的权限
- // role1 := models.RolePurview{PurviewIds:"11,12,13"}
- //
- // //用户角色2对应的权限
- // role2 := models.RolePurview{PurviewIds:"11,15,16"}
- //
- //
- // //模拟角色2
- // //判断该用户有多少个角色,合并最大角色权限
- // if len(roles) == 1{ //单个
- // targetRole = role1
- // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
- //
- // }else{ //多个
- // targetRole = role1
- // targetRole.PurviewIds = targetRole.PurviewIds + "," + role2.PurviewIds
- // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
- // }
- //
- // userRolePurview = RemoveRepeatedElement2(userRolePurview)
- //
- // fmt.Println(userRolePurview)
- // //所有权限列表
- // allPermission := []models.Purview{{Id:10,Urlfor:"/m/api/dialysis/dialysisPrescription-/api/dialysis/prescription"},
- // {Id:11,Urlfor:"/m/api/dialysis/dialysisPrescription1-/api/dialysis/prescription1"},
- // {Id:12,Urlfor:"/m/api/dialysis/dialysisPrescription2-/api/dialysis/prescription2"},
- // {Id:13,Urlfor:"/m/api/dialysis/dialysisPrescription3-/api/dialysis/prescription3"},
- // {Id:14,Urlfor:"/m/api/dialysis/dialysisPrescription4-/api/dialysis/prescription4"},
- // {Id:15,Urlfor:"/m/api/dialysis/dialysisPrescription5-/api/dialysis/prescription5"},
- // {Id:16,Urlfor:"/m/api/dialysis/dialysisPrescription6-/api/dialysis/prescription6"}}
- // for _, item := range allPermission {
- // //判断当前路由是否在权限路由列表里面
- // if strings.Split(item.Urlfor, "-")[1] == strings.Split(this.Ctx.Request.RequestURI , "?")[0]{
- // fmt.Println(strings.Split(this.Ctx.Request.RequestURI , "?")[0])
- //
- // //获取该角色的所有权限
- // for _, items := range userRolePurview{
- // id, _ := strconv.ParseInt(items, 10, 64)
- // if id == item.Id{
- // isPermission = true
- // }
- // }
- // if !isPermission{
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePreExist)
- // this.StopRun()
- // }
- // }
- //
- // }
- //}
- }
-
- type BaseServeAPIController struct {
- BaseAPIController
- }
-
- func (this *BaseServeAPIController) Prepare() {
- this.BaseAPIController.Prepare()
- if this.GetAdminUserInfo() == nil {
- //var userAdmin models.AdminUser
- //userAdmin.Id = 1448
- //userAdmin.Mobile = "13318599895"
- //
- //userAdmin.Id = 597
- //userAdmin.Mobile = "19874122664"
- //userAdmin.IsSuperAdmin = false
- //userAdmin.Status = 1
- //userAdmin.CreateTime = 1530786071
- //userAdmin.ModifyTime = 1530786071
- //var subscibe models.ServeSubscibe
- //subscibe.ID = 1
- //subscibe.OrgId = 3877
- //subscibe.PeriodStart = 1538035409
- //subscibe.PeriodEnd = 1569571409
- //subscibe.State = 1
- //subscibe.Status = 1
- //subscibe.CreatedTime = 1538035409
- //subscibe.UpdatedTime = 1538035409
- //subscibes := make(map[int64]*models.ServeSubscibe, 0)
- //subscibes[4] = &subscibe
- //var adminUserInfo service.AdminUserInfo
- //adminUserInfo.CurrentOrgId = 3877
- //adminUserInfo.CurrentAppId = 4
- //adminUserInfo.AdminUser = &userAdmin
- //adminUserInfo.Subscibes = subscibes
- //this.SetSession("admin_user_info", &adminUserInfo)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
- this.StopRun()
- }
-
- //if adminUserInfo.AppRole != nil {
- // if adminUserInfo.AppRole.Id > 0 {
- // app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
- // if app_role != nil {
- // if app_role.Status != 1 {
- // this.DelSession("mobile_admin_user_info")
- // this.Ctx.SetCookie("token_cookie", "")
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
- // this.StopRun()
- // }
- // }
- // }
- //
- //}
- //fmt.Println("222222222")
-
- }
-
- func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
- newArr = make([]string, 0)
- for i := 0; i < len(arr); i++ {
- repeat := false
- for j := i + 1; j < len(arr); j++ {
- if arr[i] == arr[j] {
- repeat = true
- break
- }
- }
- if !repeat {
- newArr = append(newArr, arr[i])
- }
- }
- return
- }
|