Interceptor.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package router
  2. import (
  3. "demo/data/dao"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "strings"
  7. )
  8. // func Interceptor(InterceptorName string) gin.HandlerFunc {
  9. // switch InterceptorName {
  10. // case "login":
  11. // return LoginInterceptor()
  12. // case "log":
  13. // return LogInterceptor()
  14. // case "authority":
  15. // return AuthorityInterceptor()
  16. // default:
  17. // return func(context *gin.Context) {
  18. // fmt.Println("未知拦截器:" + InterceptorName)
  19. // }
  20. // }
  21. // }
  22. func Interceptor() gin.HandlerFunc {
  23. return func(context *gin.Context) {
  24. path := context.FullPath()
  25. if path[0:len(Prefix)] == Prefix {
  26. path = path[len(Prefix):]
  27. }
  28. authority := PathRouterMap[path]
  29. if authority.AuthorityVerification != "" {
  30. Interceptors := strings.Split(authority.AuthorityVerification, ",")
  31. for _, v := range Interceptors {
  32. switch v {
  33. case "log":
  34. if !LogInterceptor(context) {
  35. return
  36. }
  37. break
  38. case "login":
  39. if !LoginInterceptor(context) {
  40. return
  41. }
  42. break
  43. case "authority":
  44. if !AuthorityInterceptor(context) {
  45. return
  46. }
  47. break
  48. default:
  49. fmt.Println("未知拦截器:" + v)
  50. }
  51. }
  52. }
  53. }
  54. }
  55. // LoginInterceptor 登录拦截器
  56. func LoginInterceptor(c *gin.Context) bool {
  57. id := GetUserIdByToken(c)
  58. if id == 0 {
  59. fmt.Println("拦截器:用户未登录")
  60. //这里终止后续请求访问
  61. c.Abort()
  62. return false
  63. }
  64. return true
  65. }
  66. // LogInterceptor 日志拦截器
  67. func LogInterceptor(c *gin.Context) bool {
  68. keys := c.Keys
  69. fmt.Println("==========================日志系统==========================\nkeys:", keys)
  70. fmt.Println("路由地址:", c.Request.URL)
  71. fmt.Println("路由参数:", c.Request.Form)
  72. return true
  73. }
  74. // AuthorityInterceptor 权限拦截器
  75. func AuthorityInterceptor(c *gin.Context) bool {
  76. id := GetUserIdByToken(c)
  77. if id == 0 {
  78. fmt.Println("拦截器:用户未登录")
  79. //这里终止后续请求访问
  80. c.Abort()
  81. return false
  82. }
  83. path := c.FullPath()
  84. if path[0:len(Prefix)] == Prefix {
  85. path = path[len(Prefix):]
  86. }
  87. if !dao.CheckAuthorityByUserId(id, PathRouterMap[path].Id) {
  88. fmt.Println("拦截器:用户权限不足")
  89. c.JSON(200, CreateResultError(500, "权限不足"))
  90. //这里终止后续请求访问
  91. c.Abort()
  92. return false
  93. }
  94. return true
  95. }
  96. //
  97. //// LoginInterceptor 登录拦截器
  98. //func LoginInterceptor() gin.HandlerFunc {
  99. // return func(c *gin.Context) {
  100. // id := GetUserIdByToken(c)
  101. // fmt.Println("拦截器", id)
  102. // if id == 0 {
  103. // fmt.Println("拦截器:用户未登录")
  104. // //这里终止后续请求访问
  105. // c.Abort()
  106. // return
  107. // }
  108. // }
  109. //}
  110. //
  111. //// LogInterceptor 日志拦截器
  112. //func LogInterceptor() gin.HandlerFunc {
  113. // return func(c *gin.Context) {
  114. // fmt.Println("日志系统")
  115. // c.Next()
  116. // fmt.Println("日志系统666")
  117. // }
  118. //}
  119. //
  120. //// AuthorityInterceptor 权限拦截器
  121. //func AuthorityInterceptor() gin.HandlerFunc {
  122. // return func(c *gin.Context) {
  123. // id := GetUserIdByToken(c)
  124. // if id == 0 {
  125. // c.Abort()
  126. // }
  127. // path := c.FullPath()
  128. // fmt.Println(path)
  129. //
  130. // //dao.CheckAuthorityByUserId(id,)
  131. // }
  132. //}