base_api_controller.go 11KB

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