Router.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, Router *gin.Engine) {
  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. func PushRouter(api *gin.RouterGroup, method, path string, handlerFunc gin.HandlerFunc) {
  31. var iRouter gin.IRoutes
  32. switch method {
  33. case "GET":
  34. iRouter = api.GET(path, handlerFunc)
  35. break
  36. case "POST":
  37. iRouter = api.POST(path, handlerFunc)
  38. break
  39. case "PUT":
  40. iRouter = api.PUT(path, handlerFunc)
  41. break
  42. case "DELETE":
  43. iRouter = api.DELETE(path, handlerFunc)
  44. break
  45. case "PATCH":
  46. iRouter = api.PATCH(path, handlerFunc)
  47. break
  48. case "HEAD":
  49. iRouter = api.HEAD(path, handlerFunc)
  50. break
  51. default:
  52. iRouter = api.Any(path, handlerFunc)
  53. }
  54. key := api.BasePath() + path
  55. if strings.HasPrefix(key, Prefix) {
  56. key = key[len(Prefix):]
  57. }
  58. PathIRouterMap[key] = iRouter
  59. }
  60. func InitAuthority(Router *gin.Engine) {
  61. //查询数据库有的权限
  62. List := dao.GetListAuthorityAll()
  63. pathMap := make(map[string]domain.BackAuthority)
  64. for i := range List {
  65. pathMap[List[i].AuthorityPath] = List[i]
  66. }
  67. routes := Router.Routes()
  68. var insertRouter = make([]*domain.BackAuthority, 0)
  69. for i := range routes {
  70. method := Router.Routes()[i].Method
  71. path := Router.Routes()[i].Path
  72. if strings.HasPrefix(path, Prefix) {
  73. path = path[len(Prefix):]
  74. }
  75. if _, ok := pathMap[path]; !ok {
  76. insertRouter = append(insertRouter, &domain.BackAuthority{
  77. AuthorityPath: path,
  78. Method: method,
  79. State: "1",
  80. AuthorityVerification: "",
  81. CreateTime: time.Now()})
  82. }
  83. }
  84. dao.AddAllListAuthorityAll(insertRouter)
  85. for i := range insertRouter {
  86. pathMap[insertRouter[i].AuthorityPath] = *insertRouter[i]
  87. }
  88. PathRouterMap = pathMap
  89. for s := range PathIRouterMap {
  90. fmt.Println("拦截器添加路由:", PathRouterMap[s].AuthorityPath, pathMap[s].AuthorityVerification)
  91. AddUseInterceptor(PathIRouterMap[s], pathMap[s].AuthorityVerification)
  92. }
  93. }
  94. func AddUseInterceptor(routes gin.IRoutes, AuthorityVerification string) {
  95. split := strings.Split(AuthorityVerification, ",")
  96. for i := range split {
  97. h := Interceptor(split[i])
  98. routes.Use(h)
  99. }
  100. fmt.Println("===================================")
  101. }