@@ -0,0 +1,184 @@
|
||||
package dataReport
|
||||
|
||||
import (
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/middleware/ua"
|
||||
"91porn-server/models/commod"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/moul/http2curl"
|
||||
)
|
||||
|
||||
var config = Config{}
|
||||
|
||||
func Init(c Config) {
|
||||
config = c
|
||||
}
|
||||
|
||||
// GenerateGlobalEventID 生成全局唯一的 event_id
|
||||
func GenerateGlobalEventID() string {
|
||||
// 生成一个新的 UUID
|
||||
return strings.ReplaceAll(uuid.New().String(), "-", "")
|
||||
}
|
||||
|
||||
func ReportAppId() string {
|
||||
if commod.KFK_APPID < 10 {
|
||||
return fmt.Sprintf("JHA-00%v", commod.KFK_APPID)
|
||||
}
|
||||
if commod.KFK_APPID < 100 {
|
||||
return fmt.Sprintf("JHA-0%v", commod.KFK_APPID)
|
||||
}
|
||||
return fmt.Sprintf("JHA-%v", commod.KFK_APPID)
|
||||
}
|
||||
|
||||
// 上报事件
|
||||
func BatchReport(data interface{}) error {
|
||||
apiUrl := config.ApiUrl + "/api/eventTracking/batchReport.json"
|
||||
reqBody, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Error("Event report json.Marshal fail", log.E(err))
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", apiUrl, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
log.Error("Event report NewRequest fail", log.E(err))
|
||||
return fmt.Errorf("failed to create request: %v", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
curl, _ := http2curl.GetCurlCommand(req)
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second, // 设置超时时间为 5 秒
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Error("Event report fail", log.Any("curl", curl), log.E(err))
|
||||
return fmt.Errorf("failed to send request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Error("Event report fail", log.Any("curl", curl), log.Any("http.code", resp.StatusCode))
|
||||
return fmt.Errorf("failed to report event, status code: %d", resp.StatusCode)
|
||||
}
|
||||
// 读取返回的 body
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Error("Event report fail", log.Any("curl", curl), log.E(err))
|
||||
return fmt.Errorf("failed to read response body: %v", err)
|
||||
}
|
||||
// 输出响应状态和返回的 body
|
||||
log.Info("Event report Response", log.Any("body", string(body)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func structToFormData(data interface{}) (string, error) {
|
||||
// 序列化结构体为 JSON 字符串
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 反序列化为 map
|
||||
var dataMap map[string]interface{}
|
||||
err = json.Unmarshal(jsonData, &dataMap)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var formData []string
|
||||
|
||||
// 遍历 map,将其转换为 form 数据
|
||||
for key, value := range dataMap {
|
||||
if value == nil {
|
||||
continue // 如果值为 nil,跳过
|
||||
}
|
||||
|
||||
// 将值转换为字符串
|
||||
valueStr := fmt.Sprintf("%v", value)
|
||||
|
||||
// 使用 URL 编码
|
||||
formData = append(formData, fmt.Sprintf(
|
||||
"%s=%s",
|
||||
url.QueryEscape(key),
|
||||
url.QueryEscape(valueStr),
|
||||
))
|
||||
}
|
||||
|
||||
return strings.Join(formData, "&"), nil
|
||||
}
|
||||
|
||||
func NewCommonField(eventId string, event string, uid uint64, districtCode string, ua ua.UA, ip string) CommonField {
|
||||
device := normalizeDevice(ua.SysType)
|
||||
deviceID := CutTo50(strings.TrimSpace(ua.DevID))
|
||||
deviceModel := strings.TrimSpace(ua.DeviceModel)
|
||||
if deviceModel == "" {
|
||||
deviceModel = strings.TrimSpace(ua.DevType)
|
||||
}
|
||||
systemName := strings.TrimSpace(ua.SystemName)
|
||||
if systemName == "" {
|
||||
systemName = device
|
||||
}
|
||||
return CommonField{
|
||||
UID: fmt.Sprintf("%v", uid),
|
||||
EventID: eventId,
|
||||
Event: event, // 事件类型由外部事件对象指定
|
||||
Channel: strings.TrimSpace(districtCode),
|
||||
SID: strings.TrimSpace(ua.SID),
|
||||
AppID: ReportAppId(),
|
||||
ClientTS: JsonNumber(time.Now().Unix()),
|
||||
Device: device,
|
||||
DeviceID: deviceID,
|
||||
UserAgent: strings.TrimSpace(ua.UserAgent),
|
||||
DeviceModel: deviceModel,
|
||||
DeviceBrand: strings.TrimSpace(ua.DeviceBrand),
|
||||
SystemName: systemName,
|
||||
SystemVersion: strings.TrimSpace(ua.SystemVersion),
|
||||
IP: ip,
|
||||
}
|
||||
}
|
||||
|
||||
func Device(ua ua.UA) string {
|
||||
return normalizeDevice(ua.SysType)
|
||||
}
|
||||
|
||||
func normalizeDevice(value string) string {
|
||||
lower := strings.ToLower(strings.TrimSpace(value))
|
||||
switch {
|
||||
case lower == "":
|
||||
return ""
|
||||
case strings.Contains(lower, "ios"), strings.Contains(lower, "iphone"), strings.Contains(lower, "ipad"), strings.Contains(lower, "apple"):
|
||||
return "iOS"
|
||||
case strings.Contains(lower, "android"):
|
||||
return "Android"
|
||||
case strings.Contains(lower, "pc"), strings.Contains(lower, "windows"), strings.Contains(lower, "mac"), strings.Contains(lower, "h5"):
|
||||
return "PC"
|
||||
}
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
// CutTo50 截取前 50 个“字符”(支持中文、-、_ 等特殊字符)
|
||||
func CutTo50(s string) string {
|
||||
runes := []rune(s)
|
||||
if len(runes) <= 50 {
|
||||
return s
|
||||
}
|
||||
return string(runes[:50])
|
||||
}
|
||||
|
||||
func JsonNumber(v int64) json.Number {
|
||||
return json.Number(strconv.FormatInt(v, 10))
|
||||
}
|
||||
|
||||
func JsonRawMessage(v interface{}) json.RawMessage {
|
||||
b, _ := json.Marshal(v)
|
||||
return b
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package dataReport
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReportEvent(t *testing.T) {
|
||||
reportAppId := "11"
|
||||
// 创建一个广告点击
|
||||
event := &CommonField{
|
||||
UID: "user_123",
|
||||
EventID: GenerateGlobalEventID(),
|
||||
Event: EventTypeAdClick,
|
||||
Channel: "666",
|
||||
AppID: reportAppId,
|
||||
//SID string `json:"sid"` // 会话ID
|
||||
//ClientTS int64 `json:"client_ts"` // 客户端时间戳
|
||||
Device: "Android",
|
||||
DeviceID: "device_12345",
|
||||
UserAgent: "Mozilla/5.0",
|
||||
DeviceBrand: "Huawei",
|
||||
DeviceModel: "Mate 40",
|
||||
IP: "192.168.0.1",
|
||||
}
|
||||
event.Payload = JsonRawMessage(AdClickEvent{
|
||||
AdID: "1286400000000040", // 广告ID
|
||||
})
|
||||
config.ApiUrl = "https://api.shuifeng.cc"
|
||||
list := []*CommonField{event}
|
||||
BatchReport(list)
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
package dataReport
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Config struct {
|
||||
ApiUrl string `json:"apiUrl"`
|
||||
}
|
||||
|
||||
const (
|
||||
EventTypeAdClick = "ad_click"
|
||||
EventTypeUserLogin = "user_login"
|
||||
EventTypeUserRegister = "user_register"
|
||||
EventTypeOrderCreated = "order_created"
|
||||
EventTypeOrderPaid = "order_paid"
|
||||
EventTypeCoinConsume = "coin_consume"
|
||||
EventTypeVideoPurchase = "video_purchase"
|
||||
EventTypeVideoLike = "video_like"
|
||||
EventTypeVideoCollect = "video_collect"
|
||||
EventTypeVideoComment = "video_comment"
|
||||
EventTypeVideoStatusChange = "video_status_change"
|
||||
|
||||
// 漫画事件
|
||||
EventTypeComicPurchase = "comic_purchase" // 漫画购买事件
|
||||
EventTypeComicLike = "comic_like" // 漫画点赞
|
||||
EventTypeComicCollect = "comic_collect" // 漫画收藏
|
||||
EventTypeComicComment = "comic_comment" // 漫画评论
|
||||
|
||||
// 小说事件
|
||||
EventTypeNovelPurchase = "novel_purchase" // 小说购买事件
|
||||
EventTypeNovelLike = "novel_like" // 小说点赞
|
||||
EventTypeNovelCollect = "novel_collect" // 小说收藏
|
||||
EventTypeNovelComment = "novel_comment" // 小说评论
|
||||
|
||||
)
|
||||
|
||||
// 定义公共字段和事件数据结构
|
||||
type CommonField struct {
|
||||
Event string `json:"event"` // 事件类型
|
||||
Channel string `json:"channel"` // 渠道码
|
||||
EventID string `json:"event_id"` // 事件唯一标识符
|
||||
AppID string `json:"app_id"` // 应用ID
|
||||
UID string `json:"uid"` // 用户ID
|
||||
SID string `json:"sid"` // 会话ID
|
||||
ClientTS json.Number `json:"client_ts"` // 客户端时间戳
|
||||
Device string `json:"device"` // 设备类型(如:Android, iOS, PC)
|
||||
DeviceID string `json:"device_id"` // 设备ID
|
||||
UserAgent string `json:"user_agent"` // 用户代理信息
|
||||
DeviceBrand string `json:"device_brand"` // 设备品牌(如:HUAWEI)
|
||||
DeviceModel string `json:"device_model"` // 设备型号
|
||||
SystemName string `json:"system_name"` // 系统名称
|
||||
SystemVersion string `json:"system_version"` // 系统版本
|
||||
IP string `json:"ip,omitempty"` // 用户IP地址
|
||||
Payload json.RawMessage `json:"payload"` // 事件数据
|
||||
}
|
||||
|
||||
// UserRegisterEvent 注册事件
|
||||
type UserRegisterEvent struct {
|
||||
Type string `json:"type"` // 注册方式:phone, deviceid, email, username
|
||||
TraceID string `json:"trace_id"` // 唯一标识,落地页点击生成
|
||||
CreateTime json.Number `json:"create_time"` // 注册时间,10位时间戳
|
||||
}
|
||||
|
||||
// UserLoginEvent 登录事件
|
||||
type UserLoginEvent struct {
|
||||
Type string `json:"type"` // 登录方式:phone, deviceid, email, username
|
||||
}
|
||||
|
||||
// OrderCreatedEvent 订单创建
|
||||
type OrderCreatedEvent struct {
|
||||
OrderID string `json:"order_id"` // 订单号(唯一标识)
|
||||
OrderType string `json:"order_type"` // 订单类型
|
||||
ProductID string `json:"product_id"` // 商品ID
|
||||
ProductName string `json:"product_name"` // 商品名称
|
||||
Amount json.Number `json:"amount"` // 订单金额(分)
|
||||
Currency string `json:"currency"` // 货币类型
|
||||
CoinQuantity json.Number `json:"coin_quantity"` // 金币数量
|
||||
VIPDurationID string `json:"vip_duration_type"`
|
||||
VIPDurationName string `json:"vip_duration_name"`
|
||||
SourcePageKey string `json:"source_page_key"` // 来源页面标识
|
||||
SourcePageName string `json:"source_page_name"` // 来源页面名称
|
||||
CreateTime json.Number `json:"create_time"` // 订单创建时间
|
||||
}
|
||||
|
||||
// OrderPaidEvent 订单支付成功
|
||||
type OrderPaidEvent struct {
|
||||
OrderID string `json:"order_id"` // 订单号(关联创建事件)
|
||||
OrderType string `json:"order_type"` // 订单类型:coin_purchase, vip_subscription
|
||||
ProductID string `json:"product_id"` // 商品ID
|
||||
Amount json.Number `json:"amount"` // 实际支付金额(分)
|
||||
Currency string `json:"currency"` // 货币类型:CNY, USD
|
||||
CoinQuantity json.Number `json:"coin_quantity"` // 金币数量(仅金币订单)
|
||||
VIPExpirationTime json.Number `json:"vip_expiration_time"` // VIP过期时间(10位时间戳)
|
||||
PayType string `json:"pay_type"` // 支付方式:wechat, alipay, bank_card
|
||||
PayChannel string `json:"pay_channel"` // 支付渠道:银行名称
|
||||
TransactionID string `json:"transaction_id"` // 第三方支付交易号
|
||||
CreateTime json.Number `json:"create_time"` // 订单创建时间
|
||||
|
||||
}
|
||||
|
||||
// CoinConsumeEvent 金币消耗
|
||||
type CoinConsumeEvent struct {
|
||||
ProductID string `json:"product_id"` // 商品ID
|
||||
ProductName string `json:"product_name"` // 商品名称
|
||||
CoinConsumeAmount json.Number `json:"coin_consume_amount"` // 金币消耗数量
|
||||
CoinBalanceBefore json.Number `json:"coin_balance_before"` // 消耗前金币余额
|
||||
CoinBalanceAfter json.Number `json:"coin_balance_after"` // 消耗后金币余额
|
||||
ConsumeReasonKey string `json:"consume_reason_key"` // 消耗原因
|
||||
ConsumeReasonName string `json:"consume_reason_name"` // 消耗原因名称
|
||||
CreateTime json.Number `json:"create_time"` // 订单创建时间
|
||||
OrderId string `json:"order_id"` // 订单id
|
||||
|
||||
}
|
||||
|
||||
// VideoPurchaseEvent 视频购买事件
|
||||
type VideoPurchaseEvent struct {
|
||||
VideoEventCore
|
||||
CoinQuantity json.Number `json:"coin_quantity"` // 金币数量
|
||||
OrderID string `json:"order_id"` // 订单号
|
||||
}
|
||||
|
||||
type VideoEventCore struct {
|
||||
MediaID string `json:"media_id"` // 老司机媒体资源ID(老司机接口返回的id字段)
|
||||
VideoID string `json:"video_id"` // 视频ID
|
||||
VideoTitle string `json:"video_title"` // 视频标题
|
||||
VideoTypeID string `json:"video_type_id"` // 视频分类ID
|
||||
VideoTypeName string `json:"video_type_name"` // 视频分类名称
|
||||
VideoContentType string `json:"video_content_type"` // 视频类型: video(长视频)、short_video(短视频)
|
||||
RecommendTraceID string `json:"recommend_trace_id"` // 推荐引擎的trace_id。没有对接搜索推荐引擎的上报空字符串。
|
||||
}
|
||||
|
||||
type VideoLikeEvent struct {
|
||||
VideoEventCore
|
||||
Flag json.Number `json:"flag"` // 点赞状态:1(点赞), 2(取消点赞)
|
||||
}
|
||||
|
||||
type VideoCollectEvent struct {
|
||||
VideoEventCore
|
||||
Flag json.Number `json:"flag"` // 收藏状态:1(收藏), 2(取消收藏)
|
||||
}
|
||||
|
||||
type VideoCommentEvent struct {
|
||||
VideoEventCore
|
||||
CommentContent string `json:"comment_content"` // 评论内容
|
||||
}
|
||||
|
||||
type VideoStatusChangeEvent struct {
|
||||
MediaID string `json:"media_id"` // 老司机媒体资源ID(老司机接口返回的id字段)
|
||||
VideoID string `json:"video_id"` // 视频ID
|
||||
VideoTitle string `json:"video_title"` // 视频标题
|
||||
VideoTypeID string `json:"video_type_id"` // 视频分类ID
|
||||
VideoTypeName string `json:"video_type_name"` // 视频分类名称
|
||||
Flag json.Number `json:"flag"` // 1(上架), 2(下架), 3(审核通过), 4(审核拒绝)
|
||||
}
|
||||
|
||||
// AdClickEvent 广告点击
|
||||
type AdClickEvent struct {
|
||||
PageKey string `json:"page_key"` // 页面标识
|
||||
PageName string `json:"page_name"` // 页面名称
|
||||
AdSlotKey string `json:"ad_slot_key"` // 广告位标识
|
||||
AdSlotName string `json:"ad_slot_name"` // 广告位名称
|
||||
AdID string `json:"ad_id"` // 广告ID
|
||||
CreativeID string `json:"creative_id"` // 素材ID(可选)
|
||||
AdType string `json:"ad_type"` // 广告类型
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
// 漫画相关上报
|
||||
//
|
||||
//
|
||||
|
||||
type (
|
||||
ComicEventPub struct {
|
||||
MediaId string `json:"media_id"` // 老司机媒体资源ID(老司机接口返回的id字段)
|
||||
ComicId string `json:"comic_id"` // 漫画 id
|
||||
ComicTitle string `json:"comic_title"` // 漫画标题
|
||||
ComicTypeId string `json:"comic_type_id"` // 分类ID
|
||||
ComicTypeName string `json:"comic_type_name"` // 分类名称
|
||||
RecommendTraceId string `json:"recommend_trace_id"` // 推荐引擎的trace_id。没有对接搜索推荐引擎的上报空字符串。
|
||||
}
|
||||
|
||||
// ComicPurchaseEvent 漫画购买
|
||||
ComicPurchaseEvent struct {
|
||||
ComicEventPub
|
||||
CoinQuantity json.Number `json:"coin_quantity"` // 金币数量
|
||||
OrderId string `json:"order_id"` // 订单号
|
||||
PageNo json.Number `json:"page_no"` // 事件发生在第几页(如果在列表页当前字段设置为0)
|
||||
}
|
||||
|
||||
// ComicCollectOrLikeEvent 漫画收藏/点赞
|
||||
ComicCollectOrLikeEvent struct {
|
||||
ComicEventPub
|
||||
Flag json.Number `json:"flag"` //收藏状态:1(收藏), 2(取消收藏)
|
||||
PageNo json.Number `json:"page_no"` // 事件发生在第几页(如果在列表页当前字段设置为0)
|
||||
}
|
||||
|
||||
// ComicCommentEvent 漫画评论
|
||||
ComicCommentEvent struct {
|
||||
ComicEventPub
|
||||
CommentContent string `json:"comment_content"` // 评论内容
|
||||
PageNo json.Number `json:"page_no"` // 事件发生在第几页(如果在列表页当前字段设置为0)
|
||||
}
|
||||
)
|
||||
|
||||
//
|
||||
//
|
||||
// 小说相关上报
|
||||
//
|
||||
//
|
||||
|
||||
type (
|
||||
NovelEventPub struct {
|
||||
MediaId string `json:"media_id"` // 老司机媒体资源ID(老司机接口返回的id字段)
|
||||
NovelId string `json:"novel_id"` // 小说 id
|
||||
NovelTitle string `json:"novel_title"` // 小说标题
|
||||
NovelTypeId string `json:"novel_type_id"` // 分类ID
|
||||
NovelTypeName string `json:"novel_type_name"` // 分类名称
|
||||
RecommendTraceId string `json:"recommend_trace_id"` // 推荐引擎的trace_id。没有对接搜索推荐引擎的上报空字符串。
|
||||
}
|
||||
|
||||
// NovelPurchaseEvent 小说购买
|
||||
NovelPurchaseEvent struct {
|
||||
NovelEventPub
|
||||
CoinQuantity json.Number `json:"coin_quantity"` // 金币数量
|
||||
OrderId string `json:"order_id"` // 订单号
|
||||
PageNo json.Number `json:"page_no"` // 事件发生在第几页(如果在列表页当前字段设置为0)
|
||||
}
|
||||
|
||||
// NovelCollectOrLikeEvent 小说收藏/点赞
|
||||
NovelCollectOrLikeEvent struct {
|
||||
NovelEventPub
|
||||
Flag json.Number `json:"flag"` // 收藏状态:1(收藏), 2(取消收藏)
|
||||
PageNo json.Number `json:"page_no"` // 事件发生在第几页(如果在列表页当前字段设置为0)
|
||||
}
|
||||
|
||||
// NovelCommentEvent 小说评论
|
||||
NovelCommentEvent struct {
|
||||
NovelEventPub
|
||||
CommentContent string `json:"comment_content"` // 评论内容
|
||||
PageNo json.Number `json:"page_no"` // 事件发生在第几页(如果在列表页当前字段设置为0)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user