package middleware import ( "bytes" "fmt" "github.com/gin-gonic/gin" "hongze/hongze_yb/utils" "io" "net/http" "strconv" ) // Common 公共中间件 type Common struct{} // 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.SetContextLogListByClaims(c, fmt.Sprint("Url:", c.Request.RequestURI)) // 将请求参数添加到上下文的日志中 utils.SetContextLogListByClaims(c, fmt.Sprint("RequestBody:", string(body))) // 将请求体恢复到原始状态 c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) c.Next() } // 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() }