base_api_controller.go 12KB

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