package middleware import ( "bytes" "eta_gn/eta_bridge/utils" "fmt" "github.com/gin-gonic/gin" "io" "net/http" "strconv" ) // Common 公共中间件 //func Common() gin.HandlerFunc { // return func(c *gin.Context) { // var currPage, pageSize int // reqPage := c.DefaultQuery("curr_page", "0") // currPage, _ = strconv.Atoi(reqPage) // if currPage <= 0 { // currPage = 1 // } // // reqPageSize := c.DefaultQuery("page_size", "0") // pageSize, _ = strconv.Atoi(reqPageSize) // if pageSize <= 0 { // pageSize = 20 // } // c.Set("curr_page", currPage) // c.Set("page_size", pageSize) // c.Next() // } //} type Common struct{} // Page // @Description: 分页参数 // @author: Roc // @receiver common // @datetime 2024-10-31 10:19:36 // @param c *gin.Context func (common *Common) Page(c *gin.Context) { var currPage, pageSize int reqPage := c.DefaultQuery("curr_page", "0") currPage, _ = strconv.Atoi(reqPage) if currPage <= 0 { currPage = 1 } reqPageSize := c.DefaultQuery("page_size", "0") pageSize, _ = strconv.Atoi(reqPageSize) if pageSize <= 0 { pageSize = 20 } c.Set("curr_page", currPage) c.Set("page_size", pageSize) c.Next() } // RequestLog // @Description: 请求参数日志 // @author: Roc // @receiver common // @datetime 2024-10-31 10:19:36 // @param c *gin.Context func (common *Common) RequestLog(c *gin.Context) { // 读取请求体 body, err := io.ReadAll(c.Request.Body) if err != nil { //log.Printf("Error reading request body: %v", err) c.AbortWithStatus(http.StatusInternalServerError) return } // 将请求地址添加到上下文的日志中 utils.SetBridgeLogListByClaims(c, fmt.Sprint("Url:", c.Request.RequestURI)) // 将请求参数添加到上下文的日志中 utils.SetBridgeLogListByClaims(c, fmt.Sprint("RequestBody:", string(body))) // 将请求体恢复到原始状态 c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) c.Next() }