广告点击添加
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package adsser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"videoweb/app/service/upstream"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const clickPath = "/api/app/ads/click"
|
||||
|
||||
// ClickAppID 广告点击上报固定 appId。
|
||||
const ClickAppID int64 = 12
|
||||
|
||||
// ClickReq 对应上游 AdsClickInfo:appId / adsId / location 均必填。
|
||||
type ClickReq struct {
|
||||
AppId int64 `json:"appId"`
|
||||
AdsId int64 `json:"adsId"`
|
||||
Location int64 `json:"location"`
|
||||
}
|
||||
|
||||
// APIEnvelope 上游通用响应。
|
||||
type APIEnvelope struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Tip string `json:"tip"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
// Click 调用上游上报广告点击。
|
||||
func Click(c *gin.Context, req ClickReq) (*APIEnvelope, error) {
|
||||
var env APIEnvelope
|
||||
if _, err := upstream.PostJSON(c, clickPath, req, &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &env, nil
|
||||
}
|
||||
@@ -122,6 +122,13 @@ type Advertisement struct {
|
||||
AdvertiseCode string `json:"advertiseCode"`
|
||||
AdvertiseLocationCode string `json:"advertiseLocationCode"`
|
||||
AdvertiseLocationName string `json:"advertiseLocationName"`
|
||||
// AdsID 点击上报用的广告 id(上游 id 为数字字符串),在 advertiseCode 覆盖 ID 之前解析;无效为 0。
|
||||
AdsID int64 `json:"-"`
|
||||
}
|
||||
|
||||
// CanReportClick 广告 id 与 location 均为正整数时才上报点击(POST /api/app/ads/click)。
|
||||
func (a Advertisement) CanReportClick() bool {
|
||||
return a.AdsID > 0 && a.Location > 0
|
||||
}
|
||||
|
||||
// NewAddressInfo 对应 swagger proto.NewAddressInfo(GET /api/app/config/info)。
|
||||
|
||||
@@ -131,6 +131,7 @@ func buildAdvertisementList(list []Advertisement) []Advertisement {
|
||||
item.AdvertiseCode = strings.TrimSpace(item.AdvertiseCode)
|
||||
item.AdvertiseLocationCode = strings.TrimSpace(item.AdvertiseLocationCode)
|
||||
item.AdvertiseLocationName = strings.TrimSpace(item.AdvertiseLocationName)
|
||||
item.AdsID = parseAdsID(item.ID)
|
||||
if item.AdvertiseCode != "" {
|
||||
item.ID = item.AdvertiseCode
|
||||
}
|
||||
@@ -145,6 +146,15 @@ func buildAdvertisementList(list []Advertisement) []Advertisement {
|
||||
return out
|
||||
}
|
||||
|
||||
// parseAdsID 解析上游数字字符串广告 id;非正整数返回 0(不上报)。
|
||||
func parseAdsID(id string) int64 {
|
||||
n, err := strconv.ParseInt(strings.TrimSpace(id), 10, 64)
|
||||
if err != nil || n <= 0 {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// firstAdvertisementList 返回第一个有可用广告的列表(按字段优先级)。
|
||||
func firstAdvertisementList(lists ...[]Advertisement) []Advertisement {
|
||||
for _, list := range lists {
|
||||
|
||||
@@ -45,3 +45,30 @@ func TestBuildAppConfigVMFallsBackToLegacyBannerField(t *testing.T) {
|
||||
t.Fatalf("DetailBannerList = %#v, want detailbannerList fallback", vm.DetailBannerList)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildAppConfigVMParsesAdsClickID(t *testing.T) {
|
||||
vm := BuildAppConfigVM(&ConfigInfoResp{
|
||||
Advitise: AdvitiseData{
|
||||
NineGridList: []Advertisement{
|
||||
{ID: " 1138200002 ", Location: 2, Images: "https://cdn.example.com/a.gif"},
|
||||
{ID: "1138200003", Location: 2, AdvertiseCode: "code-x", Images: "https://cdn.example.com/b.gif"},
|
||||
{ID: "abc", Location: 2, Images: "https://cdn.example.com/c.gif"},
|
||||
{ID: "1138200004", Location: 0, Images: "https://cdn.example.com/d.gif"},
|
||||
},
|
||||
},
|
||||
})
|
||||
got := vm.NineGridList
|
||||
if len(got) != 4 {
|
||||
t.Fatalf("NineGridList = %#v", got)
|
||||
}
|
||||
if got[0].AdsID != 1138200002 || !got[0].CanReportClick() {
|
||||
t.Fatalf("item0 = %#v, want reportable id 1138200002", got[0])
|
||||
}
|
||||
// advertiseCode 覆盖展示 ID,但点击上报仍用上游原始 id
|
||||
if got[1].ID != "code-x" || got[1].AdsID != 1138200003 {
|
||||
t.Fatalf("item1 = %#v, want AdsID from raw id", got[1])
|
||||
}
|
||||
if got[2].CanReportClick() || got[3].CanReportClick() {
|
||||
t.Fatalf("invalid id/location must not report: %#v %#v", got[2], got[3])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user