base_api_controller.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.Header.Get("Permission") == "5" { //医保相关权限
  87. // org_id, _ := beego.AppConfig.Int64("org_id")
  88. // org, _ := service.GetOrgById(org_id)
  89. // admin_user_id, _ := this.GetInt64("admin_user_id")
  90. // if admin_user_id != org.Creator { //超级管理员不受此限制
  91. // isPermission := false
  92. // //该机构下该用户有多少个
  93. // role, _ := service.GetUserAllRole(org_id, admin_user_id)
  94. // var roles []string
  95. // if len(role.RoleIds) <= 0 { //该用户没有设置角色
  96. //
  97. // } else {
  98. // roles = strings.Split(role.RoleIds, ",")
  99. // }
  100. // fmt.Println(roles)
  101. //
  102. // //获取该用户下所有角色的权限总集
  103. // var userRolePurviews string
  104. // var userRolePurviewsArr []string
  105. // for _, item := range roles {
  106. // role_id, _ := strconv.ParseInt(item, 10, 64)
  107. // purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  108. // if len(userRolePurviews) == 0 {
  109. // userRolePurviews = purviews
  110. // } else {
  111. // userRolePurviews = userRolePurviews + "," + purviews
  112. // }
  113. // }
  114. // //该用户所拥有角色的权限的总集
  115. // userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
  116. // fmt.Println(userRolePurviewsArr)
  117. // //系统所记录的权限列表
  118. // allPermission, _ := service.GetAllFunctionPurview()
  119. //
  120. // for _, item := range allPermission {
  121. // //获取该角色的所有权限
  122. // for _, items := range userRolePurviewsArr {
  123. // id, _ := strconv.ParseInt(items, 10, 64)
  124. // if id == item.ID {
  125. // isPermission = true
  126. // }
  127. // }
  128. // if !isPermission {
  129. // msg, _ := service.FindErrorMsgByStr(item.ID)
  130. // json := make(map[string]interface{})
  131. // json["msg"] = msg
  132. // json["code"] = 0
  133. // json["state"] = 0
  134. // this.Data["json"] = json
  135. // this.ServeJSON()
  136. // this.StopRun()
  137. // }
  138. //
  139. // }
  140. // }
  141. //}
  142. }
  143. type BaseServeAPIController struct {
  144. BaseAPIController
  145. }
  146. func (this *BaseServeAPIController) Prepare() {
  147. this.BaseAPIController.Prepare()
  148. if this.GetAdminUserInfo() == nil {
  149. org_id, _ := beego.AppConfig.Int64("org_id")
  150. var userAdmin models.AdminUser
  151. userAdmin.Id = 1448
  152. userAdmin.Mobile = "13318599895"
  153. userAdmin.IsSuperAdmin = false
  154. userAdmin.Status = 1
  155. userAdmin.CreateTime = 1530786071
  156. userAdmin.ModifyTime = 1530786071
  157. var subscibe models.ServeSubscibe
  158. subscibe.ID = 1
  159. subscibe.OrgId = org_id
  160. subscibe.PeriodStart = 1538035409
  161. subscibe.PeriodEnd = 1569571409
  162. subscibe.State = 1
  163. subscibe.Status = 1
  164. subscibe.CreatedTime = 1538035409
  165. subscibe.UpdatedTime = 1538035409
  166. subscibes := make(map[int64]*models.ServeSubscibe, 0)
  167. subscibes[4] = &subscibe
  168. var adminUserInfo service.AdminUserInfo
  169. adminUserInfo.CurrentOrgId = org_id
  170. adminUserInfo.AdminUser = &userAdmin
  171. this.SetSession("admin_user_info", &adminUserInfo)
  172. }
  173. //if this.Ctx.Request.Header.Get("Permission") == "5" { //医保相关权限
  174. // org_id, _ := beego.AppConfig.Int64("org_id")
  175. // org, _ := service.GetOrgById(org_id)
  176. // admin_user_id, _ := this.GetInt64("admin_user_id")
  177. // if admin_user_id != org.Creator { //超级管理员不受此限制
  178. // isPermission := false
  179. // //该机构下该用户有多少个
  180. // role, _ := service.GetUserAllRole(org_id, admin_user_id)
  181. // var roles []string
  182. // if len(role.RoleIds) <= 0 { //该用户没有设置角色
  183. //
  184. // } else {
  185. // roles = strings.Split(role.RoleIds, ",")
  186. // }
  187. // fmt.Println(roles)
  188. //
  189. // //获取该用户下所有角色的权限总集
  190. // var userRolePurviews string
  191. // var userRolePurviewsArr []string
  192. // for _, item := range roles {
  193. // role_id, _ := strconv.ParseInt(item, 10, 64)
  194. // purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  195. // if len(userRolePurviews) == 0 {
  196. // userRolePurviews = purviews
  197. // } else {
  198. // userRolePurviews = userRolePurviews + "," + purviews
  199. // }
  200. // }
  201. // //该用户所拥有角色的权限的总集
  202. // userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
  203. // fmt.Println(userRolePurviewsArr)
  204. // //系统所记录的权限列表
  205. // allPermission, _ := service.GetAllFunctionPurview()
  206. //
  207. // for _, item := range allPermission {
  208. // //获取该角色的所有权限
  209. // for _, items := range userRolePurviewsArr {
  210. // id, _ := strconv.ParseInt(items, 10, 64)
  211. // if id == item.ID {
  212. // isPermission = true
  213. // }
  214. // }
  215. // if !isPermission {
  216. // msg, _ := service.FindErrorMsgByStr(item.ID)
  217. // json := make(map[string]interface{})
  218. // json["msg"] = msg
  219. // json["code"] = 0
  220. // json["state"] = 0
  221. // this.Data["json"] = json
  222. // this.ServeJSON()
  223. // this.StopRun()
  224. // }
  225. //
  226. // }
  227. // }
  228. //}
  229. }
  230. func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
  231. newArr = make([]string, 0)
  232. for i := 0; i < len(arr); i++ {
  233. repeat := false
  234. for j := i + 1; j < len(arr); j++ {
  235. if arr[i] == arr[j] {
  236. repeat = true
  237. break
  238. }
  239. }
  240. if !repeat {
  241. newArr = append(newArr, arr[i])
  242. }
  243. }
  244. return
  245. }