71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
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)
|
|
}
|