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" historyDeletePath = "/api/app/playlet/history/delete" 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"` PlayName string `json:"playName"` 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"` } // PlayletHistoryDeleteReq 对应 swagger proto.PlayletHistoryDeleteReq type PlayletHistoryDeleteReq struct { PlayletID int64 `json:"playletId"` } // PlayletHistoryDeleteResp 对应 swagger proto.PlayletHistoryDeleteResp type PlayletHistoryDeleteResp 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,无请求体。 // 文档:http://172.104.181.99:25319/swagger/index.html#/app短剧相关接口/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 } // HistoryDelete 删除单条短剧观看历史。 // POST /api/app/playlet/history/delete // 文档:http://172.104.181.99:25319/swagger/index.html#/app短剧相关接口/post_api_app_playlet_history_delete func HistoryDelete(c *gin.Context, req PlayletHistoryDeleteReq) (*APIEnvelope[PlayletHistoryDeleteResp], error) { if req.PlayletID <= 0 { return nil, fmt.Errorf("playlet history delete invalid playletId") } var env APIEnvelope[PlayletHistoryDeleteResp] if _, err := upstream.PostJSON(c, historyDeletePath, req, &env); err != nil { return nil, err } return &env, nil }