| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package router
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- )
- func Interceptor(InterceptorName string) gin.HandlerFunc {
- switch InterceptorName {
- case "login":
- return LoginInterceptor()
- case "log":
- return LogInterceptor()
- case "authority":
- return AuthorityInterceptor()
- default:
- return func(context *gin.Context) {
- fmt.Println("未知拦截器:" + InterceptorName)
- }
- }
- }
- // LoginInterceptor 登录拦截器
- func LoginInterceptor() gin.HandlerFunc {
- return func(c *gin.Context) {
- id := GetUserIdByToken(c)
- fmt.Println("拦截器", id)
- if id == 0 {
- fmt.Println("拦截器:用户未登录")
- //这里终止后续请求访问
- c.Abort()
- return
- }
- }
- }
- // LogInterceptor 日志拦截器
- func LogInterceptor() gin.HandlerFunc {
- return func(c *gin.Context) {
- fmt.Println("日志系统")
- c.Next()
- fmt.Println("日志系统666")
- }
- }
- // AuthorityInterceptor 权限拦截器
- func AuthorityInterceptor() gin.HandlerFunc {
- return func(c *gin.Context) {
- id := GetUserIdByToken(c)
- if id == 0 {
- c.Abort()
- }
- path := c.FullPath()
- fmt.Println(path)
- //dao.CheckAuthorityByUserId(id,)
- }
- }
|