base_api_controller.go 11KB

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