base_api_controller.go 6.9KB

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