@@ -0,0 +1,130 @@
|
||||
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 (
|
||||
collectSetPath = "/api/app/video/collect/set"
|
||||
collectListPath = "/api/app/video/collect/list"
|
||||
defaultCollectPageSize = 40
|
||||
)
|
||||
|
||||
// VideoCollectSetReq 对应 swagger proto.VideoCollectSetReq。
|
||||
// POST /api/app/video/collect/set
|
||||
type VideoCollectSetReq struct {
|
||||
ID int64 `json:"id"`
|
||||
IsCollect bool `json:"isCollect"`
|
||||
}
|
||||
|
||||
// VideoCollectSetResp 对应 swagger proto.VideoCollectSetResp。
|
||||
type VideoCollectSetResp struct {
|
||||
ID int64 `json:"id"`
|
||||
CountCollect int64 `json:"countCollect"`
|
||||
IsCollect int `json:"isCollect"` // 1-已收藏 2-未收藏
|
||||
}
|
||||
|
||||
// VideoCollectData 对应 swagger proto.VideoCollectData。
|
||||
// GET /api/app/video/collect/list
|
||||
type VideoCollectData 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"`
|
||||
CollectedAt string `json:"collectedAt"`
|
||||
}
|
||||
|
||||
// VideoCollectListResp 对应 swagger proto.VideoCollectListResp。
|
||||
type VideoCollectListResp struct {
|
||||
List []VideoCollectData `json:"list"`
|
||||
PageNum int `json:"pageNum"`
|
||||
PageSize int `json:"pageSize"`
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
}
|
||||
|
||||
// CollectListQuery 视频收藏列表分页。
|
||||
type CollectListQuery struct {
|
||||
PageNum int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
// Normalize 校正分页默认值。
|
||||
func (q *CollectListQuery) Normalize() {
|
||||
if q.PageNum <= 0 {
|
||||
q.PageNum = 1
|
||||
}
|
||||
if q.PageSize <= 0 {
|
||||
q.PageSize = defaultCollectPageSize
|
||||
}
|
||||
if q.PageSize > 100 {
|
||||
q.PageSize = 100
|
||||
}
|
||||
}
|
||||
|
||||
// CollectSet 设置视频收藏/取消收藏。
|
||||
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/post_api_app_video_collect_set
|
||||
func CollectSet(c *gin.Context, req VideoCollectSetReq) (*APIEnvelope[VideoCollectSetResp], error) {
|
||||
if req.ID <= 0 {
|
||||
return nil, fmt.Errorf("video collect invalid id")
|
||||
}
|
||||
var env APIEnvelope[VideoCollectSetResp]
|
||||
if _, err := upstream.PostJSON(c, collectSetPath, req, &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &env, nil
|
||||
}
|
||||
|
||||
// FetchCollectList 拉取视频收藏列表:GET /api/app/video/collect/list。
|
||||
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/get_api_app_video_collect_list
|
||||
func FetchCollectList(c *gin.Context, q CollectListQuery) (*VideoCollectListResp, int, error) {
|
||||
q.Normalize()
|
||||
params := url.Values{}
|
||||
params.Set("pageNum", strconv.Itoa(q.PageNum))
|
||||
params.Set("pageSize", strconv.Itoa(q.PageSize))
|
||||
|
||||
var env APIEnvelope[VideoCollectListResp]
|
||||
if err := upstream.GetJSON(c, collectListPath+"?"+params.Encode(), &env); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if env.Code != 200 {
|
||||
return nil, env.Code, fmt.Errorf("video collect list code=%d msg=%s", env.Code, env.Msg)
|
||||
}
|
||||
return &env.Data, env.Code, nil
|
||||
}
|
||||
|
||||
// FetchCollectListOrEmpty 失败返回空列表;游客会话失效时尝试重登后重试一次。
|
||||
func FetchCollectListOrEmpty(c *gin.Context, q CollectListQuery) *VideoCollectListResp {
|
||||
data, code, err := FetchCollectList(c, q)
|
||||
if err == nil {
|
||||
if data == nil {
|
||||
return &VideoCollectListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
||||
}
|
||||
return data
|
||||
}
|
||||
if retry, _ := authser.OnSessionExpired(c, code); retry {
|
||||
data, _, err = FetchCollectList(c, q)
|
||||
if err == nil {
|
||||
if data == nil {
|
||||
return &VideoCollectListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
||||
}
|
||||
return data
|
||||
}
|
||||
}
|
||||
log.Error("fetch video collect list failed", log.E(err))
|
||||
return &VideoCollectListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"videoweb/app/service/upstream"
|
||||
"videoweb/pub/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const videoDetailPath = "/api/app/video/detail"
|
||||
|
||||
// FetchVideoDetail 拉取 GET /api/app/video/detail。
|
||||
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/get_api_app_video_detail
|
||||
func FetchVideoDetail(c *gin.Context, id int64, slug string) (*VideoInfoResp, error) {
|
||||
params := url.Values{}
|
||||
slug = strings.TrimSpace(slug)
|
||||
if slug != "" {
|
||||
params.Set("slug", slug)
|
||||
} else if id > 0 {
|
||||
params.Set("id", strconv.FormatInt(id, 10))
|
||||
} else {
|
||||
return nil, fmt.Errorf("video detail missing id/slug")
|
||||
}
|
||||
|
||||
var env APIEnvelope[VideoInfoResp]
|
||||
if err := upstream.GetJSON(c, videoDetailPath+"?"+params.Encode(), &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if env.Code != 200 {
|
||||
return nil, fmt.Errorf("video detail code=%d msg=%s tip=%s", env.Code, env.Msg, env.Tip)
|
||||
}
|
||||
return &env.Data, nil
|
||||
}
|
||||
|
||||
// FetchVideoDetailByKey 路径段为纯数字时按 id,否则按 slug。
|
||||
func FetchVideoDetailByKey(c *gin.Context, key string) (*VideoInfoResp, error) {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
return nil, fmt.Errorf("video detail missing key")
|
||||
}
|
||||
if id, err := strconv.ParseInt(key, 10, 64); err == nil && id > 0 && key == strconv.FormatInt(id, 10) {
|
||||
return FetchVideoDetail(c, id, "")
|
||||
}
|
||||
return FetchVideoDetail(c, 0, key)
|
||||
}
|
||||
|
||||
// FetchVideoDetailOrNilByKey 失败时返回 nil。
|
||||
func FetchVideoDetailOrNilByKey(c *gin.Context, key string) *VideoInfoResp {
|
||||
data, err := FetchVideoDetailByKey(c, key)
|
||||
if err != nil {
|
||||
log.Error("fetch video detail failed", log.E(err), log.Any("key", key))
|
||||
return nil
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// ResolveVideoPageTitle 播放页标题。
|
||||
func ResolveVideoPageTitle(v *VideoInfoResp) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
if t := strings.TrimSpace(v.SeoInfo.Title); t != "" {
|
||||
return t
|
||||
}
|
||||
return strings.TrimSpace(v.Title)
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"videoweb/app/service/upstream"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const likeSetPath = "/api/app/video/like/set"
|
||||
|
||||
// VideoLikeSetReq 对应 swagger proto.VideoLikeSetReq。
|
||||
// POST /api/app/video/like/set
|
||||
type VideoLikeSetReq struct {
|
||||
ID int64 `json:"id"`
|
||||
IsLike bool `json:"isLike"`
|
||||
}
|
||||
|
||||
// VideoLikeSetResp 对应 swagger proto.VideoLikeSetResp。
|
||||
type VideoLikeSetResp struct {
|
||||
ID int64 `json:"id"`
|
||||
CountLike int64 `json:"countLike"`
|
||||
IsLike int `json:"isLike"` // 1-已点赞 2-未点赞
|
||||
}
|
||||
|
||||
// LikeSet 设置视频点赞/取消点赞。
|
||||
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/post_api_app_video_like_set
|
||||
func LikeSet(c *gin.Context, req VideoLikeSetReq) (*APIEnvelope[VideoLikeSetResp], error) {
|
||||
if req.ID <= 0 {
|
||||
return nil, fmt.Errorf("video like invalid id")
|
||||
}
|
||||
var env APIEnvelope[VideoLikeSetResp]
|
||||
if _, err := upstream.PostJSON(c, likeSetPath, req, &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &env, nil
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"videoweb/app/service/playletser"
|
||||
"videoweb/app/service/upstream"
|
||||
"videoweb/pub/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const videoAllListPath = "/api/app/video/all/list"
|
||||
|
||||
// FetchVideoAllList 拉取 GET /api/app/video/all/list。
|
||||
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/get_api_app_video_all_list
|
||||
func FetchVideoAllList(c *gin.Context, q SeriesListQuery) (*VideoAllListResp, error) {
|
||||
q.Normalize()
|
||||
params := url.Values{}
|
||||
params.Set("pageNum", strconv.Itoa(q.PageNum))
|
||||
params.Set("pageSize", strconv.Itoa(q.PageSize))
|
||||
params.Set("mediaType", strconv.Itoa(q.MediaType))
|
||||
params.Set("sortType", strconv.Itoa(q.SortType))
|
||||
if q.TopicID > 0 {
|
||||
params.Set("topicId", strconv.FormatInt(q.TopicID, 10))
|
||||
}
|
||||
|
||||
var env APIEnvelope[VideoAllListResp]
|
||||
if err := upstream.GetPublicJSON(c, videoAllListPath+"?"+params.Encode(), &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if env.Code != 200 {
|
||||
return nil, fmt.Errorf("video all list code=%d msg=%s", env.Code, env.Msg)
|
||||
}
|
||||
return &env.Data, nil
|
||||
}
|
||||
|
||||
// FetchVideoAllListOrEmpty 失败时返回空列表。
|
||||
func FetchVideoAllListOrEmpty(c *gin.Context, q SeriesListQuery) *VideoAllListResp {
|
||||
data, err := FetchVideoAllList(c, q)
|
||||
if err != nil {
|
||||
log.Error("fetch video all list failed", log.E(err))
|
||||
return &VideoAllListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
||||
}
|
||||
if data == nil {
|
||||
return &VideoAllListResp{PageNum: q.PageNum, PageSize: q.PageSize}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// BuildSeriesListPagination 剧集列表分页。
|
||||
func BuildSeriesListPagination(q SeriesListQuery, totalCount int64) playletser.PaginationVM {
|
||||
q.Normalize()
|
||||
return playletser.BuildPagination(func(p int) string {
|
||||
nq := q
|
||||
nq.PageNum = p
|
||||
return nq.URL()
|
||||
}, q.PageNum, q.PageSize, totalCount)
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"videoweb/app/service/upstream"
|
||||
"videoweb/pub/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
videoTopicListPath = "/api/app/video/topic/list"
|
||||
|
||||
// mediaType:0全部 1影集 2动画
|
||||
MediaAll = 0
|
||||
MediaFilm = 1
|
||||
MediaAnime = 2
|
||||
|
||||
// sortType:1最新 2最热 3收藏最多
|
||||
SortNewest = 1
|
||||
SortHeat = 2
|
||||
SortCollect = 3
|
||||
|
||||
defaultSeriesPageSize = 16
|
||||
)
|
||||
|
||||
// SeriesListQuery 剧集页筛选:对应 video/all/list + topic/list 的 mediaType。
|
||||
type SeriesListQuery struct {
|
||||
MediaType int
|
||||
TopicID int64
|
||||
SortType int
|
||||
PageNum int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
// Normalize 校正默认值。
|
||||
func (q *SeriesListQuery) Normalize() {
|
||||
if q.PageNum <= 0 {
|
||||
q.PageNum = 1
|
||||
}
|
||||
if q.PageSize <= 0 {
|
||||
q.PageSize = defaultSeriesPageSize
|
||||
}
|
||||
if q.PageSize > 50 {
|
||||
q.PageSize = 50
|
||||
}
|
||||
switch q.MediaType {
|
||||
case MediaFilm, MediaAnime:
|
||||
default:
|
||||
q.MediaType = MediaAll
|
||||
}
|
||||
switch q.SortType {
|
||||
case SortHeat, SortCollect:
|
||||
default:
|
||||
q.SortType = SortNewest
|
||||
}
|
||||
if q.TopicID < 0 {
|
||||
q.TopicID = 0
|
||||
}
|
||||
}
|
||||
|
||||
// ParseSeriesListQuery 从路径解析剧集筛选。
|
||||
// 路由:/series/ 、/series/:media/:topic/:sortType/ 、
|
||||
//
|
||||
// /series/:media/:topic/:sortType/page/:page/
|
||||
func ParseSeriesListQuery(c *gin.Context) SeriesListQuery {
|
||||
q := SeriesListQuery{PageSize: defaultSeriesPageSize}
|
||||
q.MediaType = parseMediaSegment(c.Param("media"))
|
||||
topic := strings.TrimSpace(c.Param("topic"))
|
||||
if topic != "" && !strings.EqualFold(topic, "all") {
|
||||
if id, err := strconv.ParseInt(topic, 10, 64); err == nil && id > 0 {
|
||||
q.TopicID = id
|
||||
}
|
||||
}
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(c.Param("sortType"))); err == nil {
|
||||
q.SortType = n
|
||||
}
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(c.Param("page"))); err == nil {
|
||||
q.PageNum = n
|
||||
}
|
||||
q.Normalize()
|
||||
return q
|
||||
}
|
||||
|
||||
func parseMediaSegment(raw string) int {
|
||||
raw = strings.TrimSpace(strings.ToLower(raw))
|
||||
switch raw {
|
||||
case "", "all", "0":
|
||||
return MediaAll
|
||||
case "film", "1", "yingji":
|
||||
return MediaFilm
|
||||
case "anime", "2", "donghua":
|
||||
return MediaAnime
|
||||
default:
|
||||
if n, err := strconv.Atoi(raw); err == nil {
|
||||
return n
|
||||
}
|
||||
return MediaAll
|
||||
}
|
||||
}
|
||||
|
||||
func mediaSegment(mediaType int) string {
|
||||
switch mediaType {
|
||||
case MediaFilm:
|
||||
return "film"
|
||||
case MediaAnime:
|
||||
return "anime"
|
||||
default:
|
||||
return "all"
|
||||
}
|
||||
}
|
||||
|
||||
func topicSegment(topicID int64) string {
|
||||
if topicID > 0 {
|
||||
return strconv.FormatInt(topicID, 10)
|
||||
}
|
||||
return "all"
|
||||
}
|
||||
|
||||
// SeriesListURL 生成剧集页筛选链接(纯路径)。
|
||||
func SeriesListURL(mediaType int, topicID int64, sortType, pageNum int) string {
|
||||
q := SeriesListQuery{
|
||||
MediaType: mediaType,
|
||||
TopicID: topicID,
|
||||
SortType: sortType,
|
||||
PageNum: pageNum,
|
||||
PageSize: defaultSeriesPageSize,
|
||||
}
|
||||
q.Normalize()
|
||||
return q.URL()
|
||||
}
|
||||
|
||||
// URL 输出:/series/ 或 /series/{media}/{topic}/{sort}/[page/{n}/]
|
||||
func (q SeriesListQuery) URL() string {
|
||||
q.Normalize()
|
||||
defaults := q.MediaType == MediaAll && q.TopicID == 0 && q.SortType == SortNewest
|
||||
if defaults && q.PageNum <= 1 {
|
||||
return "/series"
|
||||
}
|
||||
path := "/series/" + mediaSegment(q.MediaType) + "/" + topicSegment(q.TopicID) + "/" + strconv.Itoa(q.SortType)
|
||||
if q.PageNum > 1 {
|
||||
path += "/page/" + strconv.Itoa(q.PageNum)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// FetchVideoTopicList 拉取 GET /api/app/video/topic/list。
|
||||
// 文档:http://172.104.181.99:25319/swagger/index.html#/视频相关接口/get_api_app_video_topic_list
|
||||
func FetchVideoTopicList(c *gin.Context, mediaType int) ([]VideoTopicInfo, error) {
|
||||
switch mediaType {
|
||||
case MediaFilm, MediaAnime:
|
||||
default:
|
||||
mediaType = MediaAll
|
||||
}
|
||||
params := url.Values{}
|
||||
params.Set("mediaType", strconv.Itoa(mediaType))
|
||||
|
||||
var env APIEnvelope[VideoTopicListResp]
|
||||
if err := upstream.GetPublicJSON(c, videoTopicListPath+"?"+params.Encode(), &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if env.Code != 200 {
|
||||
return nil, fmt.Errorf("video topic list code=%d msg=%s", env.Code, env.Msg)
|
||||
}
|
||||
return env.Data.List, nil
|
||||
}
|
||||
|
||||
// FetchVideoTopicListOrEmpty 失败时返回空题材。
|
||||
func FetchVideoTopicListOrEmpty(c *gin.Context, mediaType int) []VideoTopicInfo {
|
||||
list, err := FetchVideoTopicList(c, mediaType)
|
||||
if err != nil {
|
||||
log.Error("fetch video topic list failed", log.E(err))
|
||||
return nil
|
||||
}
|
||||
out := make([]VideoTopicInfo, 0, len(list))
|
||||
for _, item := range list {
|
||||
if item.ID <= 0 || item.DisplayName() == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"videoweb/app/service/configser"
|
||||
)
|
||||
|
||||
// APIEnvelope 上游统一响应壳。
|
||||
type APIEnvelope[T any] struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Tip string `json:"tip"`
|
||||
Data T `json:"data"`
|
||||
}
|
||||
|
||||
// VideoTopicInfo 对应 swagger proto.VideoTopicInfo。
|
||||
type VideoTopicInfo struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Desc string `json:"desc"`
|
||||
Image string `json:"image"`
|
||||
VideoCategoryID int64 `json:"videoCategoryId"`
|
||||
Works int64 `json:"works"`
|
||||
SeoInfo SeoValue `json:"seoInfo"`
|
||||
}
|
||||
|
||||
// SeoValue 对应 swagger pubdef.SeoValue。
|
||||
type SeoValue struct {
|
||||
Title string `json:"title"`
|
||||
Keyworld []string `json:"keyworld"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// VideoTopicListResp 对应 swagger proto.VideoTopicListResp。
|
||||
// GET /api/app/video/topic/list
|
||||
type VideoTopicListResp struct {
|
||||
List []VideoTopicInfo `json:"list"`
|
||||
}
|
||||
|
||||
// VideoAllListItemResp 对应 swagger proto.VideoAllListItemResp。
|
||||
type VideoAllListItemResp struct {
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Slug string `json:"slug"`
|
||||
CoverImg string `json:"coverImg"`
|
||||
CreatorID int64 `json:"creatorId"`
|
||||
CreatorName string `json:"creatorName"`
|
||||
Keyworld []string `json:"keyworld"`
|
||||
Tags []int64 `json:"tags"`
|
||||
MediaType int `json:"mediaType"` // 1影集 2动画
|
||||
TopicIDs []int64 `json:"topicIds"`
|
||||
TopicNames []string `json:"topicNames"`
|
||||
Topics []VideoTopicInfo `json:"topics"`
|
||||
VideoCount int `json:"videoCount"`
|
||||
VideoTime int `json:"videoTime"`
|
||||
WatchTimes int64 `json:"watchTimes"`
|
||||
AddedTime string `json:"addedTime"`
|
||||
}
|
||||
|
||||
// VideoAllListResp 对应 swagger proto.VideoAllListResp。
|
||||
// GET /api/app/video/all/list
|
||||
type VideoAllListResp struct {
|
||||
List []VideoAllListItemResp `json:"list"`
|
||||
PageNum int `json:"pageNum"`
|
||||
PageSize int `json:"pageSize"`
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
SeoInfo SeoInfo `json:"seoInfo"`
|
||||
Advitise configser.AdvitiseData `json:"advitise"`
|
||||
}
|
||||
|
||||
// VideoItemResp 对应 swagger proto.VideoItemResp(推荐列表等)。
|
||||
type VideoItemResp struct {
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Slug string `json:"slug"`
|
||||
CoverImg string `json:"coverImg"`
|
||||
CreatorID int64 `json:"creatorId"`
|
||||
CreatorName string `json:"creatorName"`
|
||||
Keyworld []string `json:"keyworld"`
|
||||
Tags []int64 `json:"tags"`
|
||||
VideoTime int `json:"videoTime"`
|
||||
WatchTimes int64 `json:"watchTimes"`
|
||||
AddedTime string `json:"addedTime"`
|
||||
}
|
||||
|
||||
// VideoData 对应 swagger proto.VideoData。
|
||||
type VideoData struct {
|
||||
TaskID string `json:"taskId"`
|
||||
FileID string `json:"fileId"`
|
||||
URL string `json:"url"`
|
||||
Cover string `json:"cover"`
|
||||
Height int `json:"height"`
|
||||
Width int `json:"width"`
|
||||
VideoTime int `json:"videoTime"`
|
||||
PreMp4FileNameFs string `json:"preMp4FileNameFs"`
|
||||
UnencryptedPreMp4 string `json:"unencryptedPreMp4"`
|
||||
}
|
||||
|
||||
// VideoInfoResp 对应 swagger proto.VideoInfoResp(含 recommendVideos)。
|
||||
// GET /api/app/video/detail
|
||||
type VideoInfoResp 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"`
|
||||
TagsName []string `json:"tagsName"`
|
||||
WatchTimes int64 `json:"watchTimes"`
|
||||
CountLike int64 `json:"countLike"`
|
||||
CountCollect int64 `json:"countCollect"`
|
||||
CountComment int64 `json:"countComment"`
|
||||
IsLike int `json:"isLike"` // 1-已点赞 2-未点赞
|
||||
IsCollect int `json:"isCollect"` // 1-已收藏 2-未收藏
|
||||
Video VideoData `json:"video"`
|
||||
RecommendVideos []VideoItemResp `json:"recommendVideos"`
|
||||
SeoInfo SeoInfo `json:"seoInfo"`
|
||||
Advitise configser.AdvitiseData `json:"advitise"`
|
||||
AddedTime string `json:"addedTime"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
|
||||
// SeoInfo 对应 swagger proto.SeoInfo(视频列表页)。
|
||||
type SeoInfo struct {
|
||||
Title string `json:"title"`
|
||||
Keywords []string `json:"keywords"`
|
||||
Description string `json:"description"`
|
||||
WebSiteName string `json:"webSiteName"`
|
||||
CanonicalURL string `json:"canonicalUrl"`
|
||||
}
|
||||
|
||||
// DisplayName 题材展示名:优先 name,其次 title。
|
||||
func (t VideoTopicInfo) DisplayName() string {
|
||||
if name := strings.TrimSpace(t.Name); name != "" {
|
||||
return name
|
||||
}
|
||||
return strings.TrimSpace(t.Title)
|
||||
}
|
||||
|
||||
// MediaTypeLabel 影集 / 动画。
|
||||
func MediaTypeLabel(mediaType int) string {
|
||||
if mediaType == MediaAnime {
|
||||
return "动画"
|
||||
}
|
||||
return "影集"
|
||||
}
|
||||
|
||||
// EpisodeLabel 集数或时长展示。
|
||||
func EpisodeLabel(item VideoAllListItemResp) string {
|
||||
if item.VideoCount > 0 {
|
||||
return "全 " + strconv.Itoa(item.VideoCount) + " 集"
|
||||
}
|
||||
if item.VideoTime > 0 {
|
||||
return strconv.Itoa(item.VideoTime) + "分钟"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
const defaultRecommendPageSize = 6
|
||||
|
||||
// SliceRecommendPage 猜你喜欢分页切片(详情 recommendVideos,最多 20 条)。
|
||||
func SliceRecommendPage(list []VideoItemResp, page, pageSize int) (out []VideoItemResp, pageNum int) {
|
||||
if pageSize <= 0 {
|
||||
pageSize = defaultRecommendPageSize
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
n := len(list)
|
||||
if n == 0 {
|
||||
return nil, 1
|
||||
}
|
||||
start := (page - 1) * pageSize
|
||||
if start >= n {
|
||||
page = 1
|
||||
start = 0
|
||||
}
|
||||
end := start + pageSize
|
||||
if end > n {
|
||||
end = n
|
||||
}
|
||||
return list[start:end], page
|
||||
}
|
||||
|
||||
// VideoDetailURL 剧集播放页入口:优先 slug,其次 id → /series-play/{key}/。
|
||||
func VideoDetailURL(id int64) string {
|
||||
return VideoPlayURL("", id)
|
||||
}
|
||||
|
||||
// VideoPlayURL 剧集播放页入口(slug 优先,无 slug 再用 id)。
|
||||
func VideoPlayURL(slug string, id int64) string {
|
||||
seg := strings.TrimSpace(slug)
|
||||
if seg == "" && id > 0 {
|
||||
seg = strconv.FormatInt(id, 10)
|
||||
}
|
||||
if seg == "" {
|
||||
return "/series"
|
||||
}
|
||||
return "/series-play/" + url.PathEscape(seg)
|
||||
}
|
||||
Reference in New Issue
Block a user