大侠
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

372 lines
15 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class ChannelUpload {
static data = {
// 后台地址
url : "https://ht.liumuchui.cn/thirdparty/wx/game/",
clue_token: "", // 抖音渠道clue_token标志
callback: "", // 快手渠道callback标志
openid : null, // 微信返回的玩家open_id
appid: null, // 应用appid
session_key: null, // 微信会话密钥session_key
unionid : null, // 用户在开放平台的唯一标识符
errmsg: null, // 错误信息
errcode: null, // 错误码
tiktokLaunchobj : null,
ksLaunchobj : null,
videocount : 0,
ksVideocount : 0,
first: false,
first_channel: "wx", // 用户首次进来的渠道来源
channel: "wx", // 用户渠道来源
loginOK: false,
}
// 建议放在微信的sdk脚本中初始化
static Init(appid, isfirstlogin) {
let t = this.data;
let obj = wx.getLaunchOptionsSync();
let first = false
if (obj.referrerInfo) {
console.log("channel获取首次启动来源:",obj)
first = true
} else {
obj = wx.getEnterOptionsSync();
console.log("channel获取启动来源:",obj)
}
if (obj.referrerInfo) {
if (obj.query.callback != null) {
if (first) this.data.first_channel = "kuai_shou"
this.data.channel = "kuai_shou"
this.data.callback = obj.query.callback
this.data.ksLaunchobj = obj
console.log("channel从快手平台进入")
}
if (obj.query.clue_token != null) {
if (first) this.data.first_channel = "tiktok"
this.data.channel = "tiktok"
this.data.clue_token = obj.query.clue_token
this.data.tiktokLaunchobj = obj
console.log("channel从抖音平台进入")
}
}
this.data.appid = appid;
this.data.first = isfirstlogin;
console.log("channel后台游戏appid:", this.data.appid);
// 进行用户登录
this.data.appid && this.userLogin();
}
// 用户登录
static userLogin() {
wx.login({
success: (res) => {
if (res.code) {
// 连接后台获取玩家openid,data填写后台游戏id
var loginobj = {
app_id: this.data.appid,
js_code: res.code,
}
console.log("channel用户微信登录成功,code:", res.code);
this.httpPost('open_id/', loginobj, (event) => {
if(event.error && event.error.length > 0){
console.error("channel用户后台登录失败,event.error:", event.error);
this.data.loginOK = false;
return;
}
if(event.data == "" || event.data.errcode || event.data.errmsg){
console.error("channel用户后台登录失败,event.data:", event.data);
this.data.loginOK = false;
return;
}
console.log("channel用户后台登录成功,返回的响应内容:", event);
this.data.loginOK = true;
this.data.session_key = event.data.session_key;
// this.data.unionid = event.data.unionid;
this.data.openid = event.data.openid;
// 检查用户是否首次登陆
if (this.data.first) {
console.log("channel首次登录用户,触发激活事件:",this.data.channel);
if (this.data.channel == "tiktok") {
this.oceanReportEvent("0");
} else if (this.data.channel == "kuai_shou") {
this.oceanReportEvent("1");
}
}
});
} else {
console.log('channel登录失败!' + res.errMsg);
}
}
})
}
// 成功观看视频
static videoAdd() {
if (this.data.channel === "tiktok"){
console.log("channel抖音渠道-开始上报事件25");
this.oceanReportEvent("25");
}
if (this.data.channel === "kuai_shou"){
console.log("channel快手渠道-开始上报事件143");
this.oceanReportEvent("143");
}else{
console.log("channel,wx渠道-触发videoAdd上报事件");
}
}
// 事件上报
static async oceanReportEvent(event, params) {
if (!this.data.loginOK) {
console.log("channel用户未登录");
return
}
if (this.data.openid == null) {
console.log("channel用户的open id为空");
return
}
if (this.data.channel !== 'tiktok' && this.data.channel !== 'kuai_shou') {
console.log("channel用户的channel不正确:",this.data.channel);
return
}
console.log("channel事件上报, 渠道为: "+ this.data.channel +",事件类型:", event);
let obj = {
token: this.data.channel == "tiktok" ? this.data.clue_token : this.data.callback,
open_id: this.data.openid,
app_id: this.data.appid,
event_type: event,
session_key: this.data.session_key,
}
// 上报抖音
if ((event == "0" || event == "25") && this.data.tiktokLaunchobj != null) {
console.log("channel验证数据通过1");
let query = this.data.tiktokLaunchobj.query;
if (query) {
this.httpPost('upload_action/', obj, null);
if(event == "25"){
console.log("channel,event=25开始请求预测数据...");
try {
const predictionData = await this.requestPrediction("default");
// console.log("预测数据获取成功:", predictionData)
if(predictionData && predictionData.encryptedData && predictionData.iv){
obj.encryptedData = predictionData.encryptedData;
obj.iv = predictionData.iv;
obj.event_type = "100";
this.httpPost('upload_action/', obj, null);
}
} catch(error) {
console.log("channel,event=25获取预测数据失败", error);
}
}
console.log("channel验证数据通过2");
// if (query.clue_token != null) {
// console.log("channel发送巨量事件:", obj);
// this.httpPost('upload_action/', obj, null);
// }
}
}
// 上报快手
if ((event == "1" || event == "143") && this.data.ksLaunchobj != null) {
console.log("ks验证数据通过1");
let query = this.data.ksLaunchobj.query;
if (query) {
if(event == "143"){
obj.watch_video_count = this.data.videocount
console.log("event=143,ks开始请求预测数据...");
try {
const predictionData = await this.requestPrediction("default");
if(predictionData && predictionData.encryptedData && predictionData.iv){
obj.encryptedData = predictionData.encryptedData;
obj.iv = predictionData.iv;
}
} catch(error) {
console.log("获取预测数据失败", error);
obj.prediction_data = "{}";
}
}
console.log("ks验证数据通过2");
if (query.callback != null) {
}
}
}
}
// 请求预测档位
static requestPrediction(scene, params) {
return new Promise((resolve, reject) => {
if (window.IAASDK && window.IAASDK.default) {
console.log('channel请求IAA预测档位...');
window.IAASDK.default.getUserLabel(scene, (result) => {
if (result.success) {
console.log('channel预测档位获取成功,返回加密数据:', result);
// 检查是否有加密数据需要解密
if (result.encryptedData && result.iv) {
resolve(result);
// this.decryptIAAData(result).then(decryptedData => {
// resolve(decryptedData);
// }).catch(error => {
// reject(error);
// });
} else {
console.log('channel没有加密数据,跳过解密步骤');
resolve(null);
}
} else {
console.log('channel预测档位获取失败:', result.error);
reject(result.error);
}
});
} else {
console.log('IAA SDK未初始化');
resolve(null);
}
});
}
// 解密IAA数据
static decryptIAAData(iaaResult) {
return new Promise((resolve, reject) => {
if (!this.data.openid) {
console.log('openid未获取,无法解密IAA数据');
reject('openid未获取');
return;
}
console.log('开始解密IAA数据...');
// 构建发送到后端的加密数据对象
const encryptDataObj = {
gameId: this.data.appid,
openId: this.data.wxOpenId,
encryptedData: iaaResult.encryptedData,
iv: iaaResult.iv
};
console.log('发送到后端解密的数据:', encryptDataObj);
// 发送到后端解密
this.httpPost('gameUser/v2/getGameDecryptData', encryptDataObj, (response) => {
if (response && response.code === 200) {
console.log('IAA数据解密成功:', response.data);
const processedData = this.handleDecryptedIAAData(response.data, iaaResult);
resolve(processedData);
} else {
console.log('IAA数据解密失败:', response);
reject('解密失败');
}
});
});
}
// 处理解密后的IAA数据
static handleDecryptedIAAData(decryptedData, originalResult) {
try {
console.log('=== IAA解密数据处理 ===');
console.log('原始IAA结果:', originalResult);
console.log('解密后数据:', decryptedData);
// 解析解密后的数据
let parsedData = decryptedData.decryptData;
if (typeof parsedData === 'string') {
parsedData = JSON.parse(parsedData);
}
console.log('', parsedData);
// 保存预测结果到本地存储
const predictionRecord = {
timestamp: Date.now(),
scene: originalResult.scene,
rawData: parsedData,
gameUser: decryptedData.gameUser
};
wx.setStorageSync('latest_iaa_prediction', JSON.stringify(predictionRecord));
console.log('IAA预测结果已保存到本地存储');
// 返回处理后的数据
return {
predictionData: parsedData,
gameUser: decryptedData.gameUser,
scene: originalResult.scene
};
} catch (error) {
console.error('处理解密数据时出错:', error);
throw error;
}
}
static httpGet(Url, obj) {
let url = this.data.url + Url;
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', url, true);
httpRequest.setRequestHeader("Content-type", "application/json");
let token = localStorage.getItem('token');
let platform = localStorage.getItem('platform');
httpRequest.setRequestHeader("Authorization", 'Bearer ' + token);
httpRequest.setRequestHeader('platform', platform);
if (obj == '') {
httpRequest.send();
} else {
httpRequest.send(JSON.stringify(obj));
}
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
let json = httpRequest.responseText;
let objJson = JSON.parse(json);
console.log('get请求返回数据', objJson);
} else if (httpRequest.status != 200) {
console.log('网络异常,请重启游戏');
}
};
}
static httpPost(Url, obj, callback) {
let url = this.data.url + Url;
console.log("channel发送请求,url:", url, " || data:", obj);
let httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url, true);
let token = localStorage.getItem('token');
let platform = localStorage.getItem('platform');
httpRequest.setRequestHeader("Content-type", "application/json");
httpRequest.setRequestHeader("Authorization", 'Bearer ' + token);
httpRequest.setRequestHeader('platform', platform);
httpRequest.send(JSON.stringify(obj));
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
let json = httpRequest.responseText;
let objJson = JSON.parse(json);
console.log('post请求返回数据', objJson);
if (callback != null) {
callback(objJson);
}
} else if (httpRequest.status != 200) {
console.log('网络异常,请重启游戏');
}
};
}
static getday() {
let e = new Date().getTime();
return Math.ceil((e) / 864e5);
}
}
exports.default = ChannelUpload;