package router import ( "demo/data/dao" "demo/data/domain" "github.com/gin-gonic/gin" "strings" "time" ) var PathRouterMap = make(map[string]domain.BackAuthority) //var PathIRouterMap = make(map[string]gin.IRoutes) const Prefix = "/api" func InitRouter(apiGroup *gin.RouterGroup) { apiGroup.Use(Interceptor()) //用户,包含用户后台 UserRouth(apiGroup) //文章详情页 DetailRouter(apiGroup) //文件上传下载 FileRouter(apiGroup) //测试 TestRouth(apiGroup) //订单,支付 OrderRouter(apiGroup) //文章接口 ArticleRouter(apiGroup) //废弃 //BackGoodsRouter(apiGroup) //------------------- //后台管理系统 BackRouter(apiGroup) //首页 HomeRouter(apiGroup) //TODO 后台管理系统全部生成路由,别进行修改 BaseBackRouter(apiGroup) } // PushRouter 根据参数,添加到对应的路由组中,并保存路由信息 func PushRouter(api *gin.RouterGroup, method, path string, handlerFunc gin.HandlerFunc) { switch method { case "GET": api.GET(path, handlerFunc) break case "POST": api.POST(path, handlerFunc) break case "PUT": api.PUT(path, handlerFunc) break case "DELETE": api.DELETE(path, handlerFunc) break case "PATCH": api.PATCH(path, handlerFunc) break case "HEAD": api.HEAD(path, handlerFunc) break default: api.Any(path, handlerFunc) } key := api.BasePath() + path if strings.HasPrefix(key, Prefix) { key = key[len(Prefix):] } } // InitAuthority 初始化,将数据库中的路径,对应到路由中 func InitAuthority(Router *gin.Engine) { //查询数据库有的权限 List := dao.GetListAuthorityAll() pathMap := make(map[string]domain.BackAuthority) //添加到map中,方便后面调用 for i := range List { pathMap[List[i].AuthorityPath] = List[i] } //获取全部路由 routes := Router.Routes() //新写的路由代码,自动添加 var insertRouter = make([]*domain.BackAuthority, 0) for i := range routes { method := Router.Routes()[i].Method path := Router.Routes()[i].Path if strings.HasPrefix(path, Prefix) { path = path[len(Prefix):] } if _, ok := pathMap[path]; !ok { insertRouter = append(insertRouter, &domain.BackAuthority{ AuthorityPath: path, Method: method, State: "1", AuthorityVerification: "", CreateTime: time.Now()}) } } //添加接口数据到数据库 dao.AddAllListAuthorityAll(insertRouter) for i := range insertRouter { pathMap[insertRouter[i].AuthorityPath] = *insertRouter[i] } PathRouterMap = pathMap //for s := range PathIRouterMap { // fmt.Println("拦截器添加路由:", PathRouterMap[s].AuthorityPath, pathMap[s].AuthorityVerification) // AddUseInterceptor(PathIRouterMap[s], pathMap[s].AuthorityVerification) //} } //func AddUseInterceptor(routes gin.IRoutes, AuthorityVerification string) { // split := strings.Split(AuthorityVerification, ",") // for i := range split { // //根据名称添加不同的拦截器 // h := Interceptor(split[i]) // routes.Use(h) // } // fmt.Println("===================================") //}