197 lines
5.2 KiB
Go
197 lines
5.2 KiB
Go
|
package adjust
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"sandc/pkg/bhttp"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/log"
|
||
|
)
|
||
|
|
||
|
// AdjustEventParams 上报事件参数
|
||
|
type AdjustEventParams struct {
|
||
|
GpsAdid string `json:"gps_adid"`
|
||
|
Adid string `json:"adid"`
|
||
|
AndroidId string `json:"android_id"`
|
||
|
IpAddress string `json:"ip_address"`
|
||
|
CreatedAtUnix string `json:"created_at_unix"`
|
||
|
Currency string `json:"currency"`
|
||
|
Environment string `json:"environment"`
|
||
|
UserAgent string `json:"user_agent"`
|
||
|
Price string `json:"price"`
|
||
|
FailReason string `json:"fail_reason"`
|
||
|
AppToken string `json:"app_token"`
|
||
|
EventToken string `json:"event_token"`
|
||
|
S2S string `json:"s2s"`
|
||
|
ClientName string `json:"client_name"` // 客户端包名
|
||
|
// Idfa string `json:"idfa"`
|
||
|
// FireAdid string `json:"fire_adid"`
|
||
|
// Oaid string `json:"oaid"`
|
||
|
// Idfv string `json:"idfv"`
|
||
|
// Revenue string `json:"revenue"`
|
||
|
}
|
||
|
|
||
|
// AdjustClient adjust客户端接口
|
||
|
type AdjustClient interface {
|
||
|
// ReportEvent 上报事件
|
||
|
ReportEvent(ctx context.Context, opts *AdjustEventParams) error
|
||
|
}
|
||
|
|
||
|
// adjustClient adjust客户端
|
||
|
type adjustClient struct {
|
||
|
appToken string
|
||
|
s2sToken string
|
||
|
env string
|
||
|
service *bhttp.BhttpService
|
||
|
}
|
||
|
|
||
|
// NewAdjustClient 创建adjust客户端
|
||
|
func NewAdjustClient(appToken, s2sToken, env string) (AdjustClient, error) {
|
||
|
service, err := bhttp.NewBhttpService(bhttp.WithTimeout(30))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
service.Client.SetHeader("Content-Type", "application/x-www-form-urlencoded")
|
||
|
service.Client.SetParam("app_token", appToken)
|
||
|
if s2sToken != "" {
|
||
|
service.Client.SetHeader("Authorization", "Bearer "+s2sToken)
|
||
|
service.Client.SetParam("s2s", "1")
|
||
|
}
|
||
|
|
||
|
return &adjustClient{
|
||
|
appToken: appToken,
|
||
|
s2sToken: s2sToken,
|
||
|
env: env,
|
||
|
service: service,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
type ReportEventRes struct {
|
||
|
Error string `json:"error,omitempty"`
|
||
|
Status string `json:"status,omitempty"`
|
||
|
}
|
||
|
|
||
|
// ReportEvent 上报事件
|
||
|
func (ac *adjustClient) ReportEvent(ctx context.Context, opts *AdjustEventParams) error {
|
||
|
// if opts.Idfa != "" && validateUUID(opts.Idfa) {
|
||
|
// ac.service.Client.SetParam("idfa", opts.Idfa)
|
||
|
// }
|
||
|
|
||
|
if opts.GpsAdid != "" {
|
||
|
// 判断兼容客户端问题
|
||
|
if !validateUUID(opts.GpsAdid) {
|
||
|
// if opts.GpsAdid == opts.Oaid {
|
||
|
// opts.Oaid = ""
|
||
|
// }
|
||
|
opts.GpsAdid = ""
|
||
|
} else {
|
||
|
ac.service.Client.SetParam("gps_adid", opts.GpsAdid)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// if opts.FireAdid != "" {
|
||
|
// ac.service.Client.SetParam("fire_adid", opts.FireAdid)
|
||
|
// }
|
||
|
|
||
|
// if opts.Oaid != "" {
|
||
|
// ac.service.Client.SetParam("oaid", opts.Oaid)
|
||
|
// }
|
||
|
|
||
|
if opts.Adid != "" {
|
||
|
ac.service.Client.SetParam("adid", opts.Adid)
|
||
|
if opts.GpsAdid == "" {
|
||
|
ac.service.Client.SetParam("gps_adid", opts.Adid)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// if opts.Idfv != "" {
|
||
|
// ac.service.Client.SetParam("idfv", opts.Idfv)
|
||
|
// } else {
|
||
|
// if opts.Idfa != "" {
|
||
|
// ac.service.Client.SetParam("idfv", opts.Idfa)
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
if opts.AndroidId != "" {
|
||
|
ac.service.Client.SetParam("android_id", opts.AndroidId)
|
||
|
}
|
||
|
|
||
|
ac.service.Client.SetParam("event_token", opts.EventToken)
|
||
|
ac.service.Client.SetParam("ip_address", opts.IpAddress)
|
||
|
ac.service.Client.SetParam("created_at_unix", opts.CreatedAtUnix)
|
||
|
ac.service.Client.SetParam("user_agent", opts.UserAgent)
|
||
|
ac.service.Client.SetParam("price", opts.Price)
|
||
|
ac.service.Client.SetParam("fail_reason", opts.FailReason)
|
||
|
ac.service.Client.SetParam("client_name", opts.ClientName)
|
||
|
// ac.service.Client.SetParam("revenue", opts.Revenue)
|
||
|
|
||
|
// 兼容之前的数据错误
|
||
|
if strings.Contains(opts.Currency, "CNY") {
|
||
|
opts.Currency = "CNY"
|
||
|
} else if strings.Contains(opts.Currency, "USD") {
|
||
|
opts.Currency = "USD"
|
||
|
}
|
||
|
|
||
|
ac.service.Client.SetParam("currency", opts.Currency)
|
||
|
|
||
|
if opts.Environment == "prod" {
|
||
|
ac.service.Client.SetParam("environment", "production")
|
||
|
} else {
|
||
|
ac.service.Client.SetParam("environment", "sandbox")
|
||
|
}
|
||
|
|
||
|
numMax := 1
|
||
|
for i := 0; i < numMax; i++ {
|
||
|
body, err := ac.service.Client.DoPost("https://s2s.adjust.com/event")
|
||
|
sendData := ac.service.Client.GetReader()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("adjust server err: %s, reader: %s", err, sendData)
|
||
|
}
|
||
|
|
||
|
// 临时记录请求参数
|
||
|
// utils.PrintLog("adjust-request-params: %s \n", ac.service.Client.GetReader())
|
||
|
// utils.PrintLog("adjust-response-body: %s \n", string(body))
|
||
|
log.Infof("adjust-request-params: i[%d] %s \n", i, sendData)
|
||
|
log.Infof("adjust-response-body: i[%d] %s \n", i, string(body))
|
||
|
|
||
|
var res ReportEventRes
|
||
|
err = json.Unmarshal(body, &res)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("adjust Unmarshal err: %s, reader: %s", res.Error, sendData)
|
||
|
}
|
||
|
|
||
|
if res.Error != "" {
|
||
|
return fmt.Errorf("adjust res err: %s, reader: %s", res.Error, sendData)
|
||
|
}
|
||
|
|
||
|
if res.Status == "OK" {
|
||
|
log.Infof("adjust send successed: i[%d] reader: %s", i, sendData)
|
||
|
break
|
||
|
} else {
|
||
|
log.Infof("adjust res status error: %s, i[%d] reader: %s", res.Status, i, sendData)
|
||
|
|
||
|
if i+1 >= numMax {
|
||
|
return fmt.Errorf("adjust res status err: %s, reader: %s", res.Status, sendData)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// validateUUID 验证uuid的有效性
|
||
|
func validateUUID(uuid string) bool {
|
||
|
if len(uuid) != 36 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if strings.ToLower(uuid) == "00000000-0000-0000-0000-000000000000" {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|