Commit aa24e641 authored by liujiangnan's avatar liujiangnan

feat

parent dcc1d80c
import { initAir } from './air'; import { initAir } from './air';
cc.Class({ cc.Class({
extends: cc.Component, extends: cc.Component,
properties: { properties: {
...@@ -9,7 +9,7 @@ cc.Class({ ...@@ -9,7 +9,7 @@ cc.Class({
if (!baseUrl) { if (!baseUrl) {
baseUrl = await this.getBaseUrl(); baseUrl = await this.getBaseUrl();
} }
await (new Promise((resolve, reject) => { const resData = 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);
...@@ -17,19 +17,18 @@ cc.Class({ ...@@ -17,19 +17,18 @@ cc.Class({
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState == 4) { if (xhr.readyState == 4) {
resolve(JSON.parse(xhr.responseText)); resolve(JSON.parse(xhr.responseText));
} else {
reject(xhr.responseText);
} }
} }
xhr.send(JSON.stringify(data)); xhr.send(JSON.stringify(data));
})); });
return resData;
}, },
async callNetworkApiGet(uri, data, baseUrl = null) { async callNetworkApiGet(uri, data, baseUrl = null) {
if (!baseUrl) { if (!baseUrl) {
baseUrl = await this.getBaseUrl(); baseUrl = await this.getBaseUrl();
} }
await (new Promise((resolve, reject) => { const resData = await new Promise((resolve, reject) => {
let queryStr = '?'; let queryStr = '?';
const params = []; const params = [];
for (const key in data) { for (const key in data) {
...@@ -42,15 +41,14 @@ cc.Class({ ...@@ -42,15 +41,14 @@ cc.Class({
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) { if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
resolve(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();
})); });
return resData;
}, },
async getBaseUrl() { async getBaseUrl() {
......
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