Commit e330edd0 authored by liujiangnan's avatar liujiangnan

feat: 改造中间层

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