This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/internal/common/pagination.go
Victor_Jay 91f2cbebc1 fix: 修复帖子提交审核权限缺失 + 防重复提交 + 分页逻辑复用
- 修复 Submit API 缺少 uid 权限检查,任意登录用户可提交他人帖子审核
- 前端 handleSubmit 添加 submitting 锁和按钮禁用/恢复,防止重复提交
- 分页计算 TotalPages/PrevPage/NextPage 提取为 Pagination 方法,消除三处重复代码
2026-05-28 14:47:14 +08:00

54 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common
import "time"
// Pagination 分页请求参数
type Pagination struct {
Page int `form:"page" json:"page" binding:"min=1"`
PageSize int `form:"page_size" json:"page_size" binding:"min=1,max=100"`
}
// DefaultPagination 默认分页(未传参时使用)
func (p *Pagination) DefaultPagination() {
if p.Page < 1 {
p.Page = 1
}
if p.PageSize < 1 || p.PageSize > 100 {
p.PageSize = 20
}
}
// Offset 计算 SQL offset
func (p *Pagination) Offset() int {
return (p.Page - 1) * p.PageSize
}
// TotalPages 根据 total 计算总页数
func (p *Pagination) TotalPages(total int64) int {
if total == 0 {
return 0
}
return int((total + int64(p.PageSize) - 1) / int64(p.PageSize))
}
// PrevPage 上一页(最小为 1
func (p *Pagination) PrevPage() int {
prev := p.Page - 1
if prev < 1 {
return 1
}
return prev
}
// NextPage 下一页(最大为 totalPages
func (p *Pagination) NextPage(totalPages int) int {
next := p.Page + 1
if next > totalPages {
return totalPages
}
return next
}
// 固定时间格式,前后端统一
const TimeFormat = time.RFC3339