Files
user-system/internal/api/handler/common.go
long-agent b8e9af001f refactor: 提取公共分页解析函数 parsePageAndSize
- 新增 internal/api/handler/common.go 存放 handler 层公共辅助函数
- parsePageAndSize: 统一提取 page/page_size 参数解析、默认值设置、ClampPageSize 调用
- device/log/webhook handler: 替换重复的分页解析代码为 parsePageAndSize 调用
- 清理不再需要的 strconv/pagination 导入
2026-05-08 12:48:03 +08:00

22 lines
575 B
Go

package handler
import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/user-management-system/internal/pagination"
)
// parsePageAndSize extracts and validates page & page_size from query parameters.
// Returns page (>=1) and pageSize (clamped to [1, MaxPageSize]).
func parsePageAndSize(c *gin.Context) (page, pageSize int) {
page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
if page < 1 {
page = 1
}
pageSize, _ = strconv.Atoi(c.DefaultQuery("page_size", strconv.Itoa(pagination.DefaultPageSize)))
pageSize = pagination.ClampPageSize(pageSize)
return
}