mobile_api_base_controller.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package mobile_api_controllers
  2. import (
  3. "XT_New/controllers"
  4. "XT_New/enums"
  5. "XT_New/models"
  6. "XT_New/service"
  7. "encoding/json"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type MobileBaseAPIController struct {
  14. controllers.BaseAPIController
  15. }
  16. func (this *MobileBaseAPIController) Prepare() {
  17. this.BaseAPIController.Prepare()
  18. // beego.Trace("============================================================")
  19. // beego.Trace("session ID: %v", this.Ctx.Input.Cookie("beegosessionID"))
  20. // beego.Trace("session : %v", this.GetSession("info"))
  21. // this.SetSession("info", time.Now().Format("2006/01/02 15:04:05"))
  22. // beego.Trace("============================================================")
  23. }
  24. //
  25. func (this *MobileBaseAPIController) GetMobileAdminUserInfo() *MobileAdminUserInfo {
  26. userInfo := this.GetSession("mobile_admin_user_info")
  27. if userInfo == nil {
  28. return nil
  29. } else {
  30. return userInfo.(*MobileAdminUserInfo)
  31. }
  32. }
  33. type MobileAdminUserInfo struct {
  34. AdminUser *models.AdminUser
  35. Org *models.Org
  36. App *models.OrgApp
  37. AppRole *models.App_Role
  38. Subscibe *models.ServeSubscibe
  39. TemplateInfo *models.GobalTemplate
  40. }
  41. type MobileBaseAPIAuthController struct {
  42. MobileBaseAPIController
  43. }
  44. func (this *MobileBaseAPIAuthController) Prepare() {
  45. token := this.Ctx.GetCookie("token_cookie")
  46. logout_cookie := this.Ctx.GetCookie("logout_cookie")
  47. //if len(token) == 0{
  48. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  49. // this.StopRun()
  50. //}
  51. this.MobileBaseAPIController.Prepare()
  52. adminUserInfo := this.GetMobileAdminUserInfo()
  53. if len(token) == 0 {
  54. this.DelSession("mobile_admin_user_info")
  55. // fmt.Println(logout_cookie)
  56. if len(logout_cookie) == 1 {
  57. this.Ctx.SetCookie("logout_cookie", "2")
  58. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeLogOut)
  59. this.StopRun()
  60. } else {
  61. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  62. this.StopRun()
  63. }
  64. } else {
  65. if adminUserInfo == nil {
  66. this.DelSession("mobile_admin_user_info")
  67. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  68. this.StopRun()
  69. }
  70. }
  71. //if this.Ctx.Request.Method != "GET" {
  72. // err := service.GetOrgSubscibeState(adminUserInfo.Subscibe)
  73. // if err != nil || adminUserInfo.Subscibe.State == 3 {
  74. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotSubscibe)
  75. // this.StopRun()
  76. // }
  77. //}
  78. if adminUserInfo.AppRole != nil {
  79. if adminUserInfo.AppRole.Id > 0 {
  80. app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
  81. if app_role != nil {
  82. if app_role.Status != 1 {
  83. this.DelSession("mobile_admin_user_info")
  84. this.Ctx.SetCookie("token_cookie", "")
  85. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
  86. this.StopRun()
  87. }
  88. }
  89. }
  90. }
  91. if this.Ctx.Request.Header.Get("Permission") == "1" {
  92. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator { //超级管理员不受此限制
  93. isPermission := false
  94. adminUserInfo := this.GetMobileAdminUserInfo()
  95. //该机构下该用户有多少个
  96. role, _ := service.GetUserAllRole(adminUserInfo.Org.Id, adminUserInfo.AdminUser.Id)
  97. var roles []string
  98. if len(role.RoleIds) <= 0 { //该用户没有设置角色
  99. } else {
  100. roles = strings.Split(role.RoleIds, ",")
  101. }
  102. //redis相关处理逻辑
  103. redis := service.RedisClient()
  104. defer redis.Close()
  105. //redis key值
  106. key := "purviews_" + strconv.FormatInt(adminUserInfo.Org.Id, 10) + strconv.FormatInt(adminUserInfo.AdminUser.Id, 10)
  107. purviews_json_str, _ := redis.Get(key).Result()
  108. //获取该用户下所有角色的权限总集
  109. var userRolePurviews string
  110. var userRolePurviewsArr []string
  111. fmt.Println("----redis 开始----")
  112. fmt.Println(purviews_json_str)
  113. fmt.Println("----redis 结束----")
  114. if len(purviews_json_str) == 0 {
  115. for _, item := range roles {
  116. role_id, _ := strconv.ParseInt(item, 10, 64)
  117. purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  118. if len(userRolePurviews) == 0 {
  119. userRolePurviews = purviews
  120. } else {
  121. userRolePurviews = userRolePurviews + "," + purviews
  122. }
  123. }
  124. //该用户所拥有角色的权限的总集
  125. userRolePurviewsArr = RemoveRepeatedPurviewElement(strings.Split(userRolePurviews, ","))
  126. //缓存数据
  127. purview_json, err := json.Marshal(userRolePurviewsArr)
  128. if err == nil {
  129. redis.Set(key, purview_json, time.Minute*60*60*24)
  130. }
  131. } else {
  132. var dat []string
  133. if err := json.Unmarshal([]byte(purviews_json_str), &dat); err == nil {
  134. } else {
  135. }
  136. userRolePurviewsArr = dat
  137. }
  138. // fmt.Println(userRolePurviewsArr)
  139. //系统所记录的权限列表
  140. allPermission, _ := service.GetAllFunctionPurview()
  141. for _, item := range allPermission {
  142. //判断当前路由是否在权限路由列表里面
  143. if strings.Split(item.Urlfor, ",")[0] == strings.Split(this.Ctx.Request.RequestURI, "?")[0]+"?"+"mode="+this.GetString("mode") {
  144. //获取该角色的所有权限
  145. for _, items := range userRolePurviewsArr {
  146. id, _ := strconv.ParseInt(items, 10, 64)
  147. if id == item.ID {
  148. isPermission = true
  149. }
  150. }
  151. if !isPermission {
  152. msg, _ := service.FindErrorMsgByStr(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  153. json := make(map[string]interface{})
  154. json["msg"] = msg
  155. json["code"] = 0
  156. json["state"] = 0
  157. this.Data["json"] = json
  158. this.ServeJSON()
  159. this.StopRun()
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. func RemoveRepeatedPurviewElement(arr []string) (newArr []string) {
  167. newArr = make([]string, 0)
  168. for i := 0; i < len(arr); i++ {
  169. repeat := false
  170. for j := i + 1; j < len(arr); j++ {
  171. if arr[i] == arr[j] {
  172. repeat = true
  173. break
  174. }
  175. }
  176. if !repeat {
  177. newArr = append(newArr, arr[i])
  178. }
  179. }
  180. return
  181. }
  182. type AdminUserInfo struct {
  183. AdminUser *models.AdminUser `json:"user"`
  184. CurrentOrgId int64 `json:"current_org_id"`
  185. CurrentAppId int64 `json:"current_app_id"`
  186. OrgIds []int64 `json:"org_ids"`
  187. Orgs map[int64]*models.Org `json:"orgs"`
  188. OrgAppIds map[int64][]int64 `json:"org_app_ids"`
  189. OrgApps map[int64](map[int64]*models.OrgApp) `json:"org_apps"`
  190. App2OrgIds map[int64]int64 `json:"app_to_org_ids"`
  191. AppRoles map[int64]*models.App_Role `json:"app_roles"`
  192. AppPurviews map[int64][]*models.Purview `json:"app_purviews"`
  193. AppUrlfors map[int64][]string `json:"app_urlfors"`
  194. Subscibes map[int64]*models.ServeSubscibe `json:"org_subscibes"`
  195. }