Commit 8524ec1d authored by limingzhe's avatar limingzhe

fix: debug

parent 00de0ce8
...@@ -487,6 +487,135 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -487,6 +487,135 @@ export abstract class middleLayerBase extends cc.Component {
}); });
} }
asyncCallNetworkApiGetNew(uri, data): Promise<any> {
return new Promise((resolve, reject) => {
this.callNetworkApiGetNew(uri, data, (res => {
resolve(res);
}));
});
}
callNetworkApiPostNew(uri, data, callBack) {
const token = cc.sys.localStorage.getItem('student_token');
data = {...data, token};
this.getEngineInfo().then((engineInfo) => {
const xhr = new XMLHttpRequest();
const url = `${engineInfo["apiBase"]}${uri}`;
xhr.open("POST", url, true);
xhr.setRequestHeader('content-type', 'application/json');
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
callBack(JSON.parse(xhr.responseText));
}
}
xhr.setRequestHeader("token", token)
xhr.send(JSON.stringify(data));
});
}
async callNetworkApiGetNew(uri, data, callBack) {
const token = cc.sys.localStorage.getItem('student_token');
const engineInfo = await this.getEngineInfo();
data = {...data};
let queryStr = '?';
const params = [];
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
params.push(`${key}=${data[key]}`);
}
}
if (params.length > 0) {
queryStr += params.join("&");
} else {
queryStr = '';
}
const url = `${engineInfo["apiBase"]}${uri}${queryStr}`;
// 如果请求失败 最多尝试5次重连
for(let i=0; i<5; i++) {
const result = await this.newXMLHttpRequest(url, token)
if(result != null) {
// 正常响应
callBack(result)
break
} else {
// 5秒有没有响应 - 重新请求
console.log(`请求失败,重试第${i+1}次`)
}
}
}
ENGINE_INFO;
getEngineInfo() {
return new Promise((resolve, reject) => {
if (this.ENGINE_INFO) {
resolve(this.ENGINE_INFO);
return;
}
const loopEngineInfo = () => {
this.scheduleOnce(() => {
if (!(<any>window).air.engineInfo) {
loopEngineInfo();
return;
}
if (this.ENGINE_INFO) {
resolve(this.ENGINE_INFO);
return;
}
const engineInfo = JSON.parse((<any>window).air.engineInfo);
if (engineInfo.isDev == 1) {
engineInfo.domain = "http://staging-teach.cdn.ireadabc.com/";
engineInfo.apiBase = 'http://staging-jianj.iteachabc.com';
engineInfo.orgId = 521;
} else {
engineInfo.domain = "http://teach.cdn.ireadabc.com/";
engineInfo.apiBase = 'http://jianj.iteachabc.com';//'http://openapi.iteachabc.com';
engineInfo.orgId = 519;
}
this.ENGINE_INFO = engineInfo;
resolve(engineInfo);
}, 0.05);
};
loopEngineInfo();
});
}
newXMLHttpRequest(url, token=null) {
return new Promise((resovle, reject) => {
const xhr = new XMLHttpRequest();
// 设置定时器 5秒终止连接
let timeoutId = setTimeout(() => {
xhr.abort();
resovle(null)
}, 5000);
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
// 如果有定时器, 清除定时器
if(timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
resovle(JSON.parse(xhr.responseText));
}
};
console.log('url = ' + url);
xhr.open('GET', url, true);
if (token) {
xhr.setRequestHeader("token", token)
}
xhr.send();
})
}
onDestroy() { onDestroy() {
this.isDestroy = true; this.isDestroy = true;
} }
......
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