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. //
  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. //
  95. // isPermission := false
  96. // roles := []int64{1,2} //模拟该用户有多少角色
  97. // var targetRole models.RolePurview
  98. // var userRolePurview []string
  99. //
  100. // //用户角色1对应的权限
  101. // role1 := models.RolePurview{PurviewIds:"11,12,13"}
  102. //
  103. // //用户角色2对应的权限
  104. // role2 := models.RolePurview{PurviewIds:"11,15,16"}
  105. //
  106. //
  107. // //模拟角色2
  108. // //判断该用户有多少个角色,合并最大角色权限
  109. // if len(roles) == 1{ //单个
  110. // targetRole = role1
  111. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  112. //
  113. // }else{ //多个
  114. // targetRole = role1
  115. // targetRole.PurviewIds = targetRole.PurviewIds + "," + role2.PurviewIds
  116. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  117. // }
  118. //
  119. // userRolePurview = RemoveRepeatedElement2(userRolePurview)
  120. //
  121. // fmt.Println(userRolePurview)
  122. // //所有权限列表
  123. // allPermission := []models.Purview{{Id:10,Urlfor:"/m/api/dialysis/dialysisPrescription-/api/dialysis/prescription"},
  124. // {Id:11,Urlfor:"/m/api/dialysis/dialysisPrescription1-/api/dialysis/prescription1"},
  125. // {Id:12,Urlfor:"/m/api/dialysis/dialysisPrescription2-/api/dialysis/prescription2"},
  126. // {Id:13,Urlfor:"/m/api/dialysis/dialysisPrescription3-/api/dialysis/prescription3"},
  127. // {Id:14,Urlfor:"/m/api/dialysis/dialysisPrescription4-/api/dialysis/prescription4"},
  128. // {Id:15,Urlfor:"/m/api/dialysis/dialysisPrescription5-/api/dialysis/prescription5"},
  129. // {Id:16,Urlfor:"/m/api/dialysis/dialysisPrescription6-/api/dialysis/prescription6"}}
  130. // for _, item := range allPermission {
  131. // //判断当前路由是否在权限路由列表里面
  132. // if strings.Split(item.Urlfor, "-")[1] == strings.Split(this.Ctx.Request.RequestURI , "?")[0]{
  133. // fmt.Println(strings.Split(this.Ctx.Request.RequestURI , "?")[0])
  134. //
  135. // //获取该角色的所有权限
  136. // for _, items := range userRolePurview{
  137. // id, _ := strconv.ParseInt(items, 10, 64)
  138. // if id == item.Id{
  139. // isPermission = true
  140. // }
  141. // }
  142. // if !isPermission{
  143. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePreExist)
  144. // this.StopRun()
  145. // }
  146. // }
  147. //
  148. // }
  149. //}
  150. }
  151. type BaseServeAPIController struct {
  152. BaseAPIController
  153. }
  154. func (this *BaseServeAPIController) Prepare() {
  155. this.BaseAPIController.Prepare()
  156. if this.GetAdminUserInfo() == nil {
  157. var userAdmin models.AdminUser
  158. userAdmin.Id = 380
  159. userAdmin.Mobile = "13532250447"
  160. userAdmin.IsSuperAdmin = true
  161. userAdmin.Status = 1
  162. userAdmin.CreateTime = 1530786071
  163. userAdmin.ModifyTime = 1530786071
  164. var subscibe models.ServeSubscibe
  165. subscibe.ID = 1
  166. subscibe.OrgId = 4
  167. subscibe.PeriodStart = 1538035409
  168. subscibe.PeriodEnd = 1569571409
  169. subscibe.State = 1
  170. subscibe.Status = 1
  171. subscibe.CreatedTime = 1538035409
  172. subscibe.UpdatedTime = 1538035409
  173. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  174. subscibes[4] = &subscibe
  175. var adminUserInfo service.AdminUserInfo
  176. adminUserInfo.CurrentOrgId = 4
  177. adminUserInfo.CurrentAppId = 5
  178. adminUserInfo.AdminUser = &userAdmin
  179. adminUserInfo.Subscibes = subscibes
  180. this.SetSession("admin_user_info", &adminUserInfo)
  181. //this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  182. //this.StopRun()
  183. }
  184. }
  185. func RemoveRepeatedElement2(arr []string) (newArr []string) {
  186. newArr = make([]string, 0)
  187. for i := 0; i < len(arr); i++ {
  188. repeat := false
  189. for j := i + 1; j < len(arr); j++ {
  190. if arr[i] == arr[j] {
  191. repeat = true
  192. break
  193. }
  194. }
  195. if !repeat {
  196. newArr = append(newArr, arr[i])
  197. }
  198. }
  199. return
  200. }