Files
web_huangguo2_h5/app/service/playletser/theme_index.go
T
谢宇宁andClaude Opus 5.5 0fbdba5ccf Initial commit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 11:47:47 +07:00

73 lines
1.7 KiB
Go

package playletser
import (
"strconv"
"strings"
)
const defaultThemeIndexPageSize = 20
// ThemeIndexQuery 专题列表页分页参数。
type ThemeIndexQuery struct {
PageNum int
PageSize int
}
func (q *ThemeIndexQuery) Normalize() {
if q.PageNum <= 0 {
q.PageNum = 1
}
if q.PageSize <= 0 {
q.PageSize = defaultThemeIndexPageSize
}
}
// ParseThemeIndexQuery 从语义化分页路由读取页码。
func ParseThemeIndexQuery(page string) ThemeIndexQuery {
q := ThemeIndexQuery{PageSize: defaultThemeIndexPageSize}
if n, err := strconv.Atoi(strings.TrimSpace(page)); err == nil {
q.PageNum = n
}
q.Normalize()
return q
}
// BuildThemeIndexPage 过滤已启用专题,并按设计稿每页 20 项分页。
func BuildThemeIndexPage(themes []PlayletThemeItem, q ThemeIndexQuery) ([]PlayletThemeItem, int64) {
q.Normalize()
visible := make([]PlayletThemeItem, 0, len(themes))
for _, theme := range themes {
if !theme.Status || theme.ID <= 0 || strings.TrimSpace(theme.Name) == "" {
continue
}
visible = append(visible, theme)
}
total := int64(len(visible))
start := (q.PageNum - 1) * q.PageSize
if start >= len(visible) {
return nil, total
}
end := start + q.PageSize
if end > len(visible) {
end = len(visible)
}
return visible[start:end], total
}
func themeIndexPageURL(page int) string {
if page <= 1 {
return "/topic"
}
return "/topic/page/" + strconv.Itoa(page)
}
func ThemeIndexCanonicalPath(q ThemeIndexQuery) string {
q.Normalize()
return themeIndexPageURL(q.PageNum)
}
func BuildThemeIndexPagination(q ThemeIndexQuery, totalCount int64) PaginationVM {
q.Normalize()
return BuildPagination(themeIndexPageURL, q.PageNum, q.PageSize, totalCount)
}