base_api_controller.go 12KB

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