base_api_controller.go 7.0KB

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