base_api_controller.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. //"XT_New/models"
  5. "XT_New/service"
  6. "strconv"
  7. "strings"
  8. )
  9. type BaseAPIController struct {
  10. BaseController
  11. }
  12. // func (this *BaseAPIController) Prepare() {
  13. // this.BaseController.Prepare()
  14. // beego.Trace("============================================================")
  15. // beego.Trace("session ID: %v", this.Ctx.Input.Cookie("beegosessionID"))
  16. // beego.Trace("session : %v", this.GetSession("info"))
  17. // this.SetSession("info", time.Now().Format("2006/01/02 15:04:05"))
  18. // beego.Trace("============================================================")
  19. // }
  20. // 输出数据格式化
  21. /*
  22. success json:
  23. {
  24. "state": 1,
  25. "code": 0,
  26. "data": json,
  27. }
  28. fail json:
  29. {
  30. "state": 0,
  31. "code": int,
  32. "msg": string,
  33. }
  34. */
  35. func (this *BaseAPIController) ServeSuccessJSON(data map[string]interface{}) {
  36. this.Data["json"] = enums.MakeSuccessResponseJSON(data)
  37. this.ServeJSON()
  38. }
  39. func (this *BaseAPIController) ServeFailJSONWithSGJErrorCode(code int) {
  40. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(code)
  41. this.ServeJSON()
  42. }
  43. func (this *BaseAPIController) ServeFailJSONWithSGJError(err *enums.SGJError) {
  44. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  45. this.ServeJSON()
  46. }
  47. func (this *BaseAPIController) ServeFailJsonSend(code int, msg string) {
  48. this.Data["json"] = enums.MakeFailResponseJSON(msg, code)
  49. this.ServeJSON()
  50. }
  51. type BaseAuthAPIController struct {
  52. BaseAPIController
  53. }
  54. func (this *BaseAuthAPIController) Prepare() {
  55. this.BaseAPIController.Prepare()
  56. if this.GetAdminUserInfo() == nil {
  57. //var userAdmin models.AdminUser
  58. //userAdmin.Id = 558
  59. //userAdmin.Mobile = "13318464649"
  60. //
  61. ////userAdmin.Id = 597
  62. ////userAdmin.Mobile = "19874122664"
  63. //userAdmin.IsSuperAdmin = false
  64. //userAdmin.Status = 1
  65. //userAdmin.CreateTime = 1530786071
  66. //userAdmin.ModifyTime = 1530786071
  67. //var subscibe models.ServeSubscibe
  68. //subscibe.ID = 1
  69. //subscibe.OrgId = 4
  70. //subscibe.PeriodStart = 1538035409
  71. //subscibe.PeriodEnd = 1569571409
  72. //subscibe.State = 1
  73. //subscibe.Status = 1
  74. //subscibe.CreatedTime = 1538035409
  75. //subscibe.UpdatedTime = 1538035409
  76. //subscibes := make(map[int64]*models.ServeSubscibe, 0)
  77. //subscibes[4] = &subscibe
  78. //var adminUserInfo service.AdminUserInfo
  79. //adminUserInfo.CurrentOrgId = 4
  80. //adminUserInfo.CurrentAppId = 18
  81. //adminUserInfo.AdminUser = &userAdmin
  82. //adminUserInfo.Subscibes = subscibes
  83. //this.SetSession("admin_user_info", &adminUserInfo)
  84. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  85. this.StopRun()
  86. }
  87. adminUserInfo := this.GetAdminUserInfo()
  88. if this.Ctx.Request.Header.Get("Permission") == "2" {
  89. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  90. if adminUserInfo.AdminUser.Id != org.Creator { //超级管理员不受此限制
  91. isPermission := false
  92. adminUserInfo := this.GetAdminUserInfo()
  93. //该机构下该用户有多少个
  94. role, _ := service.GetUserAllRole(adminUserInfo.CurrentOrgId, adminUserInfo.AdminUser.Id)
  95. var roles []string
  96. if len(role.RoleIds) <= 0 { //该用户没有设置角色
  97. } else {
  98. roles = strings.Split(role.RoleIds, ",")
  99. }
  100. //获取该用户下所有角色的权限总集
  101. var userRolePurviews string
  102. var userRolePurviewsArr []string
  103. for _, item := range roles {
  104. role_id, _ := strconv.ParseInt(item, 10, 64)
  105. purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  106. if len(userRolePurviews) == 0 {
  107. userRolePurviews = purviews
  108. } else {
  109. userRolePurviews = userRolePurviews + "," + purviews
  110. }
  111. }
  112. //该用户所拥有角色的权限的总集
  113. userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
  114. //系统所记录的权限列表
  115. allPermission, _ := service.GetAllFunctionPurview()
  116. for _, item := range allPermission {
  117. // fmt.Println(len(strings.Split(item.Urlfor, ",")))
  118. // fmt.Println(strings.Split(item.Urlfor, ","))
  119. //fmt.Println(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  120. //判断当前路由是否在权限路由列表里面
  121. if strings.Split(item.Urlfor, ",")[1] == strings.Split(this.Ctx.Request.RequestURI, "?")[0]+"?"+"mode="+this.GetString("mode") {
  122. //获取该角色的所有权限
  123. for _, items := range userRolePurviewsArr {
  124. id, _ := strconv.ParseInt(items, 10, 64)
  125. if id == item.ID {
  126. isPermission = true
  127. }
  128. }
  129. if !isPermission {
  130. msg, _ := service.FindErrorMsgByStr(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  131. json := make(map[string]interface{})
  132. json["msg"] = msg
  133. json["code"] = 0
  134. json["state"] = 0
  135. this.Data["json"] = json
  136. this.ServeJSON()
  137. this.StopRun()
  138. }
  139. }
  140. }
  141. }
  142. }
  143. if this.Ctx.Request.Header.Get("Permission") == "3" {
  144. org, _ := service.GetOrgById(adminUserInfo.CurrentOrgId)
  145. if adminUserInfo.AdminUser.Id != org.Creator { //超级管理员不受此限制
  146. isPermission := false
  147. adminUserInfo := this.GetAdminUserInfo()
  148. //该机构下该用户有多少个
  149. role, _ := service.GetUserAllRole(adminUserInfo.CurrentOrgId, adminUserInfo.AdminUser.Id)
  150. var roles []string
  151. if len(role.RoleIds) <= 0 { //该用户没有设置角色
  152. } else {
  153. roles = strings.Split(role.RoleIds, ",")
  154. }
  155. //获取该用户下所有角色的权限总集
  156. var userRolePurviews string
  157. var userRolePurviewsArr []string
  158. for _, item := range roles {
  159. role_id, _ := strconv.ParseInt(item, 10, 64)
  160. purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  161. if len(userRolePurviews) == 0 {
  162. userRolePurviews = purviews
  163. } else {
  164. userRolePurviews = userRolePurviews + "," + purviews
  165. }
  166. }
  167. //该用户所拥有角色的权限的总集
  168. userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
  169. //系统所记录的权限列表
  170. allPermission, _ := service.GetAllFunctionPurview()
  171. for _, item := range allPermission {
  172. // fmt.Println(strings.Split(item.Urlfor, ",")[2])
  173. // fmt.Println(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  174. //判断当前路由是否在权限路由列表里面
  175. if strings.Split(item.Urlfor, ",")[2] == strings.Split(this.Ctx.Request.RequestURI, "?")[0]+"?"+"mode="+this.GetString("mode") {
  176. //获取该角色的所有权限
  177. for _, items := range userRolePurviewsArr {
  178. id, _ := strconv.ParseInt(items, 10, 64)
  179. if id == item.ID {
  180. isPermission = true
  181. }
  182. }
  183. if !isPermission {
  184. msg, _ := service.FindErrorMsgByStr(strings.Split(this.Ctx.Request.RequestURI, "?")[0] + "?" + "mode=" + this.GetString("mode"))
  185. json := make(map[string]interface{})
  186. json["msg"] = msg
  187. json["code"] = 0
  188. json["state"] = 0
  189. this.Data["json"] = json
  190. this.ServeJSON()
  191. this.StopRun()
  192. }
  193. }
  194. }
  195. }
  196. }
  197. //if this.Ctx.Request.Method != "GET" {
  198. // adminUserInfo := this.GetAdminUserInfo()
  199. //
  200. // err := service.GetOrgSubscibeState(adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId])
  201. // if err != nil || adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId].State == 3 {
  202. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotSubscibe)
  203. // this.StopRun()
  204. // }
  205. //}
  206. //if this.Ctx.Request.Header.Get("Permission") == "1" {
  207. // isPermission := false
  208. //
  209. // //adminUserInfo := this.GetAdminUserInfo()
  210. //
  211. // //service.GetUserAllRole(adminUserInfo.CurrentOrgId)
  212. //
  213. //
  214. //
  215. //
  216. // roles := []int64{1,2} //模拟该用户有多少角色
  217. // var targetRole models.RolePurview
  218. // var userRolePurview []string
  219. //
  220. // //用户角色1对应的权限
  221. // role1 := models.RolePurview{PurviewIds:"11,12,13"}
  222. //
  223. // //用户角色2对应的权限
  224. // role2 := models.RolePurview{PurviewIds:"11,15,16"}
  225. //
  226. //
  227. // //模拟角色2
  228. // //判断该用户有多少个角色,合并最大角色权限
  229. // if len(roles) == 1{ //单个
  230. // targetRole = role1
  231. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  232. //
  233. // }else{ //多个
  234. // targetRole = role1
  235. // targetRole.PurviewIds = targetRole.PurviewIds + "," + role2.PurviewIds
  236. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  237. // }
  238. //
  239. // userRolePurview = RemoveRepeatedElement2(userRolePurview)
  240. //
  241. // fmt.Println(userRolePurview)
  242. // //所有权限列表
  243. // allPermission := []models.Purview{{Id:10,Urlfor:"/m/api/dialysis/dialysisPrescription-/api/dialysis/prescription"},
  244. // {Id:11,Urlfor:"/m/api/dialysis/dialysisPrescription1-/api/dialysis/prescription1"},
  245. // {Id:12,Urlfor:"/m/api/dialysis/dialysisPrescription2-/api/dialysis/prescription2"},
  246. // {Id:13,Urlfor:"/m/api/dialysis/dialysisPrescription3-/api/dialysis/prescription3"},
  247. // {Id:14,Urlfor:"/m/api/dialysis/dialysisPrescription4-/api/dialysis/prescription4"},
  248. // {Id:15,Urlfor:"/m/api/dialysis/dialysisPrescription5-/api/dialysis/prescription5"},
  249. // {Id:16,Urlfor:"/m/api/dialysis/dialysisPrescription6-/api/dialysis/prescription6"}}
  250. // for _, item := range allPermission {
  251. // //判断当前路由是否在权限路由列表里面
  252. // if strings.Split(item.Urlfor, "-")[1] == strings.Split(this.Ctx.Request.RequestURI , "?")[0]{
  253. // fmt.Println(strings.Split(this.Ctx.Request.RequestURI , "?")[0])
  254. //
  255. // //获取该角色的所有权限
  256. // for _, items := range userRolePurview{
  257. // id, _ := strconv.ParseInt(items, 10, 64)
  258. // if id == item.Id{
  259. // isPermission = true
  260. // }
  261. // }
  262. // if !isPermission{
  263. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePreExist)
  264. // this.StopRun()
  265. // }
  266. // }
  267. //
  268. // }
  269. //}
  270. }
  271. type BaseServeAPIController struct {
  272. BaseAPIController
  273. }
  274. func (this *BaseServeAPIController) Prepare() {
  275. this.BaseAPIController.Prepare()
  276. if this.GetAdminUserInfo() == nil {
  277. //var userAdmin models.AdminUser
  278. //userAdmin.Id = 558
  279. //userAdmin.Mobile = "13535547901"
  280. ////userAdmin.Id = 597
  281. ////userAdmin.Mobile = "19874122664"
  282. //userAdmin.IsSuperAdmin = false
  283. //userAdmin.Status = 1
  284. //userAdmin.CreateTime = 1530786071
  285. //userAdmin.ModifyTime = 1530786071
  286. //var subscibe models.ServeSubscibe
  287. //subscibe.ID = 1
  288. //subscibe.OrgId = 4
  289. //subscibe.PeriodStart = 1538035409
  290. //subscibe.PeriodEnd = 1569571409
  291. //subscibe.State = 1
  292. //subscibe.Status = 1
  293. //subscibe.CreatedTime = 1538035409
  294. //subscibe.UpdatedTime = 1538035409
  295. //subscibes := make(map[int64]*models.ServeSubscibe, 0)
  296. //subscibes[4] = &subscibe
  297. //var adminUserInfo service.AdminUserInfo
  298. //adminUserInfo.CurrentOrgId = 4
  299. //adminUserInfo.CurrentAppId = 18
  300. //adminUserInfo.AdminUser = &userAdmin
  301. //adminUserInfo.Subscibes = subscibes
  302. //this.SetSession("admin_user_info", &adminUserInfo)
  303. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  304. this.StopRun()
  305. }
  306. //if adminUserInfo.AppRole != nil {
  307. // if adminUserInfo.AppRole.Id > 0 {
  308. // app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
  309. // if app_role != nil {
  310. // if app_role.Status != 1 {
  311. // this.DelSession("mobile_admin_user_info")
  312. // this.Ctx.SetCookie("token_cookie", "")
  313. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
  314. // this.StopRun()
  315. // }
  316. // }
  317. // }
  318. //
  319. //}
  320. //fmt.Println("222222222")
  321. }
  322. func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
  323. newArr = make([]string, 0)
  324. for i := 0; i < len(arr); i++ {
  325. repeat := false
  326. for j := i + 1; j < len(arr); j++ {
  327. if arr[i] == arr[j] {
  328. repeat = true
  329. break
  330. }
  331. }
  332. if !repeat {
  333. newArr = append(newArr, arr[i])
  334. }
  335. }
  336. return
  337. }