base_api_controller.go 12KB

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