base_api_controller.go 6.3KB

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