Router.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package router
  2. import (
  3. "demo/data/dao"
  4. "demo/data/domain"
  5. "github.com/gin-gonic/gin"
  6. "strings"
  7. "time"
  8. )
  9. var PathRouterMap = make(map[string]domain.BackAuthority)
  10. //var PathIRouterMap = make(map[string]gin.IRoutes)
  11. const Prefix = "/api"
  12. func InitRouter(apiGroup *gin.RouterGroup) {
  13. apiGroup.Use(Interceptor())
  14. //用户,包含用户后台
  15. UserRouth(apiGroup)
  16. //文章详情页
  17. DetailRouter(apiGroup)
  18. //文件上传下载
  19. FileRouter(apiGroup)
  20. //测试
  21. //TestRouth(apiGroup)
  22. //订单,支付
  23. OrderRouter(apiGroup)
  24. //文章接口
  25. ArticleRouter(apiGroup)
  26. //废弃
  27. //BackGoodsRouter(apiGroup)
  28. //-------------------
  29. //后台管理系统
  30. BackRouter(apiGroup)
  31. BackGoodsRouter(apiGroup)
  32. //首页
  33. HomeRouter(apiGroup)
  34. //TODO 后台管理系统全部生成路由,别进行修改
  35. BaseBackRouter(apiGroup)
  36. }
  37. // PushRouter 根据参数,添加到对应的路由组中,并保存路由信息
  38. func PushRouter(api *gin.RouterGroup, method, path string, handlerFunc gin.HandlerFunc) {
  39. method = strings.ToUpper(method)
  40. switch method {
  41. case "GET":
  42. api.GET(path, handlerFunc)
  43. break
  44. case "POST":
  45. api.POST(path, handlerFunc)
  46. break
  47. case "PUT":
  48. api.PUT(path, handlerFunc)
  49. break
  50. case "DELETE":
  51. api.DELETE(path, handlerFunc)
  52. break
  53. case "PATCH":
  54. api.PATCH(path, handlerFunc)
  55. break
  56. case "HEAD":
  57. api.HEAD(path, handlerFunc)
  58. break
  59. default:
  60. api.Any(path, handlerFunc)
  61. }
  62. key := api.BasePath() + path
  63. if strings.HasPrefix(key, Prefix) {
  64. key = key[len(Prefix):]
  65. }
  66. }
  67. // InitAuthority 初始化,将数据库中的路径,对应到路由中
  68. func InitAuthority(Router *gin.Engine) {
  69. //查询数据库有的权限
  70. List := dao.GetListAuthorityAll()
  71. pathMap := make(map[string]domain.BackAuthority)
  72. //添加到map中,方便后面调用
  73. for i := range List {
  74. pathMap[List[i].AuthorityPath] = List[i]
  75. }
  76. //获取全部路由
  77. routes := Router.Routes()
  78. //新写的路由代码,自动添加
  79. var insertRouter = make([]*domain.BackAuthority, 0)
  80. for i := range routes {
  81. method := Router.Routes()[i].Method
  82. path := Router.Routes()[i].Path
  83. if strings.HasPrefix(path, Prefix) {
  84. path = path[len(Prefix):]
  85. }
  86. if _, ok := pathMap[path]; !ok {
  87. insertRouter = append(insertRouter, &domain.BackAuthority{
  88. AuthorityPath: path,
  89. Method: method,
  90. State: "1",
  91. AuthorityVerification: "",
  92. CreateTime: time.Now()})
  93. }
  94. }
  95. //添加接口数据到数据库
  96. dao.AddAllListAuthorityAll(insertRouter)
  97. for i := range insertRouter {
  98. pathMap[insertRouter[i].AuthorityPath] = *insertRouter[i]
  99. }
  100. PathRouterMap = pathMap
  101. //for s := range PathIRouterMap {
  102. // fmt.Println("拦截器添加路由:", PathRouterMap[s].AuthorityPath, pathMap[s].AuthorityVerification)
  103. // AddUseInterceptor(PathIRouterMap[s], pathMap[s].AuthorityVerification)
  104. //}
  105. }
  106. //func AddUseInterceptor(routes gin.IRoutes, AuthorityVerification string) {
  107. // split := strings.Split(AuthorityVerification, ",")
  108. // for i := range split {
  109. // //根据名称添加不同的拦截器
  110. // h := Interceptor(split[i])
  111. // routes.Use(h)
  112. // }
  113. // fmt.Println("===================================")
  114. //}