Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
140 lines
4.5 KiB
Go
140 lines
4.5 KiB
Go
package playletser
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
"videoweb/app/service/authser"
|
|
"videoweb/app/service/upstream"
|
|
"videoweb/pub/common/log"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
historyListPath = "/api/app/playlet/history/list"
|
|
historyClearPath = "/api/app/playlet/history/clear"
|
|
defaultHistoryPageSize = 40
|
|
)
|
|
|
|
// PlayletHistoryData 对应 swagger proto.PlayletHistoryData
|
|
// GET /api/app/playlet/history/list
|
|
type PlayletHistoryData struct {
|
|
ID int64 `json:"id"`
|
|
Status int `json:"status"`
|
|
IsFeatured bool `json:"isFeatured"`
|
|
IsNew bool `json:"isNew"`
|
|
IsRecommend bool `json:"isRecommend"`
|
|
ShowOnBanner bool `json:"showOnBanner"`
|
|
Banner string `json:"banner"`
|
|
OnlineAt string `json:"onlineAt"`
|
|
MediaCreatedAt string `json:"mediaCreatedAt"`
|
|
MediaUpdatedAt string `json:"mediaUpdatedAt"`
|
|
ThemeIDs []int64 `json:"themeIds"`
|
|
ThemeNames []string `json:"themeNames"`
|
|
ThemeSlugs []string `json:"themeSlugs"`
|
|
Title string `json:"title"`
|
|
Slug string `json:"slug"`
|
|
Desc string `json:"desc"`
|
|
Anchor string `json:"anchor"`
|
|
Cover string `json:"cover"`
|
|
TagIDs []int64 `json:"tagIds"`
|
|
TagNames []string `json:"tagNames"`
|
|
TagSlugs []string `json:"tagSlugs"`
|
|
TotalEpisode int `json:"totalEpisode"`
|
|
UpdateStatus int `json:"updateStatus"`
|
|
CountBrowse int64 `json:"countBrowse"`
|
|
CountCollect int64 `json:"countCollect"`
|
|
CountLike int64 `json:"countLike"`
|
|
CreatedAt string `json:"createdAt"`
|
|
UpdatedAt string `json:"updatedAt"`
|
|
WatchedCount int `json:"watchedCount"`
|
|
UnwatchedCount int `json:"unwatchedCount"`
|
|
LastChapterID int64 `json:"lastChapterId"`
|
|
LastEpisode int `json:"lastEpisode"`
|
|
IsFinished bool `json:"isFinished"`
|
|
HistoryAt string `json:"historyAt"`
|
|
}
|
|
|
|
// PlayletHistoryListResp 对应 swagger proto.PlayletHistoryListResp
|
|
type PlayletHistoryListResp struct {
|
|
List []PlayletHistoryData `json:"list"`
|
|
PageNum int `json:"pageNum"`
|
|
PageSize int `json:"pageSize"`
|
|
TotalCount int64 `json:"totalCount"`
|
|
}
|
|
|
|
// PlayletHistoryClearResp 对应 swagger proto.PlayletHistoryClearResp
|
|
type PlayletHistoryClearResp struct {
|
|
DeletedCount int `json:"deletedCount"`
|
|
}
|
|
|
|
// HistoryListQuery 观看历史分页参数。
|
|
type HistoryListQuery struct {
|
|
PageNum int
|
|
PageSize int
|
|
}
|
|
|
|
// Normalize 校正分页默认值。
|
|
func (q *HistoryListQuery) Normalize() {
|
|
if q.PageNum <= 0 {
|
|
q.PageNum = 1
|
|
}
|
|
if q.PageSize <= 0 {
|
|
q.PageSize = defaultHistoryPageSize
|
|
}
|
|
if q.PageSize > 100 {
|
|
q.PageSize = 100
|
|
}
|
|
}
|
|
|
|
// FetchHistoryList 分页查询当前用户观看历史。
|
|
// 第二个返回值为上游业务 code(网络错误时为 0)。
|
|
func FetchHistoryList(c *gin.Context, q HistoryListQuery) (*PlayletHistoryListResp, int, error) {
|
|
q.Normalize()
|
|
params := url.Values{}
|
|
params.Set("pageNum", strconv.Itoa(q.PageNum))
|
|
params.Set("pageSize", strconv.Itoa(q.PageSize))
|
|
|
|
var env APIEnvelope[PlayletHistoryListResp]
|
|
if err := upstream.GetJSON(c, historyListPath+"?"+params.Encode(), &env); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if env.Code != 200 {
|
|
return nil, env.Code, fmt.Errorf("playlet history list code=%d msg=%s", env.Code, env.Msg)
|
|
}
|
|
return &env.Data, env.Code, nil
|
|
}
|
|
|
|
// FetchHistoryListOrEmpty 失败时返回空列表;游客会话失效时尝试重登后重试一次。
|
|
func FetchHistoryListOrEmpty(c *gin.Context, q HistoryListQuery) *PlayletHistoryListResp {
|
|
data, code, err := FetchHistoryList(c, q)
|
|
if err == nil {
|
|
if data == nil {
|
|
return &PlayletHistoryListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
|
}
|
|
return data
|
|
}
|
|
if retry, _ := authser.OnSessionExpired(c, code); retry {
|
|
data, _, err = FetchHistoryList(c, q)
|
|
if err == nil {
|
|
if data == nil {
|
|
return &PlayletHistoryListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
|
}
|
|
return data
|
|
}
|
|
}
|
|
log.Error("fetch playlet history list failed", log.E(err))
|
|
return &PlayletHistoryListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
|
}
|
|
|
|
// HistoryClear 清空当前用户观看历史。
|
|
// POST /api/app/playlet/history/clear,无请求体。
|
|
func HistoryClear(c *gin.Context) (*APIEnvelope[PlayletHistoryClearResp], error) {
|
|
var env APIEnvelope[PlayletHistoryClearResp]
|
|
if _, err := upstream.PostJSON(c, historyClearPath, nil, &env); err != nil {
|
|
return nil, err
|
|
}
|
|
return &env, nil
|
|
}
|