base_api_controller.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = 400
  57. //userAdmin.Mobile = "13535547901"
  58. userAdmin.Id = 597
  59. userAdmin.Mobile = "19874122664"
  60. userAdmin.IsSuperAdmin = false
  61. userAdmin.Status = 1
  62. userAdmin.CreateTime = 1530786071
  63. userAdmin.ModifyTime = 1530786071
  64. var subscibe models.ServeSubscibe
  65. subscibe.ID = 1
  66. subscibe.OrgId = 12
  67. subscibe.PeriodStart = 1538035409
  68. subscibe.PeriodEnd = 1569571409
  69. subscibe.State = 1
  70. subscibe.Status = 1
  71. subscibe.CreatedTime = 1538035409
  72. subscibe.UpdatedTime = 1538035409
  73. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  74. subscibes[4] = &subscibe
  75. var adminUserInfo service.AdminUserInfo
  76. adminUserInfo.CurrentOrgId = 12
  77. adminUserInfo.CurrentAppId = 18
  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 = 400
  166. //userAdmin.Mobile = "13535547901"
  167. userAdmin.Id = 597
  168. userAdmin.Mobile = "19874122664"
  169. userAdmin.IsSuperAdmin = false
  170. userAdmin.Status = 1
  171. userAdmin.CreateTime = 1530786071
  172. userAdmin.ModifyTime = 1530786071
  173. var subscibe models.ServeSubscibe
  174. subscibe.ID = 1
  175. subscibe.OrgId = 12
  176. subscibe.PeriodStart = 1538035409
  177. subscibe.PeriodEnd = 1569571409
  178. subscibe.State = 1
  179. subscibe.Status = 1
  180. subscibe.CreatedTime = 1538035409
  181. subscibe.UpdatedTime = 1538035409
  182. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  183. subscibes[4] = &subscibe
  184. var adminUserInfo service.AdminUserInfo
  185. adminUserInfo.CurrentOrgId = 12
  186. adminUserInfo.CurrentAppId = 18
  187. adminUserInfo.AdminUser = &userAdmin
  188. adminUserInfo.Subscibes = subscibes
  189. this.SetSession("admin_user_info", &adminUserInfo)
  190. //this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
  191. //this.StopRun()
  192. }
  193. }
  194. func RemoveRepeatedElement2(arr []string) (newArr []string) {
  195. newArr = make([]string, 0)
  196. for i := 0; i < len(arr); i++ {
  197. repeat := false
  198. for j := i + 1; j < len(arr); j++ {
  199. if arr[i] == arr[j] {
  200. repeat = true
  201. break
  202. }
  203. }
  204. if !repeat {
  205. newArr = append(newArr, arr[i])
  206. }
  207. }
  208. return
  209. }