From 456ebdad45cdc22dc47e9a7ec8c4fd04a08b9ac3 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 28 Oct 2026 17:10:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(adcenter):=20=E5=88=87=E5=88=B0=20versionU?= =?UTF-8?q?rl=20/api/stat/advitise/v2/get=20=E6=8E=A5=E5=8F=A3=E6=8B=89?= =?UTF-8?q?=E5=8F=96=EF=BC=8CappId=3D12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 --- app/conf/app.yaml | 1 + app/service/configser/announcement.go | 153 +++++++++++++++++++------- 2 files changed, 113 insertions(+), 41 deletions(-) diff --git a/app/conf/app.yaml b/app/conf/app.yaml index f6d3034..38d6c30 100644 --- a/app/conf/app.yaml +++ b/app/conf/app.yaml @@ -29,6 +29,7 @@ listenAddr: "20001" origDomain: "https://yuan.bbaazz.me/" trustedProxies: "" bucketVideoAddressV2: "http://139.162.27.29:19996" +versionUrl: "http://139.162.47.194:8089" adCenter: apiDomain: "https://adopenapi.adcs01.top" merchantCode: "1991129808640122880" diff --git a/app/service/configser/announcement.go b/app/service/configser/announcement.go index f2f3002..d5cf2c3 100644 --- a/app/service/configser/announcement.go +++ b/app/service/configser/announcement.go @@ -2,60 +2,131 @@ package configser import ( "context" + "encoding/json" + "fmt" "net/http" + "sort" + "strconv" + "strings" + "videoweb/app/appg" + "videoweb/pub/common/httputil" "videoweb/pub/common/log" "videoweb/pub/common/netutil" - "videoweb/pub/pubdef" - "videoweb/pub/storage" "videoweb/pub/storage/proxy" ) -// UpdateAdvitiseData 从媒资中心按 AppId 拉取广告,图片拼接 CDN 后写入缓存 -func UpdateAdvitiseData(ctx context.Context) { - config := GetConfigInfo() - queryAdvitiseReq := &proxy.AppAdvitiseInfoReq{ - Id: pubdef.AppId, +// 广告中心应用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 未配置") } - advitiseResult, err := storage.QueryAdvitiseList(ctx, queryAdvitiseReq, "") - if err != nil || advitiseResult.Code != http.StatusOK { - if advitiseResult != nil { - log.Warn("UpdateAdvitiseData.QueryAdvitiseList failed", log.Any("filter", queryAdvitiseReq), log.E(err), log.Any("respCode", advitiseResult.Code)) - } else { - log.Warn("UpdateAdvitiseData.QueryAdvitiseList failed", log.Any("filter", queryAdvitiseReq), log.E(err)) - } + 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 +} + +// UpdateAdvitiseData 从广告中心拉取广告并更新内存缓存,列表为空时重试一次 +func UpdateAdvitiseData(ctx context.Context) { + resp, err := fetchAdvertiseList() + if err != nil { + log.Error("UpdateAdvitiseData fetchAdvertiseList fail", log.E(err)) 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 - for _, list := range []*[]proxy.AdvitiseInfo{ - &data.DialogList, - &data.HomeList, - &data.VideoTopList, - &data.VideoDownList, - &data.HotAppList, - &data.NewAppList, - &data.MustAppList, - &data.TitleAppList, - &data.FriendList, - &data.NineGridList, - &data.VideoList, - &data.StopList, - &data.ForcedJumpList, - &data.AppInfoAList, - &data.AppInfoBList, - } { - mergeAdvitiseImages(*list, config.ImgCdn) + config := GetConfigInfo() + data := proxy.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.VideoTopList = append(data.VideoTopList, info) + case 3: + 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() defer CacheMgr.Lock.Unlock() 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) - } - } -}