base_api_controller.go 6.4KB

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