Interceptor.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func Interceptor(InterceptorName string) gin.HandlerFunc {
  7. switch InterceptorName {
  8. case "login":
  9. return LoginInterceptor()
  10. case "log":
  11. return LogInterceptor()
  12. case "authority":
  13. return AuthorityInterceptor()
  14. default:
  15. return func(context *gin.Context) {
  16. fmt.Println("未知拦截器:" + InterceptorName)
  17. }
  18. }
  19. }
  20. // LoginInterceptor 登录拦截器
  21. func LoginInterceptor() gin.HandlerFunc {
  22. return func(c *gin.Context) {
  23. id := GetUserIdByToken(c)
  24. fmt.Println("拦截器", id)
  25. if id == 0 {
  26. fmt.Println("拦截器:用户未登录")
  27. //这里终止后续请求访问
  28. c.Abort()
  29. return
  30. }
  31. }
  32. }
  33. // LogInterceptor 日志拦截器
  34. func LogInterceptor() gin.HandlerFunc {
  35. return func(c *gin.Context) {
  36. fmt.Println("日志系统")
  37. c.Next()
  38. fmt.Println("日志系统666")
  39. }
  40. }
  41. // AuthorityInterceptor 权限拦截器
  42. func AuthorityInterceptor() gin.HandlerFunc {
  43. return func(c *gin.Context) {
  44. id := GetUserIdByToken(c)
  45. if id == 0 {
  46. c.Abort()
  47. }
  48. path := c.FullPath()
  49. fmt.Println(path)
  50. //dao.CheckAuthorityByUserId(id,)
  51. }
  52. }