Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package playletser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"videoweb/app/service/upstream"
|
||||
"videoweb/pub/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
recommendListPath = "/api/app/playlet/list"
|
||||
defaultRecommendSize = 25
|
||||
)
|
||||
|
||||
// RecommendQuery 专题详情分页参数。
|
||||
type RecommendQuery struct {
|
||||
PageNum int
|
||||
PageSize int
|
||||
SortOrder int
|
||||
ThemeID int64
|
||||
ThemeSlug string
|
||||
}
|
||||
|
||||
// Normalize 校正分页默认值。
|
||||
func (q *RecommendQuery) Normalize() {
|
||||
if q.PageNum <= 0 {
|
||||
q.PageNum = 1
|
||||
}
|
||||
if q.PageSize <= 0 {
|
||||
q.PageSize = defaultRecommendSize
|
||||
}
|
||||
if q.PageSize > 100 {
|
||||
q.PageSize = 100
|
||||
}
|
||||
if q.SortOrder != sortTodayUpdate {
|
||||
q.SortOrder = sortHeat
|
||||
}
|
||||
}
|
||||
|
||||
func recommendListURL(q RecommendQuery) string {
|
||||
q.Normalize()
|
||||
params := url.Values{}
|
||||
params.Set("pageNum", strconv.Itoa(q.PageNum))
|
||||
params.Set("pageSize", strconv.Itoa(q.PageSize))
|
||||
params.Set("sortOrder", strconv.Itoa(q.SortOrder))
|
||||
if slug := strings.TrimSpace(q.ThemeSlug); slug != "" {
|
||||
params.Set("themeSlug", slug)
|
||||
} else if q.ThemeID > 0 {
|
||||
params.Set("themeId", strconv.FormatInt(q.ThemeID, 10))
|
||||
}
|
||||
return recommendListPath + "?" + params.Encode()
|
||||
}
|
||||
|
||||
// FetchRecommendList 按 Swagger 的题材 ID 或 slug 参数分页查询专题短剧。
|
||||
func FetchRecommendList(c *gin.Context, q RecommendQuery) (*PlayletVideoResp, error) {
|
||||
var env APIEnvelope[PlayletVideoResp]
|
||||
if err := upstream.GetPublicJSON(c, recommendListURL(q), &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if env.Code != 200 {
|
||||
return nil, fmt.Errorf("playlet recommend list code=%d msg=%s", env.Code, env.Msg)
|
||||
}
|
||||
return &env.Data, nil
|
||||
}
|
||||
|
||||
// FetchRecommendListOrEmpty 失败时返回空列表。
|
||||
func FetchRecommendListOrEmpty(c *gin.Context, q RecommendQuery) *PlayletVideoResp {
|
||||
q.Normalize()
|
||||
data, err := FetchRecommendList(c, q)
|
||||
if err != nil {
|
||||
log.Error("fetch playlet recommend list failed", log.E(err))
|
||||
return &PlayletVideoResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
||||
}
|
||||
if data == nil {
|
||||
return &PlayletVideoResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
||||
}
|
||||
data.SeoInfo.Normalize()
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user