Commit 9c385b37 authored by liujiangnan's avatar liujiangnan

feat: 日志采集和请求重试

parent 90cee03c
......@@ -37,10 +37,13 @@ export function initAir(_this) {
startScanCodeCallback: null,
needUpdateCallback: null,
getEngineInfoCallback: null,
logFileUploadCallback: null,
takePictureCallBack: null,
configurationPortraitCallback: null,
configurationLandscapeCallback: null,
openPhotoAlbumCallback: null,
startBuyToAppStoreCallBack: null,
fileSaveCallback: null,
recognitionCameraCallBack: null,
testCallBack: null,
testConvertCallBack: null,
......@@ -286,6 +289,19 @@ export function initAir(_this) {
};
window.courseware.callOcMethod("getEngineInfo", {});
},
logUpload(callback) {
_this.log(`===成功调用logUpload===`);
// 调用原生获取机器信息
window.air.logFileUploadCallback = (res) => {
if(typeof(res) == 'string') {
callback && callback(res);
} else {
callback && callback(JSON.stringify(res));
}
window.air.logFileUploadCallback = null;
};
window.courseware.callOcMethod("logUpload", {});
},
behaviorToCollectWithParam(event, type="") {
// 埋点所需接口 levelPayForSuccess userRegistrationEvent
window.courseware.callOcMethod("behaviorToCollectWithParam", {event, type});
......@@ -376,6 +392,16 @@ export function initAir(_this) {
};
window.courseware.callOcMethod("onConfigurationLandscape", params);
},
openPhotoAlbum(params = {}, callback) {
_this.log(`===成功调用openPhotoAlbum===${JSON.stringify(params)}`);
window.air.openPhotoAlbumCallback = (res) => {
res = typeof(res) == 'string' ? res : JSON.stringify(res);
_this.log(`===成功调用openPhotoAlbumCallback=== res == ${res}`);
callback && callback(res);
window.air.openPhotoAlbumCallback = null;
};
window.courseware.callOcMethod("openPhotoAlbum", params);
},
startBuyToAppStore(params = {}, callback) {
// 苹果支付
_this.log(`===成功调用startBuyToAppStore===`);
......@@ -384,6 +410,17 @@ export function initAir(_this) {
};
window.courseware.callOcMethod("startBuyToAppStore", params);
},
downloadFileAndSave(params = {}, callback) {
// 苹果支付
_this.log(`===成功调用downloadFileAndSave===`);
window.air.fileSaveCallback = (res) => {
res = typeof(res) == 'string' ? res : JSON.stringify(res);
_this.log(`===成功调用fileSaveCallback=== res == ${res}`);
callback && callback(res);
window.air.fileSaveCallback = null;
};
window.courseware.callOcMethod("downloadFileAndSave", params);
},
callOcMethod(method, param) {
const paramStr = JSON.stringify(param);
_this.log(`===成功调用原生方法${method}===参数==${paramStr}`);
......@@ -397,6 +434,8 @@ export function initAir(_this) {
console.error(`非源生环境,网页版不支持${method}方法,将返回此方法的Mock数据以便于网页版调试使用`);
if(method == 'getEngineInfo'){
window.air.getEngineInfoCallback({isDev: 1, uuid: 'debug-xxxxxx-xxxxxx', version: '1.0.2'});
} else if(method == 'logUpload'){
window.air.logFileUploadCallback({"fileUrl":"abcedfgh","error":""});
} else if (method == 'startTest') {
} else if (method == 'stopTest') {
window.air.testCallBack(testMockData);
......
......@@ -23,6 +23,7 @@ export default class NewClass extends middleLayerBase {
initAir(this);
this.reWriteAir();
(<any>window).courseware.getEngineInfo();
this.role = 'student'; // 上古遗留问题
......@@ -35,7 +36,7 @@ export default class NewClass extends middleLayerBase {
return;
}
const token = cc.sys.localStorage.getItem('token');
const { code } = await this.asyncCallNetworkApiGet("/api/jj/v1/heart", {token});
const { code } = await this.asyncCallNetworkApiGet("/api/kyxq/v1/heart", {token});
if (code != 200) {
global.middleLayer.showTips("登录过期,请重新登录");
this.loginPage.active = true;
......@@ -48,11 +49,38 @@ export default class NewClass extends middleLayerBase {
}
async navLogCaller() {
const { data } = await this.asyncCallNetworkApiGet("/api/application/v1/engine/report", {
engineid: this.ENGINE_INFO.uuid,
packagename: 'com.iplayabc.kyxq'
});
if (data && data == true) {
(<any>window).courseware.logUpload(async (res) => {
const {fileUrl, error} = JSON.parse(res);
if (fileUrl) {
await this.asyncCallNetworkApiPost("/api/application/v1/engine/report", {
engineid: this.ENGINE_INFO.uuid,
packagename: 'com.iplayabc.kyxq',
file_name: fileUrl
});
}
});
}
this.scheduleOnce(()=>{
this.navLogCaller();
}, 5);
}
async start() {
this.node.zIndex = 9999;
cc.game.addPersistRootNode(this.node);
this.initListener();
this.scheduleOnce(()=>{
// 启动日志召回监听服务
this.navLogCaller();
}, 5);
const token = cc.sys.localStorage.getItem('token');
if (!token) {
// this.loginPage.active = true;
......
......@@ -45,10 +45,11 @@ export abstract class middleLayerBase extends cc.Component {
}
}
callNetworkApiPost(uri, data, callBack) {
callNetworkApiPost(uri, data, callBack, sendHook = null) {
const token = cc.sys.localStorage.getItem('token');
data = {...data, token};
this.getEngineInfo().then((engineInfo) => {
data.engine_uuid = engineInfo['uuid'];
const xhr = new XMLHttpRequest();
const url = `${engineInfo["apiBase"]}${uri}`;
xhr.open("POST", url, true);
......@@ -59,10 +60,11 @@ export abstract class middleLayerBase extends cc.Component {
}
}
xhr.send(JSON.stringify(data));
sendHook && sendHook(xhr);
});
}
callNetworkApiGet(uri, data, callBack) {
callNetworkApiGet(uri, data, callback, sendHook = null) {
const token = cc.sys.localStorage.getItem('token');
data = {...data, token};
this.getEngineInfo().then((engineInfo) => {
......@@ -75,28 +77,54 @@ export abstract class middleLayerBase extends cc.Component {
}
queryStr += params.join("&");
const xhr = new XMLHttpRequest();
let flag = false;
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
callBack(JSON.parse(xhr.responseText));
flag = true;
callback(JSON.parse(xhr.responseText));
}
};
const url = `${engineInfo["apiBase"]}${uri}${queryStr}`;
console.log('url = ' + url);
sendHook && sendHook(xhr);
xhr.open('GET', url, true);
xhr.send();
// 五秒超时重试一次
if (sendHook) {
// 有hook的无法重试请求
return;
}
this.scheduleOnce(() => {
if (flag) {
return;
}
xhr.abort();
this.callNetworkApiGet(uri, data, callback);
}, 5);
});
}
getConfigInfo(url, callback) {
const xhr = new XMLHttpRequest();
let flag = false;
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
flag = true;
callback(JSON.parse(xhr.responseText));
}
};
console.log('url = ' + `${url}/config.json`);
xhr.open('GET', `${url}/config.json`, true);
xhr.send();
// 五秒超时重试一次
this.scheduleOnce(() => {
if (flag) {
return;
}
xhr.abort();
this.getConfigInfo(url, callback);
}, 5);
}
ENGINE_INFO;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment