common.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package middleware
  2. import (
  3. "bytes"
  4. "eta_gn/eta_bridge/utils"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "io"
  8. "net/http"
  9. "strconv"
  10. )
  11. // Common 公共中间件
  12. //func Common() gin.HandlerFunc {
  13. // return func(c *gin.Context) {
  14. // var currPage, pageSize int
  15. // reqPage := c.DefaultQuery("curr_page", "0")
  16. // currPage, _ = strconv.Atoi(reqPage)
  17. // if currPage <= 0 {
  18. // currPage = 1
  19. // }
  20. //
  21. // reqPageSize := c.DefaultQuery("page_size", "0")
  22. // pageSize, _ = strconv.Atoi(reqPageSize)
  23. // if pageSize <= 0 {
  24. // pageSize = 20
  25. // }
  26. // c.Set("curr_page", currPage)
  27. // c.Set("page_size", pageSize)
  28. // c.Next()
  29. // }
  30. //}
  31. type Common struct{}
  32. // Page
  33. // @Description: 分页参数
  34. // @author: Roc
  35. // @receiver common
  36. // @datetime 2024-10-31 10:19:36
  37. // @param c *gin.Context
  38. func (common *Common) Page(c *gin.Context) {
  39. var currPage, pageSize int
  40. reqPage := c.DefaultQuery("curr_page", "0")
  41. currPage, _ = strconv.Atoi(reqPage)
  42. if currPage <= 0 {
  43. currPage = 1
  44. }
  45. reqPageSize := c.DefaultQuery("page_size", "0")
  46. pageSize, _ = strconv.Atoi(reqPageSize)
  47. if pageSize <= 0 {
  48. pageSize = 20
  49. }
  50. c.Set("curr_page", currPage)
  51. c.Set("page_size", pageSize)
  52. c.Next()
  53. }
  54. // RequestLog
  55. // @Description: 请求参数日志
  56. // @author: Roc
  57. // @receiver common
  58. // @datetime 2024-10-31 10:19:36
  59. // @param c *gin.Context
  60. func (common *Common) RequestLog(c *gin.Context) {
  61. // 读取请求体
  62. body, err := io.ReadAll(c.Request.Body)
  63. if err != nil {
  64. //log.Printf("Error reading request body: %v", err)
  65. c.AbortWithStatus(http.StatusInternalServerError)
  66. return
  67. }
  68. // 将请求地址添加到上下文的日志中
  69. utils.SetBridgeLogListByClaims(c, fmt.Sprint("Url:", c.Request.RequestURI))
  70. // 将请求参数添加到上下文的日志中
  71. utils.SetBridgeLogListByClaims(c, fmt.Sprint("RequestBody:", string(body)))
  72. // 将请求体恢复到原始状态
  73. c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
  74. c.Next()
  75. }