refactor: 提取公共分页解析函数 parsePageAndSize

- 新增 internal/api/handler/common.go 存放 handler 层公共辅助函数
- parsePageAndSize: 统一提取 page/page_size 参数解析、默认值设置、ClampPageSize 调用
- device/log/webhook handler: 替换重复的分页解析代码为 parsePageAndSize 调用
- 清理不再需要的 strconv/pagination 导入
This commit is contained in:
2026-05-08 12:48:03 +08:00
parent b3374dccf4
commit b8e9af001f
4 changed files with 27 additions and 36 deletions

View File

@@ -3,11 +3,9 @@ package handler
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/user-management-system/internal/pagination"
"github.com/user-management-system/internal/service"
)
@@ -43,12 +41,7 @@ func (h *LogHandler) GetMyLoginLogs(c *gin.Context) {
return
}
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)
page, pageSize := parsePageAndSize(c)
logs, total, err := h.loginLogService.GetMyLoginLogs(c.Request.Context(), userID, page, pageSize)
if err != nil {
@@ -86,12 +79,7 @@ func (h *LogHandler) GetMyOperationLogs(c *gin.Context) {
return
}
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)
page, pageSize := parsePageAndSize(c)
logs, total, err := h.operationLogService.GetMyOperationLogs(c.Request.Context(), userID, page, pageSize)
if err != nil {