| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package router
- import (
- "demo/data/dao"
- "demo/data/domain"
- "fmt"
- "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, Router *gin.Engine) {
- //用户,包含用户后台
- UserRouth(apiGroup)
- //文章详情页
- DetailRouter(apiGroup)
- //文件上传下载
- FileRouter(apiGroup)
- //测试
- TestRouth(apiGroup)
- //订单,支付
- OrderRouter(apiGroup)
- //-------------------
- //后台管理系统
- BackRouter(apiGroup)
- //TODO 后台管理系统全部生成路由,别进行修改
- BaseBackRouter(apiGroup)
- }
- func PushRouter(api *gin.RouterGroup, method, path string, handlerFunc gin.HandlerFunc) {
- var iRouter gin.IRoutes
- switch method {
- case "GET":
- iRouter = api.GET(path, handlerFunc)
- break
- case "POST":
- iRouter = api.POST(path, handlerFunc)
- break
- case "PUT":
- iRouter = api.PUT(path, handlerFunc)
- break
- case "DELETE":
- iRouter = api.DELETE(path, handlerFunc)
- break
- case "PATCH":
- iRouter = api.PATCH(path, handlerFunc)
- break
- case "HEAD":
- iRouter = api.HEAD(path, handlerFunc)
- break
- default:
- iRouter = api.Any(path, handlerFunc)
- }
- key := api.BasePath() + path
- if strings.HasPrefix(key, Prefix) {
- key = key[len(Prefix):]
- }
- PathIRouterMap[key] = iRouter
- }
- func InitAuthority(Router *gin.Engine) {
- //查询数据库有的权限
- List := dao.GetListAuthorityAll()
- pathMap := make(map[string]domain.BackAuthority)
- 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("===================================")
- }
|