base_api_controller.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. )
  5. type BaseAPIController struct {
  6. BaseController
  7. }
  8. // func (this *BaseAPIController) Prepare() {
  9. // this.BaseController.Prepare()
  10. // beego.Trace("============================================================")
  11. // beego.Trace("session ID: %v", this.Ctx.Input.Cookie("beegosessionID"))
  12. // beego.Trace("session : %v", this.GetSession("info"))
  13. // this.SetSession("info", time.Now().Format("2006/01/02 15:04:05"))
  14. // beego.Trace("============================================================")
  15. // }
  16. // 输出数据格式化
  17. /*
  18. success json:
  19. {
  20. "state": 1,
  21. "code": 0,
  22. "data": json,
  23. }
  24. fail json:
  25. {
  26. "state": 0,
  27. "code": int,
  28. "msg": string,
  29. }
  30. */
  31. func (this *BaseAPIController) ServeSuccessJSON(data map[string]interface{}) {
  32. this.Data["json"] = enums.MakeSuccessResponseJSON(data)
  33. this.ServeJSON()
  34. }
  35. func (this *BaseAPIController) ServeFailJSONWithSGJErrorCode(code int) {
  36. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(code)
  37. this.ServeJSON()
  38. }
  39. func (this *BaseAPIController) ServeFailJSONWithSGJError(err *enums.SGJError) {
  40. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  41. this.ServeJSON()
  42. }
  43. func (this *BaseAPIController) ServeFailJsonSend(code int, msg string) {
  44. this.Data["json"] = enums.MakeFailResponseJSON(msg, code)
  45. this.ServeJSON()
  46. }
  47. type BaseAuthAPIController struct {
  48. BaseAPIController
  49. }
  50. func (this *BaseAuthAPIController) Prepare() {
  51. this.BaseAPIController.Prepare()
  52. if this.GetAdminUserInfo() == nil {
  53. //var userAdmin models.AdminUser
  54. //userAdmin.Id = 380
  55. //userAdmin.Mobile = "13532250447"
  56. //userAdmin.IsSuperAdmin = true
  57. //userAdmin.Status = 1
  58. //userAdmin.CreateTime = 1530786071
  59. //userAdmin.ModifyTime = 1530786071
  60. //var subscibe models.ServeSubscibe
  61. //subscibe.ID = 1
  62. //subscibe.OrgId = 4
  63. //subscibe.PeriodStart = 1538035409
  64. //subscibe.PeriodEnd = 1569571409
  65. //subscibe.State = 1
  66. //subscibe.Status = 1
  67. //subscibe.CreatedTime = 1538035409
  68. //subscibe.UpdatedTime = 1538035409
  69. //subscibes := make(map[int64]*models.ServeSubscibe, 0)
  70. //subscibes[4] = &subscibe
  71. //
  72. //var template models.GobalTemplate
  73. //template.TemplateId = 2
  74. //
  75. //var adminUserInfo service.AdminUserInfo
  76. //adminUserInfo.CurrentOrgId = 4
  77. //adminUserInfo.CurrentAppId = 5
  78. //adminUserInfo.AdminUser = &userAdmin
  79. //adminUserInfo.Subscibes = subscibes
  80. //this.SetSession("admin_user_info", &adminUserInfo)
  81. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  82. this.StopRun()
  83. }
  84. //if this.Ctx.Request.Method != "GET" {
  85. // adminUserInfo := this.GetAdminUserInfo()
  86. //
  87. // err := service.GetOrgSubscibeState(adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId])
  88. // if err != nil || adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId].State == 3 {
  89. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotSubscibe)
  90. // this.StopRun()
  91. // }
  92. //}
  93. //if this.Ctx.Request.Header.Get("Permission") == "1" {
  94. // isPermission := false
  95. //
  96. // //adminUserInfo := this.GetAdminUserInfo()
  97. //
  98. // //service.GetUserAllRole(adminUserInfo.CurrentOrgId)
  99. //
  100. //
  101. //
  102. //
  103. // roles := []int64{1,2} //模拟该用户有多少角色
  104. // var targetRole models.RolePurview
  105. // var userRolePurview []string
  106. //
  107. // //用户角色1对应的权限
  108. // role1 := models.RolePurview{PurviewIds:"11,12,13"}
  109. //
  110. // //用户角色2对应的权限
  111. // role2 := models.RolePurview{PurviewIds:"11,15,16"}
  112. //
  113. //
  114. // //模拟角色2
  115. // //判断该用户有多少个角色,合并最大角色权限
  116. // if len(roles) == 1{ //单个
  117. // targetRole = role1
  118. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  119. //
  120. // }else{ //多个
  121. // targetRole = role1
  122. // targetRole.PurviewIds = targetRole.PurviewIds + "," + role2.PurviewIds
  123. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  124. // }
  125. //
  126. // userRolePurview = RemoveRepeatedElement2(userRolePurview)
  127. //
  128. // fmt.Println(userRolePurview)
  129. // //所有权限列表
  130. // allPermission := []models.Purview{{Id:10,Urlfor:"/m/api/dialysis/dialysisPrescription-/api/dialysis/prescription"},
  131. // {Id:11,Urlfor:"/m/api/dialysis/dialysisPrescription1-/api/dialysis/prescription1"},
  132. // {Id:12,Urlfor:"/m/api/dialysis/dialysisPrescription2-/api/dialysis/prescription2"},
  133. // {Id:13,Urlfor:"/m/api/dialysis/dialysisPrescription3-/api/dialysis/prescription3"},
  134. // {Id:14,Urlfor:"/m/api/dialysis/dialysisPrescription4-/api/dialysis/prescription4"},
  135. // {Id:15,Urlfor:"/m/api/dialysis/dialysisPrescription5-/api/dialysis/prescription5"},
  136. // {Id:16,Urlfor:"/m/api/dialysis/dialysisPrescription6-/api/dialysis/prescription6"}}
  137. // for _, item := range allPermission {
  138. // //判断当前路由是否在权限路由列表里面
  139. // if strings.Split(item.Urlfor, "-")[1] == strings.Split(this.Ctx.Request.RequestURI , "?")[0]{
  140. // fmt.Println(strings.Split(this.Ctx.Request.RequestURI , "?")[0])
  141. //
  142. // //获取该角色的所有权限
  143. // for _, items := range userRolePurview{
  144. // id, _ := strconv.ParseInt(items, 10, 64)
  145. // if id == item.Id{
  146. // isPermission = true
  147. // }
  148. // }
  149. // if !isPermission{
  150. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePreExist)
  151. // this.StopRun()
  152. // }
  153. // }
  154. //
  155. // }
  156. //}
  157. }
  158. type BaseServeAPIController struct {
  159. BaseAPIController
  160. }
  161. func (this *BaseServeAPIController) Prepare() {
  162. this.BaseAPIController.Prepare()
  163. if this.GetAdminUserInfo() == nil {
  164. //var userAdmin models.AdminUser
  165. //userAdmin.Id = 380
  166. //userAdmin.Mobile = "13532250447"
  167. //userAdmin.IsSuperAdmin = true
  168. //userAdmin.Status = 1
  169. //userAdmin.CreateTime = 1530786071
  170. //userAdmin.ModifyTime = 1530786071
  171. //var subscibe models.ServeSubscibe
  172. //subscibe.ID = 1
  173. //subscibe.OrgId = 4
  174. //subscibe.PeriodStart = 1538035409
  175. //subscibe.PeriodEnd = 1569571409
  176. //subscibe.State = 1
  177. //subscibe.Status = 1
  178. //subscibe.CreatedTime = 1538035409
  179. //subscibe.UpdatedTime = 1538035409
  180. //subscibes := make(map[int64]*models.ServeSubscibe, 0)
  181. //subscibes[4] = &subscibe
  182. //var adminUserInfo service.AdminUserInfo
  183. //adminUserInfo.CurrentOrgId = 4
  184. //adminUserInfo.CurrentAppId = 5
  185. //adminUserInfo.AdminUser = &userAdmin
  186. //adminUserInfo.Subscibes = subscibes
  187. //this.SetSession("admin_user_info", &adminUserInfo)
  188. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  189. this.StopRun()
  190. }
  191. }
  192. func RemoveRepeatedElement2(arr []string) (newArr []string) {
  193. newArr = make([]string, 0)
  194. for i := 0; i < len(arr); i++ {
  195. repeat := false
  196. for j := i + 1; j < len(arr); j++ {
  197. if arr[i] == arr[j] {
  198. repeat = true
  199. break
  200. }
  201. }
  202. if !repeat {
  203. newArr = append(newArr, arr[i])
  204. }
  205. }
  206. return
  207. }