base_api_controller.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. }
  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. //org_id, _ := beego.AppConfig.Int64("org_id")
  166. org_id, _ := this.GetInt64("org_id")
  167. var userAdmin models.AdminUser
  168. userAdmin.Id = 1448
  169. userAdmin.Mobile = "13318599895"
  170. //userAdmin.Id = 597
  171. //userAdmin.Mobile = "19874122664"
  172. userAdmin.IsSuperAdmin = false
  173. userAdmin.Status = 1
  174. userAdmin.CreateTime = 1530786071
  175. userAdmin.ModifyTime = 1530786071
  176. var subscibe models.ServeSubscibe
  177. subscibe.ID = 1
  178. subscibe.OrgId = org_id
  179. subscibe.PeriodStart = 1538035409
  180. subscibe.PeriodEnd = 1569571409
  181. subscibe.State = 1
  182. subscibe.Status = 1
  183. subscibe.CreatedTime = 1538035409
  184. subscibe.UpdatedTime = 1538035409
  185. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  186. subscibes[4] = &subscibe
  187. var adminUserInfo service.AdminUserInfo
  188. adminUserInfo.CurrentOrgId = org_id
  189. adminUserInfo.AdminUser = &userAdmin
  190. this.SetSession("admin_user_info", &adminUserInfo)
  191. }
  192. //if adminUserInfo.AppRole != nil {
  193. // if adminUserInfo.AppRole.Id > 0 {
  194. // app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
  195. // if app_role != nil {
  196. // if app_role.Status != 1 {
  197. // this.DelSession("mobile_admin_user_info")
  198. // this.Ctx.SetCookie("token_cookie", "")
  199. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
  200. // this.StopRun()
  201. // }
  202. // }
  203. // }
  204. //
  205. //}
  206. //fmt.Println("222222222")
  207. }
  208. func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
  209. newArr = make([]string, 0)
  210. for i := 0; i < len(arr); i++ {
  211. repeat := false
  212. for j := i + 1; j < len(arr); j++ {
  213. if arr[i] == arr[j] {
  214. repeat = true
  215. break
  216. }
  217. }
  218. if !repeat {
  219. newArr = append(newArr, arr[i])
  220. }
  221. }
  222. return
  223. }