Commit e330edd0 authored by liujiangnan's avatar liujiangnan

feat: 改造中间层

parent a4a0c910
......@@ -6,23 +6,31 @@ cc.Class({
properties: {
},
callNetworkApiPost(uri, data, callBack) {
this.getBaseUrl((baseUrl) => {
async callNetworkApiPost(uri, data, baseUrl = null) {
if (!baseUrl) {
baseUrl = await this.getBaseUrl();
}
await (new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url = `${baseUrl}${uri}`;
xhr.open("POST", url, true);
xhr.setRequestHeader('content-type', 'application/json');
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
callBack(JSON.parse(xhr.responseText));
resolve(JSON.parse(xhr.responseText));
} else {
reject(xhr.responseText);
}
}
xhr.send(JSON.stringify(data));
});
}));
},
callNetworkApiGet(uri, data, callBack) {
this.getBaseUrl((baseUrl) => {
async callNetworkApiGet(uri, data, baseUrl = null) {
if (!baseUrl) {
baseUrl = await this.getBaseUrl();
}
await (new Promise((resolve, reject) => {
let queryStr = '?';
const params = [];
for (const key in data) {
......@@ -34,25 +42,27 @@ cc.Class({
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
callBack(JSON.parse(xhr.responseText));
resolve(JSON.parse(xhr.responseText));
} else {
reject(xhr.responseText);
}
};
const url = `${baseUrl}${uri}${queryStr}`;
console.warn('url = ' + url);
xhr.open('GET', url, true);
xhr.send();
});
}));
},
async getBaseUrl(callback) {
async getBaseUrl() {
const engineInfo = await this.getEngineInfo();
const { isDev, uuid } = JSON.parse(engineInfo);
this.isDev = isDev;
this.engineCode = uuid;
if (isDev == 1) {
callback('http://staging-openapi.iteachabc.com');
return 'http://api.iplayabc.com';
} else {
callback('http://openapi.iteachabc.com');
return 'http://api.iplayabc.com';
}
},
......@@ -67,20 +77,6 @@ cc.Class({
});
}
},
asyncCallNetworkApiGet(apiName, data) {
return new Promise((resolve, reject) => {
this.callNetworkApiGet(apiName, data, (res => {
resolve(res);
}));
});
},
asyncCallNetworkApiPost(uri, data) {
return new Promise((resolve, reject) => {
this.callNetworkApiPost(uri, data, (res) => {
resolve(res);
});
});
},
onMiddleLayerLoadingFinish() {
if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
......@@ -160,7 +156,7 @@ cc.Class({
this.currentSceneName = bundleName;
this.showWaitingLetters();
const data = await this.asyncCallNetworkApiGet(`/api/template/v1/${bundleName}`, {});
const data = await this.callNetworkApiGet(`/api/template/v1/${bundleName}`, {});
let configData = null;
if (cc.sys.os == cc.sys.OS_IOS) {
configData = data.data.conf.ios;
......
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