Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
73 lines
1.7 KiB
Go
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)
|
|
}
|