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 bacfd4945d feat: 编辑器与帖子展示布局优化 + 新增个人空间
- 编辑器投稿页布局优化:标题与编辑器高度动态适配,工具栏与内容区视觉对齐
- 稿件展示页布局优化:MD 渲染区与评论区视觉分离,代码块主题微调
- CSRF 中间件:图像上传端点豁免,解决 Vditor 拖拽/粘贴上传 403
- Post 状态映射、GetGinUser、SaveUploadedFile 提取至 common 包,遵循审计建议
- 新增个人空间功能:/space(需登录)重定向到 /space/:uid,/space/:uid 公开访问
- 空间页模板:参考 B 站布局,展示头像、用户名、个性签名、UID、加入时间及稿件列表
- 导航栏:用户名链接指向 /space,新增「设置」入口
- 分层实现:SpaceController → SpaceService → PostRepo.FindByUserID,严格 ISP 接口隔离
2026-05-30 19:06:10 +08:00

62 lines
1.3 KiB
Go
Raw Permalink 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
}
// PageCount 根据 total 和 pageSize 计算总页数(无需创建 Pagination 实例)
func PageCount(total int64, pageSize int) int {
if total == 0 {
return 0
}
return int((total + int64(pageSize) - 1) / int64(pageSize))
}
// 固定时间格式,前后端统一
const TimeFormat = time.RFC3339