Compare commits

...
3 Commits
Author SHA1 Message Date
rootandClaude Opus 4.7 f4460ab90c refactor(adcenter): /api/app/ads/click 改用 AdsClickInfo,接收并透传 appId
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-10-29 16:23:21 +08:00
rootandClaude Opus 4.7 af473f63dd feat(adcenter): 新增 POST /api/app/ads/click 广告点击上报到广告中心
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-10-29 16:19:44 +08:00
root f0ddce30ed 代码提交 2026-10-28 17:28:17 +08:00
6 changed files with 144 additions and 32 deletions
+31
View File
@@ -0,0 +1,31 @@
package adsapi
import (
"strconv"
"videoweb/app/api/proto"
"videoweb/app/service/configser"
"videoweb/pub/common"
"videoweb/pub/common/srvdef"
"github.com/gin-gonic/gin"
)
// Click 广告点击上报
// @Tags 广告
// @Summary 广告点击上报
// @Description 用户点击广告时异步上报到广告中心。请求可带 appId 覆盖默认,缺省时按本站 appId 上报。
// @Accept json
// @Produce json
// @Param param body proto.AdsClickInfo true "参数"
// @Success 200 {string} json
// @Router /api/app/ads/click [POST]
func Click(c *gin.Context) {
var req proto.AdsClickInfo
if err := c.ShouldBindJSON(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
configser.AsyncAdvertiseClick(strconv.FormatInt(req.AdsId, 10), int32(req.AppId))
common.ServerJSON(c, srvdef.Success, nil)
}
+1
View File
@@ -58,6 +58,7 @@ var (
"/api/app/upload/image", "/api/app/upload/image",
"/search", "/search",
"/api/app/applist", "/api/app/applist",
"/api/app/ads/click",
"/h5/m3u8", "/h5/m3u8",
"/enkey", "/enkey",
"/newenkey", "/newenkey",
+11
View File
@@ -0,0 +1,11 @@
package router
import (
"videoweb/app/api/adsapi"
"github.com/gin-gonic/gin"
)
func adsRouter(r *gin.Engine) {
r.POST("/api/app/ads/click", adsapi.Click)
}
+1
View File
@@ -114,6 +114,7 @@ func registeRouter(r *gin.Engine) {
appInfoRouter(r) appInfoRouter(r)
healthRouter(r) healthRouter(r)
richTextRouter(r) richTextRouter(r)
adsRouter(r)
} }
// 设置swag 接口信息 // 设置swag 接口信息
+66
View File
@@ -0,0 +1,66 @@
package configser
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"videoweb/app/appg"
"videoweb/pub/common/godef"
"videoweb/pub/common/httputil"
"videoweb/pub/common/log"
)
type advertiseClickReq struct {
Id int64 `json:"id"`
AppId int32 `json:"appId"`
}
type advertiseClickResp struct {
Code int64 `json:"code"`
}
// resolveAdvertiseAppId 请求带了 AppId 用请求的,否则回落到默认 advertiseAppId
func resolveAdvertiseAppId(reqAppId int32) int32 {
if reqAppId > 0 {
return reqAppId
}
return advertiseAppId
}
// AdvertiseClickCenter 同步上报单条广告点击到广告中心。
// appId<=0 时回落到默认 advertiseAppId。
func AdvertiseClickCenter(advertiseId int64, appId int32) error {
versionUrl := strings.TrimRight(strings.TrimSpace(appg.Conf.VersionUrl), "/")
if versionUrl == "" {
return fmt.Errorf("versionUrl 未配置")
}
url := versionUrl + "/api/stat/advitise/v2/click"
body, _ := json.Marshal(advertiseClickReq{Id: advertiseId, AppId: resolveAdvertiseAppId(appId)})
var resp advertiseClickResp
code, err := httputil.Client().POSTJsonWithJResp(&resp, url, map[string]string{"Content-Type": "application/json"}, body)
if err != nil {
log.Error("AdvertiseClickCenter POST fail", log.Any("url", url), log.Any("id", advertiseId), log.E(err))
return err
}
if code != http.StatusOK {
log.Error("AdvertiseClickCenter response status", log.Any("code", code), log.Any("id", advertiseId))
return fmt.Errorf("response status %d", code)
}
return nil
}
// AsyncAdvertiseClick 异步上报广告点击,非数字 id 忽略。
// appId<=0 时回落到默认 advertiseAppId。
func AsyncAdvertiseClick(id string, appId int32) {
advertiseId, err := strconv.ParseInt(strings.TrimSpace(id), 10, 64)
if err != nil {
log.Warn("AsyncAdvertiseClick invalid id", log.Any("id", id))
return
}
godef.Go(func() {
_ = AdvertiseClickCenter(advertiseId, appId)
})
}
+34 -32
View File
@@ -35,39 +35,41 @@ type fileHandler func(ctx *gin.Context) (contentLength int64, contentType string
func Enkey(ctx *gin.Context) { func Enkey(ctx *gin.Context) {
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) { fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
idStr := c.Query("id") // 已取消 /enkey 的 ticket 校验,直接返回加密 key
tStr := c.Query("t") // idStr := c.Query("id")
ticket := c.Query("ticket") // tStr := c.Query("t")
// ticket := c.Query("ticket")
//
// if idStr == "" || tStr == "" || ticket == "" {
// err = errors.New("unauthorized")
// return
// }
// videoId, parseErr := strconv.ParseInt(idStr, 10, 64)
// if parseErr != nil {
// err = errors.New("invalid id")
// return
// }
// tVal, parseErr := strconv.ParseInt(tStr, 10, 64)
// if parseErr != nil {
// err = errors.New("invalid t")
// return
// }
//
// ip := ipaddr.ResolveClientIP(c)
// ticketInfo, ticketOK := CheckH5M3U8TicketForEnkey(videoId, tVal, ip, ticket)
// if !ticketOK || ticketInfo.UseNewKey {
// log.Warn("Enkey: ticket invalid", log.Any("ip", ip), log.Any("videoId", videoId))
// err = errors.New("ticket check failed")
// return
// }
// if ticketInfo.OfficialPlayletID > 0 && !officialChapterAllowed(c, ticketInfo.OfficialPlayletID, videoId) {
// err = errors.New("本集暂未在官网开放")
// return
// }
if idStr == "" || tStr == "" || ticket == "" { buffer, bErr := m3u8.Enkey(c)
err = errors.New("unauthorized") if bErr != nil {
return err = bErr
}
videoId, parseErr := strconv.ParseInt(idStr, 10, 64)
if parseErr != nil {
err = errors.New("invalid id")
return
}
tVal, parseErr := strconv.ParseInt(tStr, 10, 64)
if parseErr != nil {
err = errors.New("invalid t")
return
}
ip := ipaddr.ResolveClientIP(c)
ticketInfo, ticketOK := CheckH5M3U8TicketForEnkey(videoId, tVal, ip, ticket)
if !ticketOK || ticketInfo.UseNewKey {
log.Warn("Enkey: ticket invalid", log.Any("ip", ip), log.Any("videoId", videoId))
err = errors.New("ticket check failed")
return
}
if ticketInfo.OfficialPlayletID > 0 && !officialChapterAllowed(c, ticketInfo.OfficialPlayletID, videoId) {
err = errors.New("本集暂未在官网开放")
return
}
buffer, err := m3u8.Enkey(c)
if err != nil {
return return
} }
contentLength = int64(buffer.Len()) contentLength = int64(buffer.Len())