base_api_controller.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. org_id, _ := beego.AppConfig.Int64("org_id")
  60. var userAdmin models.AdminUser
  61. userAdmin.Id = 1448
  62. userAdmin.Mobile = "13318599895"
  63. //userAdmin.Id = 597
  64. //userAdmin.Mobile = "19874122664"
  65. userAdmin.IsSuperAdmin = false
  66. userAdmin.Status = 1
  67. userAdmin.CreateTime = 1530786071
  68. userAdmin.ModifyTime = 1530786071
  69. var subscibe models.ServeSubscibe
  70. subscibe.ID = 1
  71. subscibe.OrgId = org_id
  72. subscibe.PeriodStart = 1538035409
  73. subscibe.PeriodEnd = 1569571409
  74. subscibe.State = 1
  75. subscibe.Status = 1
  76. subscibe.CreatedTime = 1538035409
  77. subscibe.UpdatedTime = 1538035409
  78. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  79. subscibes[4] = &subscibe
  80. var adminUserInfo service.AdminUserInfo
  81. adminUserInfo.CurrentOrgId = org_id
  82. adminUserInfo.CurrentAppId = 8642
  83. adminUserInfo.AdminUser = &userAdmin
  84. this.SetSession("admin_user_info", &adminUserInfo)
  85. }
  86. //if this.Ctx.Request.Method != "GET" {
  87. // adminUserInfo := this.GetAdminUserInfo()
  88. //
  89. // err := service.GetOrgSubscibeState(adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId])
  90. // if err != nil || adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId].State == 3 {
  91. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotSubscibe)
  92. // this.StopRun()
  93. // }
  94. //}
  95. //if this.Ctx.Request.Header.Get("Permission") == "1" {
  96. // isPermission := false
  97. //
  98. // //adminUserInfo := this.GetAdminUserInfo()
  99. //
  100. // //service.GetUserAllRole(adminUserInfo.CurrentOrgId)
  101. //
  102. //
  103. //
  104. //
  105. // roles := []int64{1,2} //模拟该用户有多少角色
  106. // var targetRole models.RolePurview
  107. // var userRolePurview []string
  108. //
  109. // //用户角色1对应的权限
  110. // role1 := models.RolePurview{PurviewIds:"11,12,13"}
  111. //
  112. // //用户角色2对应的权限
  113. // role2 := models.RolePurview{PurviewIds:"11,15,16"}
  114. //
  115. //
  116. // //模拟角色2
  117. // //判断该用户有多少个角色,合并最大角色权限
  118. // if len(roles) == 1{ //单个
  119. // targetRole = role1
  120. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  121. //
  122. // }else{ //多个
  123. // targetRole = role1
  124. // targetRole.PurviewIds = targetRole.PurviewIds + "," + role2.PurviewIds
  125. // userRolePurview = strings.Split(targetRole.PurviewIds, ",")
  126. // }
  127. //
  128. // userRolePurview = RemoveRepeatedElement2(userRolePurview)
  129. //
  130. // fmt.Println(userRolePurview)
  131. // //所有权限列表
  132. // allPermission := []models.Purview{{Id:10,Urlfor:"/m/api/dialysis/dialysisPrescription-/api/dialysis/prescription"},
  133. // {Id:11,Urlfor:"/m/api/dialysis/dialysisPrescription1-/api/dialysis/prescription1"},
  134. // {Id:12,Urlfor:"/m/api/dialysis/dialysisPrescription2-/api/dialysis/prescription2"},
  135. // {Id:13,Urlfor:"/m/api/dialysis/dialysisPrescription3-/api/dialysis/prescription3"},
  136. // {Id:14,Urlfor:"/m/api/dialysis/dialysisPrescription4-/api/dialysis/prescription4"},
  137. // {Id:15,Urlfor:"/m/api/dialysis/dialysisPrescription5-/api/dialysis/prescription5"},
  138. // {Id:16,Urlfor:"/m/api/dialysis/dialysisPrescription6-/api/dialysis/prescription6"}}
  139. // for _, item := range allPermission {
  140. // //判断当前路由是否在权限路由列表里面
  141. // if strings.Split(item.Urlfor, "-")[1] == strings.Split(this.Ctx.Request.RequestURI , "?")[0]{
  142. // fmt.Println(strings.Split(this.Ctx.Request.RequestURI , "?")[0])
  143. //
  144. // //获取该角色的所有权限
  145. // for _, items := range userRolePurview{
  146. // id, _ := strconv.ParseInt(items, 10, 64)
  147. // if id == item.Id{
  148. // isPermission = true
  149. // }
  150. // }
  151. // if !isPermission{
  152. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePreExist)
  153. // this.StopRun()
  154. // }
  155. // }
  156. //
  157. // }
  158. //}
  159. }
  160. type BaseServeAPIController struct {
  161. BaseAPIController
  162. }
  163. func (this *BaseServeAPIController) Prepare() {
  164. this.BaseAPIController.Prepare()
  165. if this.GetAdminUserInfo() == nil {
  166. //org_id, _ := beego.AppConfig.Int64("org_id")
  167. org_id, _ := beego.AppConfig.Int64("org_id")
  168. //org_id, _ := this.GetInt64("org_id")
  169. var userAdmin models.AdminUser
  170. userAdmin.Id = 1448
  171. userAdmin.Mobile = "13318599895"
  172. //userAdmin.Id = 597
  173. //userAdmin.Mobile = "19874122664"
  174. userAdmin.IsSuperAdmin = false
  175. userAdmin.Status = 1
  176. userAdmin.CreateTime = 1530786071
  177. userAdmin.ModifyTime = 1530786071
  178. var subscibe models.ServeSubscibe
  179. subscibe.ID = 1
  180. subscibe.OrgId = org_id
  181. subscibe.PeriodStart = 1538035409
  182. subscibe.PeriodEnd = 1569571409
  183. subscibe.State = 1
  184. subscibe.Status = 1
  185. subscibe.CreatedTime = 1538035409
  186. subscibe.UpdatedTime = 1538035409
  187. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  188. subscibes[4] = &subscibe
  189. var adminUserInfo service.AdminUserInfo
  190. adminUserInfo.CurrentOrgId = org_id
  191. adminUserInfo.AdminUser = &userAdmin
  192. this.SetSession("admin_user_info", &adminUserInfo)
  193. }
  194. //if adminUserInfo.AppRole != nil {
  195. // if adminUserInfo.AppRole.Id > 0 {
  196. // app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
  197. // if app_role != nil {
  198. // if app_role.Status != 1 {
  199. // this.DelSession("mobile_admin_user_info")
  200. // this.Ctx.SetCookie("token_cookie", "")
  201. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
  202. // this.StopRun()
  203. // }
  204. // }
  205. // }
  206. //
  207. //}
  208. //fmt.Println("222222222")
  209. }
  210. func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
  211. newArr = make([]string, 0)
  212. for i := 0; i < len(arr); i++ {
  213. repeat := false
  214. for j := i + 1; j < len(arr); j++ {
  215. if arr[i] == arr[j] {
  216. repeat = true
  217. break
  218. }
  219. }
  220. if !repeat {
  221. newArr = append(newArr, arr[i])
  222. }
  223. }
  224. return
  225. }