142 lines
4.4 KiB
Go
142 lines
4.4 KiB
Go
package videoser
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strconv"
|
|
"videoweb/app/service/authser"
|
|
"videoweb/app/service/upstream"
|
|
"videoweb/pub/common/log"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
videoHistoryListPath = "/api/app/video/history/list"
|
|
videoHistoryClearPath = "/api/app/video/history/clear"
|
|
videoHistoryDeletePath = "/api/app/video/history/delete"
|
|
defaultHistoryPageSize = 40
|
|
)
|
|
|
|
// VideoHistoryData 对应 swagger proto.VideoHistoryData。
|
|
// GET /api/app/video/history/list
|
|
type VideoHistoryData struct {
|
|
ID int64 `json:"id"`
|
|
Title string `json:"title"`
|
|
Slug string `json:"slug"`
|
|
CoverImg string `json:"coverImg"`
|
|
Keyworld []string `json:"keyworld"`
|
|
Tags []int64 `json:"tags"`
|
|
VideoTime int `json:"videoTime"`
|
|
WatchTimes int64 `json:"watchTimes"`
|
|
CountLike int64 `json:"countLike"`
|
|
CountCollect int64 `json:"countCollect"`
|
|
CountComment int64 `json:"countComment"`
|
|
IsCollect int `json:"isCollect"`
|
|
HistoryAt string `json:"historyAt"`
|
|
}
|
|
|
|
// VideoHistoryListResp 对应 swagger proto.VideoHistoryListResp。
|
|
type VideoHistoryListResp struct {
|
|
List []VideoHistoryData `json:"list"`
|
|
PageNum int `json:"pageNum"`
|
|
PageSize int `json:"pageSize"`
|
|
TotalCount int64 `json:"totalCount"`
|
|
}
|
|
|
|
// VideoHistoryClearResp 对应 swagger proto.VideoHistoryClearResp。
|
|
type VideoHistoryClearResp struct {
|
|
DeletedCount int `json:"deletedCount"`
|
|
}
|
|
|
|
// VideoHistoryDeleteReq 对应 swagger proto.VideoHistoryDeleteReq。
|
|
type VideoHistoryDeleteReq struct {
|
|
VideoID int64 `json:"videoId"`
|
|
}
|
|
|
|
// VideoHistoryDeleteResp 对应 swagger proto.VideoHistoryDeleteResp。
|
|
type VideoHistoryDeleteResp 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 分页查询视频观看历史:GET /api/app/video/history/list。
|
|
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/get_api_app_video_history_list
|
|
func FetchHistoryList(c *gin.Context, q HistoryListQuery) (*VideoHistoryListResp, int, error) {
|
|
q.Normalize()
|
|
params := url.Values{}
|
|
params.Set("pageNum", strconv.Itoa(q.PageNum))
|
|
params.Set("pageSize", strconv.Itoa(q.PageSize))
|
|
|
|
var env APIEnvelope[VideoHistoryListResp]
|
|
if err := upstream.GetJSON(c, videoHistoryListPath+"?"+params.Encode(), &env); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if env.Code != 200 {
|
|
return nil, env.Code, fmt.Errorf("video history list code=%d msg=%s", env.Code, env.Msg)
|
|
}
|
|
return &env.Data, env.Code, nil
|
|
}
|
|
|
|
// FetchHistoryListOrEmpty 失败返回空列表;游客会话失效时尝试重登后重试一次。
|
|
func FetchHistoryListOrEmpty(c *gin.Context, q HistoryListQuery) *VideoHistoryListResp {
|
|
data, code, err := FetchHistoryList(c, q)
|
|
if err == nil {
|
|
if data == nil {
|
|
return &VideoHistoryListResp{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 &VideoHistoryListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
|
}
|
|
return data
|
|
}
|
|
}
|
|
log.Error("fetch video history list failed", log.E(err))
|
|
return &VideoHistoryListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
|
}
|
|
|
|
// HistoryClear 清空视频观看历史:POST /api/app/video/history/clear。
|
|
func HistoryClear(c *gin.Context) (*APIEnvelope[VideoHistoryClearResp], error) {
|
|
var env APIEnvelope[VideoHistoryClearResp]
|
|
if _, err := upstream.PostJSON(c, videoHistoryClearPath, nil, &env); err != nil {
|
|
return nil, err
|
|
}
|
|
return &env, nil
|
|
}
|
|
|
|
// HistoryDelete 删除单条视频观看历史:POST /api/app/video/history/delete。
|
|
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/post_api_app_video_history_delete
|
|
func HistoryDelete(c *gin.Context, req VideoHistoryDeleteReq) (*APIEnvelope[VideoHistoryDeleteResp], error) {
|
|
if req.VideoID <= 0 {
|
|
return nil, fmt.Errorf("video history delete invalid videoId")
|
|
}
|
|
var env APIEnvelope[VideoHistoryDeleteResp]
|
|
if _, err := upstream.PostJSON(c, videoHistoryDeletePath, req, &env); err != nil {
|
|
return nil, err
|
|
}
|
|
return &env, nil
|
|
}
|