Router.go 3.0 KB

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