83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package playletser
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func hotRankPageURL(rankType, pageNum int) string {
|
|
switch rankType {
|
|
case hotRankNew, hotRankBinge:
|
|
default:
|
|
rankType = hotRankHot
|
|
}
|
|
if pageNum <= 0 {
|
|
pageNum = 1
|
|
}
|
|
path := "/popular-recommend"
|
|
if rankType != hotRankHot {
|
|
path += "/" + strconv.Itoa(rankType)
|
|
}
|
|
if pageNum > 1 {
|
|
path += "/page/" + strconv.Itoa(pageNum)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// HotRankPageURL 生成热榜页链接(供模板使用)。
|
|
func HotRankPageURL(rankType, pageNum int) string {
|
|
return hotRankPageURL(rankType, pageNum)
|
|
}
|
|
|
|
// BuildHotRankTabs 热榜类型 Tab。
|
|
func BuildHotRankTabs(q HotRankQuery) []FilterChipVM {
|
|
q.Normalize()
|
|
return []FilterChipVM{
|
|
{Label: "热播榜", URL: hotRankPageURL(hotRankHot, 1), Active: q.RankType == hotRankHot},
|
|
{Label: "新剧榜", URL: hotRankPageURL(hotRankNew, 1), Active: q.RankType == hotRankNew},
|
|
{Label: "追剧榜", URL: hotRankPageURL(hotRankBinge, 1), Active: q.RankType == hotRankBinge},
|
|
}
|
|
}
|
|
|
|
// BuildHotRankPagination 热榜分页。
|
|
func BuildHotRankPagination(q HotRankQuery, totalCount int64) PaginationVM {
|
|
q.Normalize()
|
|
return BuildPagination(func(p int) string {
|
|
return hotRankPageURL(q.RankType, p)
|
|
}, q.PageNum, q.PageSize, totalCount)
|
|
}
|
|
|
|
// HotRankCanonicalPath 返回热榜筛选和分页对应的唯一规范路径。
|
|
func HotRankCanonicalPath(q HotRankQuery) string {
|
|
q.Normalize()
|
|
return hotRankPageURL(q.RankType, q.PageNum)
|
|
}
|
|
|
|
// ParseHotRankQuery 从路径参数解析热榜筛选。
|
|
// 路由:/popular-recommend/ 、/popular-recommend/page/:page/ 、/popular-recommend/:rankType/ 、/popular-recommend/:rankType/page/:page/
|
|
func ParseHotRankQuery(c *gin.Context) HotRankQuery {
|
|
q := HotRankQuery{PageSize: defaultHotRankPageSize}
|
|
if n, err := strconv.Atoi(strings.TrimSpace(c.Param("rankType"))); err == nil {
|
|
q.RankType = n
|
|
}
|
|
if n, err := strconv.Atoi(strings.TrimSpace(c.Param("page"))); err == nil {
|
|
q.PageNum = n
|
|
}
|
|
q.Normalize()
|
|
return q
|
|
}
|
|
|
|
// HotRankTabTitle 当前热榜标题。
|
|
func HotRankTabTitle(rankType int) string {
|
|
switch rankType {
|
|
case hotRankNew:
|
|
return "新剧榜"
|
|
case hotRankBinge:
|
|
return "追剧榜"
|
|
default:
|
|
return "热播榜"
|
|
}
|
|
}
|