router.go 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package router
  2. import (
  3. "github.com/gin-gonic/gin"
  4. jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
  5. )
  6. var (
  7. routerNoCheckRole = make([]func(*gin.RouterGroup), 0)
  8. routerCheckRole = make([]func(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware), 0)
  9. )
  10. // initRouter 路由示例
  11. func initRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.Engine {
  12. // 无需认证的路由
  13. noCheckRoleRouter(r)
  14. // 需要认证的路由
  15. checkRoleRouter(r, authMiddleware)
  16. return r
  17. }
  18. // noCheckRoleRouter 无需认证的路由示例
  19. func noCheckRoleRouter(r *gin.Engine) {
  20. // 可根据业务需求来设置接口版本
  21. v1 := r.Group("/api/v1")
  22. for _, f := range routerNoCheckRole {
  23. f(v1)
  24. }
  25. }
  26. // checkRoleRouter 需要认证的路由示例
  27. func checkRoleRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) {
  28. // 可根据业务需求来设置接口版本
  29. v1 := r.Group("/api/v1")
  30. for _, f := range routerCheckRole {
  31. f(v1, authMiddleware)
  32. }
  33. }