Router.go 3.0 KB

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