Interceptor.go 898 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "strings"
  6. )
  7. func LoginInterceptor() gin.HandlerFunc {
  8. return func(c *gin.Context) {
  9. id := GetUserIdByToken(c)
  10. fmt.Println("拦截器", id)
  11. if id == 0 {
  12. fmt.Println("拦截器:用户未登录")
  13. //这里终止后续请求访问
  14. c.Abort()
  15. return
  16. }
  17. }
  18. }
  19. func Interceptor(InterceptorName string) gin.HandlerFunc {
  20. switch InterceptorName {
  21. case "login":
  22. return LoginInterceptor()
  23. case "log":
  24. return LogInterceptor()
  25. default:
  26. return func(context *gin.Context) {}
  27. }
  28. }
  29. func AddUseInterceptor(routes gin.IRoutes, AuthorityVerification string) {
  30. split := strings.Split(AuthorityVerification, ",")
  31. for i := range split {
  32. routes.Use(Interceptor(split[i]))
  33. }
  34. }
  35. func LogInterceptor() gin.HandlerFunc {
  36. return func(c *gin.Context) {
  37. fmt.Println("日志系统")
  38. c.Next()
  39. fmt.Println("日志系统666")
  40. }
  41. }