- 替换集团广告中心加密拉取逻辑 - 位置 1-14 分别映射到 Home/DetailTop/NineGrid/Detailbanner/LaunchScreen/MustApp/TitleApp/Friend/Dialog/Post/Floating/A/B/VideoList Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
183 lines
5.2 KiB
Go
183 lines
5.2 KiB
Go
package configser
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"videoweb/app/api/proto"
|
|
"videoweb/app/appg"
|
|
"videoweb/pub/common/httputil"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/netutil"
|
|
"videoweb/pub/models/videoinformationmod"
|
|
"videoweb/pub/pubdef"
|
|
"videoweb/pub/storage/proxy"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func UpdateAnnouncementData(ctx context.Context) {
|
|
config := GetConfigInfo()
|
|
resp := proto.AnnouncementInfo{
|
|
VideoInfo: make([]proto.VideoInfoData, 0),
|
|
PdfAddr: config.PdfAddr,
|
|
NewUrlAddr: config.DHNewUrlAddr1,
|
|
NewUrlAddr1: config.DHNewUrlAddr2,
|
|
NewUrlAddr2: config.DHNewUrlAddr3,
|
|
ForeverUrlAddr: config.ForeverUrlAddr,
|
|
AppAddr: config.AppAddr,
|
|
TranslationAddr: config.TranslationAddr,
|
|
}
|
|
|
|
videoFilter := bson.M{
|
|
videoinformationmod.FN.VideoInformation.Status: pubdef.InformationStatusVerifyOK,
|
|
videoinformationmod.FN.VideoInformation.IsHot: true,
|
|
}
|
|
videoOpt := &options.FindOptions{}
|
|
videoOpt.SetSort(bson.M{videoinformationmod.FN.VideoInformation.Sort: -1})
|
|
videoOpt.SetLimit(2)
|
|
|
|
videoList, serr := videoinformationmod.FindManyVideoInformations(ctx, videoFilter, videoOpt)
|
|
if serr != nil {
|
|
log.Error("VideoCategoryData", log.Any("videoFilter", videoFilter), log.E(serr))
|
|
return
|
|
}
|
|
// 查询视频
|
|
for _, videoInfo := range videoList {
|
|
v := proto.VideoInfoToData(videoInfo, config.ImgCdn, GetCreateInfo(videoInfo.CreatorId, config.ImgCdn))
|
|
for _, tagId := range videoInfo.Tags {
|
|
tagInfo := GetVideoTag(tagId)
|
|
if tagInfo != nil {
|
|
v.Tags = append(v.Tags, proto.TagsInfoData{
|
|
Id: tagInfo.Id,
|
|
Name: tagInfo.Name,
|
|
Slug: tagInfo.Slug,
|
|
})
|
|
}
|
|
}
|
|
resp.VideoInfo = append(resp.VideoInfo, v)
|
|
}
|
|
CacheMgr.Lock.Lock()
|
|
defer CacheMgr.Lock.Unlock()
|
|
CacheMgr.Announcement = resp
|
|
}
|
|
|
|
// 广告中心应用ID(本站专用)
|
|
const advertiseAppId int32 = 12
|
|
|
|
// advertiseInfo 广告中心 /api/stat/advitise/v2/get 的广告条目
|
|
type advertiseInfo struct {
|
|
Id int64 `json:"id"` // 广告id
|
|
Title string `json:"title"` // 广告标题
|
|
CoverImg string `json:"coverImg"` // 封面
|
|
LocId int32 `json:"locId"` // 位置id
|
|
JumpType int32 `json:"jumpType"` // 跳转方式
|
|
Link string `json:"link"` // 链接地址
|
|
Sort int64 `json:"sort"` // 排序
|
|
Tags string `json:"tags"` // 标签
|
|
Remark string `json:"remark"` // 备注描述
|
|
}
|
|
|
|
type advertiseReq struct {
|
|
AppId int32 `json:"appId"`
|
|
}
|
|
|
|
type advertiseRes struct {
|
|
AdvertiseList []advertiseInfo `json:"advertiseList"`
|
|
}
|
|
|
|
// fetchAdvertiseList 请求广告中心 /api/stat/advitise/v2/get
|
|
func fetchAdvertiseList() (*advertiseRes, error) {
|
|
versionUrl := strings.TrimRight(strings.TrimSpace(appg.Conf.VersionUrl), "/")
|
|
if versionUrl == "" {
|
|
return nil, fmt.Errorf("versionUrl 未配置")
|
|
}
|
|
url := versionUrl + "/api/stat/advitise/v2/get"
|
|
body, _ := json.Marshal(advertiseReq{AppId: advertiseAppId})
|
|
var resp advertiseRes
|
|
code, err := httputil.Client().POSTJsonWithJResp(&resp, url, map[string]string{"Content-Type": "application/json"}, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if code != http.StatusOK {
|
|
return nil, fmt.Errorf("response status %d", code)
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
// UpdateAdvitiseDataFromJt 从广告中心拉取广告并更新内存缓存,列表为空时重试一次
|
|
func UpdateAdvitiseDataFromJt() {
|
|
resp, err := fetchAdvertiseList()
|
|
if err != nil {
|
|
log.Error("UpdateAdvitiseDataFromJt fetchAdvertiseList fail", log.E(err))
|
|
return
|
|
}
|
|
if len(resp.AdvertiseList) == 0 {
|
|
resp, err = fetchAdvertiseList()
|
|
if err != nil {
|
|
log.Error("UpdateAdvitiseDataFromJt fetchAdvertiseList retry fail", log.E(err))
|
|
return
|
|
}
|
|
}
|
|
list := resp.AdvertiseList
|
|
sort.SliceStable(list, func(i, j int) bool {
|
|
if list[i].LocId != list[j].LocId {
|
|
return list[i].LocId < list[j].LocId
|
|
}
|
|
return list[i].Sort < list[j].Sort
|
|
})
|
|
|
|
config := GetConfigInfo()
|
|
data := proto.AdvitiseData{}
|
|
|
|
for _, v := range list {
|
|
pos := int64(v.LocId)
|
|
info := proxy.AdvitiseInfo{
|
|
Id: strconv.FormatInt(v.Id, 10),
|
|
Location: pos,
|
|
Title: v.Title,
|
|
Images: netutil.MergeURL(config.ImgCdn, strings.TrimLeft(v.CoverImg, "/")),
|
|
Link: strings.ReplaceAll(v.Link, "inner://yinseinner/", "yinseinner://"),
|
|
}
|
|
switch pos {
|
|
case 1:
|
|
data.HomeList = append(data.HomeList, info)
|
|
case 2:
|
|
data.DetailTopList = append(data.DetailTopList, info)
|
|
case 3:
|
|
data.NineGridList = append(data.NineGridList, info)
|
|
case 4:
|
|
data.DetailbannerList = append(data.DetailbannerList, info)
|
|
case 5:
|
|
data.LaunchScreenList = append(data.LaunchScreenList, info)
|
|
case 6:
|
|
data.MustAppList = append(data.MustAppList, info)
|
|
case 7:
|
|
data.TitleAppList = append(data.TitleAppList, info)
|
|
case 8:
|
|
data.FriendList = append(data.FriendList, info)
|
|
case 9:
|
|
data.DialogList = append(data.DialogList, info)
|
|
case 10:
|
|
data.PostList = append(data.PostList, info)
|
|
case 11:
|
|
data.FloatingList = append(data.FloatingList, info)
|
|
case 12:
|
|
data.AList = append(data.AList, info)
|
|
case 13:
|
|
data.BList = append(data.BList, info)
|
|
case 14:
|
|
data.VideoList = append(data.VideoList, info)
|
|
}
|
|
}
|
|
|
|
CacheMgr.Lock.Lock()
|
|
defer CacheMgr.Lock.Unlock()
|
|
CacheMgr.Advitise = data
|
|
}
|