feat(adcenter): 切到 versionUrl /api/stat/advitise/v2/get 接口拉取,appId=12
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,7 @@ listenAddr: "20001"
|
|||||||
origDomain: "https://yuan.bbaazz.me/"
|
origDomain: "https://yuan.bbaazz.me/"
|
||||||
trustedProxies: ""
|
trustedProxies: ""
|
||||||
bucketVideoAddressV2: "http://139.162.27.29:19996"
|
bucketVideoAddressV2: "http://139.162.27.29:19996"
|
||||||
|
versionUrl: "http://139.162.47.194:8089"
|
||||||
adCenter:
|
adCenter:
|
||||||
apiDomain: "https://adopenapi.adcs01.top"
|
apiDomain: "https://adopenapi.adcs01.top"
|
||||||
merchantCode: "1991129808640122880"
|
merchantCode: "1991129808640122880"
|
||||||
|
|||||||
@@ -2,60 +2,131 @@ package configser
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"videoweb/app/appg"
|
||||||
|
"videoweb/pub/common/httputil"
|
||||||
"videoweb/pub/common/log"
|
"videoweb/pub/common/log"
|
||||||
"videoweb/pub/common/netutil"
|
"videoweb/pub/common/netutil"
|
||||||
"videoweb/pub/pubdef"
|
|
||||||
"videoweb/pub/storage"
|
|
||||||
"videoweb/pub/storage/proxy"
|
"videoweb/pub/storage/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateAdvitiseData 从媒资中心按 AppId 拉取广告,图片拼接 CDN 后写入缓存
|
// 广告中心应用ID(本站专用)
|
||||||
func UpdateAdvitiseData(ctx context.Context) {
|
const advertiseAppId int32 = 12
|
||||||
config := GetConfigInfo()
|
|
||||||
queryAdvitiseReq := &proxy.AppAdvitiseInfoReq{
|
// advertiseInfo 广告中心 /api/stat/advitise/v2/get 的广告条目
|
||||||
Id: pubdef.AppId,
|
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 未配置")
|
||||||
}
|
}
|
||||||
advitiseResult, err := storage.QueryAdvitiseList(ctx, queryAdvitiseReq, "")
|
url := versionUrl + "/api/stat/advitise/v2/get"
|
||||||
if err != nil || advitiseResult.Code != http.StatusOK {
|
body, _ := json.Marshal(advertiseReq{AppId: advertiseAppId})
|
||||||
if advitiseResult != nil {
|
var resp advertiseRes
|
||||||
log.Warn("UpdateAdvitiseData.QueryAdvitiseList failed", log.Any("filter", queryAdvitiseReq), log.E(err), log.Any("respCode", advitiseResult.Code))
|
code, err := httputil.Client().POSTJsonWithJResp(&resp, url, map[string]string{"Content-Type": "application/json"}, body)
|
||||||
} else {
|
if err != nil {
|
||||||
log.Warn("UpdateAdvitiseData.QueryAdvitiseList failed", log.Any("filter", queryAdvitiseReq), log.E(err))
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if code != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("response status %d", code)
|
||||||
|
}
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateAdvitiseData 从广告中心拉取广告并更新内存缓存,列表为空时重试一次
|
||||||
|
func UpdateAdvitiseData(ctx context.Context) {
|
||||||
|
resp, err := fetchAdvertiseList()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("UpdateAdvitiseData fetchAdvertiseList fail", log.E(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(resp.AdvertiseList) == 0 {
|
||||||
|
resp, err = fetchAdvertiseList()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("UpdateAdvitiseData 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
|
||||||
|
})
|
||||||
|
|
||||||
data := advitiseResult.Data
|
config := GetConfigInfo()
|
||||||
for _, list := range []*[]proxy.AdvitiseInfo{
|
data := proxy.AdvitiseData{}
|
||||||
&data.DialogList,
|
|
||||||
&data.HomeList,
|
for _, v := range list {
|
||||||
&data.VideoTopList,
|
pos := int64(v.LocId)
|
||||||
&data.VideoDownList,
|
info := proxy.AdvitiseInfo{
|
||||||
&data.HotAppList,
|
Id: strconv.FormatInt(v.Id, 10),
|
||||||
&data.NewAppList,
|
Location: pos,
|
||||||
&data.MustAppList,
|
Title: v.Title,
|
||||||
&data.TitleAppList,
|
Images: netutil.MergeURL(config.ImgCdn, strings.TrimLeft(v.CoverImg, "/")),
|
||||||
&data.FriendList,
|
Link: strings.ReplaceAll(v.Link, "inner://yinseinner/", "yinseinner://"),
|
||||||
&data.NineGridList,
|
}
|
||||||
&data.VideoList,
|
switch pos {
|
||||||
&data.StopList,
|
case 1:
|
||||||
&data.ForcedJumpList,
|
data.HomeList = append(data.HomeList, info)
|
||||||
&data.AppInfoAList,
|
case 2:
|
||||||
&data.AppInfoBList,
|
data.VideoTopList = append(data.VideoTopList, info)
|
||||||
} {
|
case 3:
|
||||||
mergeAdvitiseImages(*list, config.ImgCdn)
|
data.NineGridList = append(data.NineGridList, info)
|
||||||
|
case 4:
|
||||||
|
data.VideoDownList = append(data.VideoDownList, info)
|
||||||
|
case 5:
|
||||||
|
data.HotAppList = append(data.HotAppList, 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.NewAppList = append(data.NewAppList, info)
|
||||||
|
case 11:
|
||||||
|
data.VideoList = append(data.VideoList, info)
|
||||||
|
case 12:
|
||||||
|
data.AppInfoAList = append(data.AppInfoAList, info)
|
||||||
|
case 13:
|
||||||
|
data.AppInfoBList = append(data.AppInfoBList, info)
|
||||||
|
case 14:
|
||||||
|
data.StopList = append(data.StopList, info)
|
||||||
|
case 15:
|
||||||
|
data.ForcedJumpList = append(data.ForcedJumpList, info)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CacheMgr.Lock.Lock()
|
CacheMgr.Lock.Lock()
|
||||||
defer CacheMgr.Lock.Unlock()
|
defer CacheMgr.Lock.Unlock()
|
||||||
CacheMgr.Advitise = data
|
CacheMgr.Advitise = data
|
||||||
}
|
}
|
||||||
|
|
||||||
func mergeAdvitiseImages(list []proxy.AdvitiseInfo, cdn string) {
|
|
||||||
for i := range list {
|
|
||||||
if len(list[i].Images) > 0 {
|
|
||||||
list[i].Images = netutil.MergeURL(cdn, list[i].Images)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user