This commit is contained in:
谢宇宁
2026-09-29 15:30:33 +07:00
parent 0fbdba5ccf
commit 9abe0b1b76
181 changed files with 608 additions and 18501 deletions
-130
View File
@@ -1,130 +0,0 @@
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}
}
-141
View File
@@ -1,141 +0,0 @@
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
}
-37
View File
@@ -1,37 +0,0 @@
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
}