广告添加

This commit is contained in:
谢宇宁
2026-10-29 20:45:09 +07:00
parent e9882465a8
commit d8f4a132af
29 changed files with 588 additions and 255 deletions
+34
View File
@@ -0,0 +1,34 @@
package configser
import (
"fmt"
"videoweb/app/service/upstream"
"github.com/gin-gonic/gin"
)
const (
adsClickPath = "/api/app/ads/click"
// AdsClickAppID 广告点击上报的应用 ID,由广告后台分配,固定为 12。
AdsClickAppID int64 = 12
)
// AdsClickReq 对应上游 POST /api/app/ads/click(appId / adsId / location 均必填)。
type AdsClickReq struct {
AppID int64 `json:"appId"`
AdsID int64 `json:"adsId"`
Location int64 `json:"location"`
}
// AdsClick 上报一次广告点击。
func AdsClick(c *gin.Context, req AdsClickReq) (*APIEnvelope[any], error) {
if req.AdsID <= 0 || req.Location <= 0 {
return nil, fmt.Errorf("ads click invalid adsId=%d location=%d", req.AdsID, req.Location)
}
req.AppID = AdsClickAppID
var env APIEnvelope[any]
if _, err := upstream.PostJSON(c, adsClickPath, req, &env); err != nil {
return nil, err
}
return &env, nil
}
+5
View File
@@ -87,6 +87,11 @@ type AdvitiseData struct {
TitleList []Advertisement `json:"titleList"`
PlayList []Advertisement `json:"playList"`
PauseyList []Advertisement `json:"pauseyList"`
// 本站后台(webhgdjapi)的字段名:横幅 / 播放页小图标 / 首页小图标
VideoTopList []Advertisement `json:"videoTopList"`
VideoDownList []Advertisement `json:"videoDownList"`
AppInfoAList []Advertisement `json:"appInfoAList"`
AppInfoBList []Advertisement `json:"appInfoBList"`
}
// Advertisement 对应 swagger proxy.AdvitiseInfo。
+50 -39
View File
@@ -7,29 +7,31 @@ import (
// AppConfigVM 注入全站页面的公共配置(字段名与接口 newAddressInfo / seoInfo 对齐)。
type AppConfigVM struct {
ForeverURLAddr string `json:"foreverUrlAddr"`
NewURLAddr string `json:"newUrlAddr"`
NewURLAddr1 string `json:"newUrlAddr1"`
NewURLAddr2 string `json:"newUrlAddr2"`
GmailAddr string `json:"gmailAddr"`
QQ string `json:"qq"`
TGGroup string `json:"tgGroup"`
Telegram string `json:"telegram"`
PotaoGroup string `json:"potaoGroup"`
CustomerServiceHost string `json:"customerServiceHost"`
AppAddr string `json:"appAddr"`
OfficialAppAddr string `json:"officialAppAddr"`
PDFAddr string `json:"pdfAddr"`
TwitterAddr string `json:"twitterAddr"`
SeoInfo SeoInfo `json:"seoInfo,omitempty"`
SeoNavInfo SeoNavInfo `json:"seoNavInfo,omitempty"`
NavPageList []NavPageItem `json:"navPageList,omitempty"`
Advitise AdvitiseData `json:"advitise"`
DetailTopList []Advertisement `json:"detailTopList,omitempty"`
NineGridList []Advertisement `json:"nineGridList,omitempty"`
DetailBannerList []Advertisement `json:"detailbannerList,omitempty"`
BottomBanner []Advertisement `json:"bottomBanner,omitempty"`
DialogList []Advertisement `json:"dialogList,omitempty"`
ForeverURLAddr string `json:"foreverUrlAddr"`
NewURLAddr string `json:"newUrlAddr"`
NewURLAddr1 string `json:"newUrlAddr1"`
NewURLAddr2 string `json:"newUrlAddr2"`
GmailAddr string `json:"gmailAddr"`
QQ string `json:"qq"`
TGGroup string `json:"tgGroup"`
Telegram string `json:"telegram"`
PotaoGroup string `json:"potaoGroup"`
CustomerServiceHost string `json:"customerServiceHost"`
AppAddr string `json:"appAddr"`
OfficialAppAddr string `json:"officialAppAddr"`
PDFAddr string `json:"pdfAddr"`
TwitterAddr string `json:"twitterAddr"`
SeoInfo SeoInfo `json:"seoInfo,omitempty"`
SeoNavInfo SeoNavInfo `json:"seoNavInfo,omitempty"`
NavPageList []NavPageItem `json:"navPageList,omitempty"`
Advitise AdvitiseData `json:"advitise"`
// 网页只用后台「站点」的 5 个广告位:
// 弹窗 / 9宫格(弹窗广告后弹出)/ 详情横幅 / 播放页小图标 / 首页小图标
DialogList []Advertisement `json:"dialogList,omitempty"`
NineGridList []Advertisement `json:"nineGridList,omitempty"`
DetailBannerList []Advertisement `json:"detailbannerList,omitempty"`
AList []Advertisement `json:"AList,omitempty"`
BList []Advertisement `json:"BList,omitempty"`
}
// BuildAppConfigVM 从上游配置组装前端可用字段。
@@ -55,11 +57,12 @@ func BuildAppConfigVM(info *ConfigInfoResp) AppConfigVM {
TwitterAddr: sanitizeURL(addr.TwitterAddr),
NavPageList: buildNavPageList(info.NavPageList),
Advitise: info.Advitise,
DetailTopList: buildAdvertisementList(info.Advitise.DetailTopList),
NineGridList: buildAdvertisementList(info.Advitise.NineGridList),
DetailBannerList: buildAdvertisementList(info.Advitise.DetailBannerList),
BottomBanner: buildAdvertisementList(info.Advitise.BottomBanner),
DialogList: buildAdvertisementList(info.Advitise.DialogList),
NineGridList: buildAdvertisementList(info.Advitise.NineGridList),
// 本站后台用 videoTopList / appInfoAList / appInfoBList;兼容 detailbannerList / AList / BList 命名
DetailBannerList: firstAdvertisementList(info.Advitise.VideoTopList, info.Advitise.DetailBannerList),
AList: firstAdvertisementList(info.Advitise.AppInfoAList, info.Advitise.AList),
BList: firstAdvertisementList(info.Advitise.AppInfoBList, info.Advitise.BList),
SeoInfo: SeoInfo{
Title: strings.TrimSpace(info.SeoInfo.Title),
Description: strings.TrimSpace(info.SeoInfo.Description),
@@ -94,20 +97,28 @@ func buildAdvertisementList(list []Advertisement) []Advertisement {
return out
}
// NineGridRow 按首页设计每行返回两个 banner,超出当前行时返回空切片。
func (c AppConfigVM) NineGridRow(row int) []Advertisement {
if row < 0 {
return nil
// ClickID 广告点击上报用的数字 ID(上游下发为数字字符串);非正整数时返回 0。
func (a Advertisement) ClickID() int64 {
id, err := strconv.ParseInt(strings.TrimSpace(a.ID), 10, 64)
if err != nil || id <= 0 {
return 0
}
start := row * 2
if start >= len(c.NineGridList) {
return nil
return id
}
// ClickTrackable 广告 ID 与广告位均有效时才输出点击上报数据(data-ads-id / data-ads-location)。
func (a Advertisement) ClickTrackable() bool {
return a.ClickID() > 0 && a.Location > 0
}
// firstAdvertisementList 返回第一个有可用广告的列表(按字段优先级)。
func firstAdvertisementList(lists ...[]Advertisement) []Advertisement {
for _, list := range lists {
if built := buildAdvertisementList(list); len(built) > 0 {
return built
}
}
end := start + 2
if end > len(c.NineGridList) {
end = len(c.NineGridList)
}
return c.NineGridList[start:end]
return nil
}
// QRText 下载弹窗二维码、跳转及复制链接内容:仅使用应用下载地址 appAddr。
+35 -20
View File
@@ -18,8 +18,7 @@ func TestBuildAppConfigVMIncludesValidAdvertisements(t *testing.T) {
NewAddressInfo: NewAddressInfo{Telegram: " https://t.me/business ", QQ: " 2168045324 ", OfficialAppAddr: " https://app.example.com/download\\ "},
Config: ConfigInfo{PotaoGroup: " https://potato.example.com/group\\ "},
Advitise: AdvitiseData{
HomeList: []Advertisement{{ID: "home", Images: "https://cdn.example.com/home.gif"}},
DetailTopList: []Advertisement{{ID: "top", Images: " https://cdn.example.com/top.gif "}},
HomeList: []Advertisement{{ID: "home", Images: "https://cdn.example.com/home.gif"}},
NineGridList: []Advertisement{
{ID: "banner-1", Images: "https://cdn.example.com/banner-1.gif"},
{ID: "banner-empty", Images: " "},
@@ -27,10 +26,13 @@ func TestBuildAppConfigVMIncludesValidAdvertisements(t *testing.T) {
{ID: "banner-3", Images: "https://cdn.example.com/banner-3.gif"},
},
DetailBannerList: []Advertisement{{ID: "detail", Images: "https://cdn.example.com/detail.gif"}},
BottomBanner: []Advertisement{
{ID: " bottom ", Images: " https://cdn.example.com/bottom.gif ", Link: " https://example.com/bottom "},
{ID: "bottom-empty", Images: " "},
VideoTopList: []Advertisement{
{ID: " video-top ", Images: " https://cdn.example.com/video-top.gif ", Link: " https://example.com/top "},
{ID: "video-top-empty", Images: " "},
},
AList: []Advertisement{{ID: "a-legacy", Images: "https://cdn.example.com/a.gif"}},
AppInfoAList: []Advertisement{{ID: "a", Images: "https://cdn.example.com/app-a.gif"}},
BList: []Advertisement{{ID: "b", Images: "https://cdn.example.com/b.gif"}},
DialogList: []Advertisement{
{ID: " 1 ", Images: " https://cdn.example.com/ad.webp\\ ", Link: " https://example.com ", Title: " 广告一 "},
{ID: "2", Images: " ", Title: "无图广告"},
@@ -56,17 +58,19 @@ func TestBuildAppConfigVMIncludesValidAdvertisements(t *testing.T) {
if len(vm.Advitise.NineGridList) != 4 {
t.Fatalf("Advitise.NineGridList length = %d, want raw length 4", len(vm.Advitise.NineGridList))
}
if len(vm.DetailTopList) != 1 || vm.DetailTopList[0].ID != "top" {
t.Fatalf("DetailTopList = %#v", vm.DetailTopList)
}
if len(vm.NineGridList) != 3 {
t.Fatalf("NineGridList length = %d, want 3", len(vm.NineGridList))
}
if len(vm.DetailBannerList) != 1 || vm.DetailBannerList[0].ID != "detail" {
// 详情横幅优先 videoTopList(本站后台字段),无效图片被过滤
if len(vm.DetailBannerList) != 1 || vm.DetailBannerList[0].ID != "video-top" || vm.DetailBannerList[0].Images != "https://cdn.example.com/video-top.gif" {
t.Fatalf("DetailBannerList = %#v", vm.DetailBannerList)
}
if len(vm.BottomBanner) != 1 || vm.BottomBanner[0].ID != "bottom" || vm.BottomBanner[0].Images != "https://cdn.example.com/bottom.gif" {
t.Fatalf("BottomBanner = %#v", vm.BottomBanner)
// 播放页小图标优先 appInfoAList;首页小图标无 appInfoBList 时回退 BList
if len(vm.AList) != 1 || vm.AList[0].ID != "a" {
t.Fatalf("AList = %#v", vm.AList)
}
if len(vm.BList) != 1 || vm.BList[0].ID != "b" {
t.Fatalf("BList = %#v", vm.BList)
}
if len(vm.DialogList) != 1 {
t.Fatalf("DialogList length = %d, want 1", len(vm.DialogList))
@@ -77,15 +81,26 @@ func TestBuildAppConfigVMIncludesValidAdvertisements(t *testing.T) {
}
}
func TestNineGridRowReturnsTwoItemsPerRow(t *testing.T) {
cfg := AppConfigVM{NineGridList: []Advertisement{{ID: "1"}, {ID: "2"}, {ID: "3"}}}
if got := cfg.NineGridRow(0); len(got) != 2 || got[0].ID != "1" || got[1].ID != "2" {
t.Fatalf("NineGridRow(0) = %#v", got)
func TestAdvertisementClickTrackable(t *testing.T) {
cases := []struct {
ad Advertisement
wantID int64
trackable bool
}{
{Advertisement{ID: "1138200002", Location: 2}, 1138200002, true},
{Advertisement{ID: " 1138200012 ", Location: 12}, 1138200012, true},
{Advertisement{ID: "1138200003", Location: 0}, 1138200003, false},
{Advertisement{ID: "", Location: 3}, 0, false},
{Advertisement{ID: "0", Location: 3}, 0, false},
{Advertisement{ID: "-5", Location: 3}, 0, false},
{Advertisement{ID: "abc", Location: 3}, 0, false},
}
if got := cfg.NineGridRow(1); len(got) != 1 || got[0].ID != "3" {
t.Fatalf("NineGridRow(1) = %#v", got)
}
if got := cfg.NineGridRow(2); len(got) != 0 {
t.Fatalf("NineGridRow(2) = %#v, want empty", got)
for _, tc := range cases {
if got := tc.ad.ClickID(); got != tc.wantID {
t.Fatalf("ClickID(%q) = %d, want %d", tc.ad.ID, got, tc.wantID)
}
if got := tc.ad.ClickTrackable(); got != tc.trackable {
t.Fatalf("ClickTrackable(%q, %d) = %v, want %v", tc.ad.ID, tc.ad.Location, got, tc.trackable)
}
}
}