@ -2,10 +2,14 @@ package internal
import (
"context"
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/encoding/gcharset"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"log"
"strings"
"time"
"tyj_admin/api/v1/game"
"tyj_admin/internal/consts"
@ -85,44 +89,119 @@ func sendMsg(ctx context.Context, url string, eventType, amount int32) {
return
}
func sendMsgHugeAmount ( ctx context . Context , url , platform , idfv , androidId , packageName string ) {
//url := "https://analytics.oceanengine.com/sdk/app/attribution"
func sendMsgHugeAmount ( ctx context . Context , url string , data map [ string ] interface { } ) ( string , error ) {
g . Client ( ) . SetHeader ( "Content-Type" , "application/json;charset=UTF-8" )
data := map [ string ] string {
"platform" : platform , // ios或android
"idfv" : idfv , // 仅ios需要
"android_id" : androidId , // 仅android需要
"package_name" : packageName ,
"customer_active_time" : fmt . Sprint ( time . Now ( ) . UnixMicro ( ) ) , // 毫秒时间戳,客户激活归因时间点
}
bytes , _ := g . Client ( ) . Post ( ctx , url , data )
g . Log ( ) . Info ( ctx , "sendMsg - res: " , bytes )
return
log . Printf ( "sendMsgHugeAmount - url: %s, data: %s" , url , gjson . MustEncodeString ( data ) )
marshal , _ := json . Marshal ( data )
bytes , err := g . Client ( ) . Post ( ctx , url , string ( marshal ) )
if err != nil {
return "" , err
}
g . Log ( ) . Info ( ctx , "sendMsgHugeAmount - res: " , bytes )
src := string ( bytes . ReadAll ( ) )
tmp , err := gcharset . ToUTF8 ( "UTF-8" , src )
if err != nil {
return "" , err
}
fmt . Println ( "Deposit - json: " , tmp )
return tmp , err
}
func AttributionHugeAmount ( ctx context . Context , req * game . ATHAReq ) ( res * game . ATHARes , err error ) {
log . Printf ( "Advertise: %s" , gjson . MustEncodeString ( req ) )
//g.Try(ctx, func(ctx context.Context) {
// model := dao.Advertisement.Ctx(ctx)
// _, err = model.Insert(do.Advertisement{Idfa: req.Idfa, Time: req.Time, Ip: req.Ip, OrgId: req.OrgId, OrgName: req.OrgName,
// GameId: req.GameId, GameName: req.GameName, AdsetId: req.AdsetId, AdsetNet: req.AdsetNet, DeviceBrand: req.DeviceBrand,
// DeviceModel: req.DeviceModel, CreativeId: req.CreativeId, ConversionType: req.ConversionType, Device: req.Device,
// OAID: req.OAID, Callback: req.Callback, TapProjectId: req.TapProjectId, TapTrackId: req.TapTrackId, IMEI: req.IMEI,
// ANID: req.ANID})
//})
log . Printf ( "AttributionHugeAmount: %s" , gjson . MustEncodeString ( req ) )
data := map [ string ] interface { } {
"platform" : req . Platform , // ios或android
"package_name" : req . PackageName ,
"customer_active_time" : fmt . Sprint ( time . Now ( ) . UnixMilli ( ) ) , // 毫秒时间戳,客户激活归因时间点
}
if req . Platform == "android" {
data [ "android_id" ] = req . AndroidId
} else if req . Platform == "ios" {
data [ "idfv" ] = req . Idfv
}
url := "https://analytics.oceanengine.com/sdk/app/attribution"
tmp , err := sendMsgHugeAmount ( ctx , url , data )
if err != nil {
return
}
json , err := gjson . DecodeToJson ( tmp )
if err != nil {
return
}
if json . Get ( "code" ) . Int ( ) == 0 {
callbackUrl := json . Get ( "callback_url" ) . String ( )
callbackParam := json . Get ( "callback_param" ) . String ( )
lastTouchTime := json . Get ( "last_touch_time" ) . Int64 ( )
model := dao . AdvertisementOceanegine . Ctx ( ctx )
_ , _ = model . Insert ( do . AdvertisementOceanegine { AdvAndroidId : req . AndroidId , AdvIdfv : req . Idfv , CallbackUrl : callbackUrl , CallbackParam : callbackParam , LastTouchTime : lastTouchTime } )
} else {
return nil , gerror . New ( "获取失败" )
}
return
}
func ConversionHugeAmount ( ctx context . Context , req * game . CSHAReq ) ( res * game . CSHARes , err error ) {
log . Printf ( "Advertise: %s" , gjson . MustEncodeString ( req ) )
//g.Try(ctx, func(ctx context.Context) {
// model := dao.Advertisement.Ctx(ctx)
// _, err = model.Insert(do.Advertisement{Idfa: req.Idfa, Time: req.Time, Ip: req.Ip, OrgId: req.OrgId, OrgName: req.OrgName,
// GameId: req.GameId, GameName: req.GameName, AdsetId: req.AdsetId, AdsetNet: req.AdsetNet, DeviceBrand: req.DeviceBrand,
// DeviceModel: req.DeviceModel, CreativeId: req.CreativeId, ConversionType: req.ConversionType, Device: req.Device,
// OAID: req.OAID, Callback: req.Callback, TapProjectId: req.TapProjectId, TapTrackId: req.TapTrackId, IMEI: req.IMEI,
// ANID: req.ANID})
//})
log . Printf ( "ConversionHugeAmount: %s" , gjson . MustEncodeString ( req ) )
adData := [ ] entity . AdvertisementOceanegine { }
device := map [ string ] interface { } {
"platform" : req . Platform ,
}
model := dao . AdvertisementOceanegine . Ctx ( ctx ) . Order ( "last_touch_time desc" )
if req . Platform == "android" {
_ = model . Where ( "adv_android_id=?" , req . Id ) . Scan ( & adData )
device [ "android_id" ] = req . Id
} else if req . Platform == "ios" {
_ = model . Where ( "adv_idfv=?" , req . Id ) . Scan ( & adData )
device [ "idfv" ] = req . Id
}
ad := map [ string ] interface { } {
"callback" : adData [ 0 ] . CallbackParam ,
}
ct := map [ string ] interface { } {
"ad" : ad ,
"device" : device ,
}
data := map [ string ] interface { } {
"event_type" : req . EventType ,
"context" : ct ,
"timestamp" : time . Now ( ) . UnixMilli ( ) ,
}
url := "https://analytics.oceanengine.com/api/v2/conversion"
_ , _ = sendMsgHugeAmount ( ctx , url , data )
return
}
func AdvertiseHugeAmount ( ctx context . Context , req * game . AdvertiseHAReq ) ( res * game . AdvertiseHARes , err error ) {
req . Platform = strings . ToLower ( req . Platform )
log . Printf ( "AdvertiseHugeAmount: %s" , gjson . MustEncodeString ( req ) )
if req . EventType == "active" {
req1 := game . ATHAReq {
Platform : req . Platform ,
PackageName : req . PackageName ,
}
if req . Platform == "android" {
req1 . AndroidId = req . Id
} else if req . Platform == "ios" {
req1 . Idfv = req . Id
} else {
return
}
_ , err = AttributionHugeAmount ( ctx , & req1 )
if err != nil {
return
}
}
req2 := game . CSHAReq {
Platform : req . Platform ,
Id : req . Id ,
EventType : req . EventType ,
}
req2 . Id = req . Id
_ , err = ConversionHugeAmount ( ctx , & req2 )
return
}