Router.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. BackGoodsRouter(apiGroup)
  25. //-------------------
  26. //后台管理系统
  27. BackRouter(apiGroup)
  28. //首页
  29. HomeRouter(apiGroup)
  30. //TODO 后台管理系统全部生成路由,别进行修改
  31. BaseBackRouter(apiGroup)
  32. }
  33. // PushRouter 根据参数,添加到对应的路由组中,并保存路由信息
  34. func PushRouter(api *gin.RouterGroup, method, path string, handlerFunc gin.HandlerFunc) {
  35. switch method {
  36. case "GET":
  37. api.GET(path, handlerFunc)
  38. break
  39. case "POST":
  40. api.POST(path, handlerFunc)
  41. break
  42. case "PUT":
  43. api.PUT(path, handlerFunc)
  44. break
  45. case "DELETE":
  46. api.DELETE(path, handlerFunc)
  47. break
  48. case "PATCH":
  49. api.PATCH(path, handlerFunc)
  50. break
  51. case "HEAD":
  52. api.HEAD(path, handlerFunc)
  53. break
  54. default:
  55. api.Any(path, handlerFunc)
  56. }
  57. key := api.BasePath() + path
  58. if strings.HasPrefix(key, Prefix) {
  59. key = key[len(Prefix):]
  60. }
  61. }
  62. // InitAuthority 初始化,将数据库中的路径,对应到路由中
  63. func InitAuthority(Router *gin.Engine) {
  64. //查询数据库有的权限
  65. List := dao.GetListAuthorityAll()
  66. pathMap := make(map[string]domain.BackAuthority)
  67. //添加到map中,方便后面调用
  68. for i := range List {
  69. pathMap[List[i].AuthorityPath] = List[i]
  70. }
  71. //获取全部路由
  72. routes := Router.Routes()
  73. //新写的路由代码,自动添加
  74. var insertRouter = make([]*domain.BackAuthority, 0)
  75. for i := range routes {
  76. method := Router.Routes()[i].Method
  77. path := Router.Routes()[i].Path
  78. if strings.HasPrefix(path, Prefix) {
  79. path = path[len(Prefix):]
  80. }
  81. if _, ok := pathMap[path]; !ok {
  82. insertRouter = append(insertRouter, &domain.BackAuthority{
  83. AuthorityPath: path,
  84. Method: method,
  85. State: "1",
  86. AuthorityVerification: "",
  87. CreateTime: time.Now()})
  88. }
  89. }
  90. //添加接口数据到数据库
  91. dao.AddAllListAuthorityAll(insertRouter)
  92. for i := range insertRouter {
  93. pathMap[insertRouter[i].AuthorityPath] = *insertRouter[i]
  94. }
  95. PathRouterMap = pathMap
  96. //for s := range PathIRouterMap {
  97. // fmt.Println("拦截器添加路由:", PathRouterMap[s].AuthorityPath, pathMap[s].AuthorityVerification)
  98. // AddUseInterceptor(PathIRouterMap[s], pathMap[s].AuthorityVerification)
  99. //}
  100. }
  101. //func AddUseInterceptor(routes gin.IRoutes, AuthorityVerification string) {
  102. // split := strings.Split(AuthorityVerification, ",")
  103. // for i := range split {
  104. // //根据名称添加不同的拦截器
  105. // h := Interceptor(split[i])
  106. // routes.Use(h)
  107. // }
  108. // fmt.Println("===================================")
  109. //}