Router.go 2.9 KB

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