373 lines
12 KiB
Go
373 lines
12 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
v1 "sandc/api/eonline/v1"
|
|
"sandc/app/eonline/internal/biz/adjust"
|
|
"sandc/app/eonline/internal/biz/shushu"
|
|
"sandc/app/eonline/internal/conf"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
)
|
|
|
|
var gEnv string
|
|
var appConfig *conf.AppConfig
|
|
|
|
func InitReport(bootstrap *conf.Bootstrap) {
|
|
gEnv = strings.ToLower(bootstrap.Server.Env)
|
|
appConfig = bootstrap.AppConfig
|
|
|
|
log.Infof("InitReport gEnv[%s] AdjustId[%s] AdjustS2SToken[%s] AdjustEventTokenSuccess[%s] AdjustEventTokenFail[%s] SsAppId[%s]",
|
|
gEnv, appConfig.AdjustAppToken, appConfig.AdjustS2SToken, appConfig.AdjustEventTokenSuccess, appConfig.AdjustEventTokenFail, appConfig.SsAppId)
|
|
|
|
var err error
|
|
|
|
_, err = newAdjustClient()
|
|
if err != nil {
|
|
log.Fatalf("InitReport: adjust.NewAdjustClient err[%+v]", err)
|
|
}
|
|
|
|
_, err = newShuShuClient()
|
|
if err != nil {
|
|
log.Fatalf("InitReport: shushu.NewClient err[%+v]", err)
|
|
}
|
|
}
|
|
|
|
func GetAdjustData(req *v1.PayoutReq, errStr, ip string) *adjust.AdjustEventParams {
|
|
if req.DataAdjust == nil {
|
|
log.Infof("GetAdjustData error: req.DataAdjust is nil Deviceid[%s]", req.Deviceid)
|
|
return nil
|
|
}
|
|
|
|
data := &adjust.AdjustEventParams{
|
|
GpsAdid: req.DataAdjust.GpsAdid,
|
|
Adid: req.DataAdjust.Adid,
|
|
AndroidId: req.DataAdjust.AndroidId,
|
|
IpAddress: ip,
|
|
CreatedAtUnix: strconv.Itoa(int(time.Now().Unix())),
|
|
Currency: req.DataAdjust.Currency,
|
|
Environment: gEnv,
|
|
UserAgent: req.DataAdjust.UserAgent,
|
|
Price: req.DataAdjust.Price,
|
|
FailReason: errStr,
|
|
AppToken: appConfig.AdjustAppToken,
|
|
EventToken: appConfig.AdjustEventTokenSuccess,
|
|
S2S: "1",
|
|
ClientName: req.ClientName,
|
|
// AppToken: "5l2aubga4by8",
|
|
// EventToken: "xlwfxq",
|
|
}
|
|
|
|
if len(errStr) > 0 {
|
|
// data.EventToken = "t4c6tj"
|
|
data.EventToken = appConfig.AdjustEventTokenFail
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
func GetAdjustData2(req *v1.PbAdjustData, errStr string) *adjust.AdjustEventParams {
|
|
if req == nil {
|
|
log.Infof("GetAdjustData2 error: req is nil")
|
|
return nil
|
|
}
|
|
|
|
data := &adjust.AdjustEventParams{
|
|
GpsAdid: req.GpsAdid,
|
|
Adid: req.Adid,
|
|
AndroidId: req.AndroidId,
|
|
IpAddress: req.IpAddress,
|
|
CreatedAtUnix: req.CreatedAtUnix,
|
|
Currency: req.Currency,
|
|
Environment: gEnv,
|
|
UserAgent: req.UserAgent,
|
|
Price: req.Price,
|
|
FailReason: errStr,
|
|
AppToken: req.AppToken,
|
|
EventToken: req.EventToken,
|
|
S2S: req.S2S,
|
|
ClientName: req.ClientName,
|
|
}
|
|
if len(errStr) > 0 {
|
|
// data.EventToken = "t4c6tj"
|
|
data.EventToken = appConfig.AdjustEventTokenFail
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
func getReportKey(key string) string {
|
|
return fmt.Sprintf("report:%s", key)
|
|
}
|
|
|
|
func AddReportData(key string, adjustEventParams *adjust.AdjustEventParams, iapReport *shushu.SSIapProperties) {
|
|
data := &v1.PbReportData{
|
|
Adjust: &v1.PbAdjustData{
|
|
GpsAdid: adjustEventParams.GpsAdid,
|
|
Adid: adjustEventParams.Adid,
|
|
AndroidId: adjustEventParams.AndroidId,
|
|
IpAddress: adjustEventParams.IpAddress,
|
|
CreatedAtUnix: adjustEventParams.CreatedAtUnix,
|
|
Currency: adjustEventParams.Currency,
|
|
Environment: adjustEventParams.Environment,
|
|
UserAgent: adjustEventParams.UserAgent,
|
|
Price: adjustEventParams.Price,
|
|
FailReason: adjustEventParams.FailReason,
|
|
AppToken: adjustEventParams.AppToken,
|
|
EventToken: adjustEventParams.EventToken,
|
|
S2S: adjustEventParams.S2S,
|
|
ClientName: adjustEventParams.ClientName,
|
|
},
|
|
ShuShu: &v1.PbShuShuData{
|
|
GpsAdid: iapReport.GpsAdid,
|
|
AppToken: iapReport.AppToken,
|
|
EventToken: iapReport.EventToken,
|
|
S2S: iapReport.S2S,
|
|
AndroidId: iapReport.AndroidId,
|
|
Adid: iapReport.Adid,
|
|
IpAddress: iapReport.IpAddress,
|
|
CreatedAtUnix: iapReport.CreatedAtUnix,
|
|
UserAgent: iapReport.UserAgent,
|
|
Price: iapReport.Price,
|
|
Currency: iapReport.Currency,
|
|
FailReason: iapReport.FailReason,
|
|
PayoutId: iapReport.PayoutId,
|
|
MerchantReference: iapReport.MerchantReference,
|
|
PaymentMethod: iapReport.PaymentMethod,
|
|
PaymentType: iapReport.PaymentType,
|
|
PaymentNumber: iapReport.PaymentNumber,
|
|
IapName: iapReport.IapName,
|
|
GamecoinNumber: iapReport.GamecoinNumber,
|
|
GamecoinType: iapReport.GamecoinType,
|
|
SsAccountId: iapReport.SsAccountId,
|
|
SsDistinctId: iapReport.SsDistinctId,
|
|
SsSuperProperties: iapReport.SsSuperProperties,
|
|
ClientName: iapReport.ClientName,
|
|
},
|
|
Rf: uint32(time.Now().Unix()),
|
|
}
|
|
|
|
// AddReportOne(key, data)
|
|
|
|
key = getReportKey(key)
|
|
report := NewReportData(db_name, key)
|
|
*report.GetData(true) = *data
|
|
report.Expire(15 * 3600) // 提现平台最后一次发送回调消息是14小时
|
|
err := report.Save()
|
|
if err != nil {
|
|
log.Infof("report.Save error: key[%s] error[%+v]", key, err)
|
|
}
|
|
}
|
|
|
|
func GetReportData(key string) *v1.PbReportData {
|
|
// return GetReportOne(key)
|
|
|
|
key = getReportKey(key)
|
|
report := NewReportData(db_name, key)
|
|
err := report.Load()
|
|
if err != nil {
|
|
log.Infof("report.Load error: key[%s] error[%+v]", key, err)
|
|
return nil
|
|
}
|
|
|
|
data := report.GetData(false)
|
|
err = report.Delete()
|
|
if err != nil {
|
|
log.Infof("report.Delete error: key[%s] error[%+v]", key, err)
|
|
}
|
|
return data
|
|
}
|
|
|
|
func SendReport(ctx context.Context, adjustEventParams *adjust.AdjustEventParams, iapReport *shushu.SSIapProperties) {
|
|
if adjustEventParams == nil || iapReport == nil {
|
|
return
|
|
}
|
|
|
|
err1 := sendAdjustReport(ctx, adjustEventParams)
|
|
if err1 != nil {
|
|
log.Infof("adjust send err[%+v]", err1)
|
|
}
|
|
|
|
err2 := sendShuShuReport(iapReport)
|
|
if err2 != nil {
|
|
log.Infof("shushu send err[%+v]", err2)
|
|
}
|
|
|
|
if err1 == nil && err2 == nil {
|
|
log.Infof("SendReport successed: GpsAdid[%s]", adjustEventParams.GpsAdid)
|
|
}
|
|
}
|
|
|
|
func newAdjustClient() (adjust.AdjustClient, error) {
|
|
// return adjust.NewAdjustClient("5l2aubga4by8", "7ea35d86e3e6688c2debcadc4efd7230", gEnv)
|
|
return adjust.NewAdjustClient(appConfig.AdjustAppToken, appConfig.AdjustS2SToken, gEnv)
|
|
}
|
|
|
|
func sendAdjustReport(ctx context.Context, adjustEventParams *adjust.AdjustEventParams) error {
|
|
adjustClient, err := newAdjustClient()
|
|
if err != nil {
|
|
return fmt.Errorf("adjust.NewAdjustClient error: %v", err)
|
|
}
|
|
// 上报IAP数据
|
|
err = adjustClient.ReportEvent(ctx, adjustEventParams)
|
|
if err != nil {
|
|
return fmt.Errorf("adjustClient.ReportEvent error: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetShuShuData(req *v1.PayoutReq, errStr, ip string) *shushu.SSIapProperties {
|
|
if req.DataAdjust == nil {
|
|
log.Infof("GetAdjustData error: req.DataAdjust is nil Deviceid[%s]", req.Deviceid)
|
|
return nil
|
|
}
|
|
|
|
data := &shushu.SSIapProperties{
|
|
// EventToken: "xlwfxq",
|
|
// AppToken: "5l2aubga4by8",
|
|
GpsAdid: req.DataAdjust.GpsAdid,
|
|
AppToken: appConfig.AdjustAppToken,
|
|
EventToken: appConfig.AdjustEventTokenSuccess,
|
|
S2S: "1",
|
|
AndroidId: req.DataAdjust.AndroidId,
|
|
Adid: req.DataAdjust.Adid,
|
|
IpAddress: ip,
|
|
CreatedAtUnix: strconv.Itoa(int(time.Now().Unix())),
|
|
UserAgent: req.DataAdjust.UserAgent,
|
|
Price: req.DataAdjust.Price,
|
|
Currency: req.DataAdjust.Currency,
|
|
FailReason: errStr,
|
|
PayoutId: "",
|
|
MerchantReference: "",
|
|
PaymentMethod: req.DataShuShu.PaymentMethod,
|
|
PaymentType: req.DataShuShu.PaymentType,
|
|
PaymentNumber: req.DataShuShu.PaymentNumber,
|
|
IapName: req.DataShuShu.IapName,
|
|
GamecoinNumber: req.DataShuShu.GamecoinNumber,
|
|
GamecoinType: req.DataShuShu.GamecoinType,
|
|
SsAccountId: req.DataShuShu.SsAccountId,
|
|
SsDistinctId: req.DataShuShu.SsDistinctId,
|
|
SsSuperProperties: req.DataShuShu.SsSuperProperties,
|
|
ClientName: req.ClientName,
|
|
}
|
|
|
|
if len(errStr) > 0 {
|
|
// data.EventToken = "t4c6tj"
|
|
data.EventToken = appConfig.AdjustEventTokenFail
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
func GetShuShuData2(req *v1.PbShuShuData, errStr string) *shushu.SSIapProperties {
|
|
if req == nil {
|
|
log.Infof("GetAdjustData2 error: req is nil")
|
|
return nil
|
|
}
|
|
|
|
data := &shushu.SSIapProperties{
|
|
GpsAdid: req.GpsAdid,
|
|
AppToken: req.AppToken,
|
|
EventToken: req.EventToken,
|
|
S2S: req.S2S,
|
|
AndroidId: req.AndroidId,
|
|
Adid: req.Adid,
|
|
IpAddress: req.IpAddress,
|
|
CreatedAtUnix: req.CreatedAtUnix,
|
|
UserAgent: req.UserAgent,
|
|
Price: req.Price,
|
|
Currency: req.Currency,
|
|
FailReason: errStr,
|
|
PayoutId: req.PayoutId,
|
|
MerchantReference: req.MerchantReference,
|
|
PaymentMethod: req.PaymentMethod,
|
|
PaymentType: req.PaymentType,
|
|
PaymentNumber: req.PaymentNumber,
|
|
IapName: req.IapName,
|
|
GamecoinNumber: req.GamecoinNumber,
|
|
GamecoinType: req.GamecoinType,
|
|
SsAccountId: req.SsAccountId,
|
|
SsDistinctId: req.SsDistinctId,
|
|
SsSuperProperties: req.SsSuperProperties,
|
|
ClientName: req.ClientName,
|
|
}
|
|
|
|
if len(errStr) > 0 {
|
|
// data.EventToken = "t4c6tj"
|
|
data.EventToken = appConfig.AdjustEventTokenFail
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
func newShuShuClient() (shushu.Client, error) {
|
|
// return shushu.NewClient("https://ss.zolnm.com", "3774fd57014846d99ccd145a76780866", gEnv)
|
|
return shushu.NewClient("https://ss.zolnm.com", appConfig.SsAppId, gEnv)
|
|
}
|
|
|
|
func sendShuShuReport(iapReport *shushu.SSIapProperties) error {
|
|
ssClient, err := newShuShuClient()
|
|
if err != nil {
|
|
return fmt.Errorf("shushu.NewClient error: %v", err)
|
|
}
|
|
|
|
propertiesMap := shushu.SSProperties{
|
|
"gps_adid": iapReport.GpsAdid,
|
|
"app_token": iapReport.AppToken,
|
|
"event_token": iapReport.EventToken,
|
|
"s2s": iapReport.S2S,
|
|
"android_id": iapReport.AndroidId,
|
|
"adid": iapReport.Adid,
|
|
"ip_address": iapReport.IpAddress,
|
|
"created_at_unix": iapReport.CreatedAtUnix,
|
|
"user_agent": iapReport.UserAgent,
|
|
"price": iapReport.Price,
|
|
"currency": iapReport.Currency,
|
|
"fail_reason": iapReport.FailReason,
|
|
"payout_id": iapReport.PayoutId,
|
|
"merchant_reference": iapReport.MerchantReference,
|
|
"payment_method": iapReport.PaymentMethod,
|
|
"payment_type": iapReport.PaymentType,
|
|
"payment_number": iapReport.PaymentNumber,
|
|
"iap_name": iapReport.IapName,
|
|
"gamecoin_number": iapReport.GamecoinNumber,
|
|
"gamecoin_type": iapReport.GamecoinType,
|
|
"ss_account_id": iapReport.SsAccountId,
|
|
"ss_distinct_id": iapReport.SsDistinctId,
|
|
"ss_super_properties": iapReport.SsSuperProperties,
|
|
"client_name": iapReport.ClientName,
|
|
}
|
|
|
|
// 上报IAP数据
|
|
if len(iapReport.FailReason) <= 0 {
|
|
eventName := "cash_out"
|
|
if gEnv == "qa" {
|
|
eventName = fmt.Sprintf("%s_qa", eventName)
|
|
}
|
|
err = ssClient.SyncIapData(iapReport.SsAccountId, iapReport.SsDistinctId, eventName, iapReport.IpAddress, propertiesMap)
|
|
if err != nil {
|
|
return fmt.Errorf("ssClient.SyncIapData error: %v", err)
|
|
}
|
|
} else {
|
|
eventName := "cash_fail"
|
|
if gEnv == "qa" {
|
|
eventName = fmt.Sprintf("%s_qa", eventName)
|
|
}
|
|
err = ssClient.ReportError(iapReport.SsAccountId, iapReport.SsDistinctId, eventName, iapReport.IpAddress, propertiesMap)
|
|
if err != nil {
|
|
return fmt.Errorf("ssClient.ReportError error: %v", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CloseReport() {
|
|
log.Infof("CloseReport gEnv[%s] AdjustId[%s] AdjustS2SToken[%s] AdjustEventTokenSuccess[%s] AdjustEventTokenFail[%s] SsAppId[%s]",
|
|
gEnv, appConfig.AdjustAppToken, appConfig.AdjustS2SToken, appConfig.AdjustEventTokenSuccess, appConfig.AdjustEventTokenFail, appConfig.SsAppId)
|
|
}
|