Commit a9d6beac authored by 范雪寒's avatar 范雪寒

feat: network

parent 5319d5b5
......@@ -459,3 +459,65 @@ export function jumpToCourseWare(courseWareId: number) {
console.log('jump to CourseWare: ' + courseWareId);
}
}
export function asyncCallNetworkApiGet(apiName, data): Promise<any> {
return new Promise((resolve, reject) => {
callNetworkApiGet(apiName, data, (res => {
resolve(res);
}));
});
}
export function asyncCallNetworkApiPost(uri, data): Promise<any> {
return new Promise((resolve, reject) => {
callNetworkApiPost(uri, data, (res) => {
resolve(res);
});
});
}
export function callNetworkApiPost(uri, data, callBack) {
const middleLayer = cc.find('middleLayer')?.getComponent('middleLayer');
if (middleLayer) {
middleLayer.callNetworkApiPost(uri, data, callBack);
return;
}
const baseUrl = 'http://staging-openapi.iteachabc.com';
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));
}
}
xhr.send(JSON.stringify(data));
}
export function callNetworkApiGet(uri, data, callBack) {
const middleLayer = cc.find('middleLayer')?.getComponent('middleLayer');
if (middleLayer) {
middleLayer.callNetworkApiGet(uri, data, callBack);
return;
}
const baseUrl = 'http://staging-openapi.iteachabc.com';
let queryStr = '?';
const params = [];
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
params.push(`${key}=${data[key]}`);
}
}
queryStr += params.join("&");
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
callBack(xhr.responseText);
}
};
const url = `${baseUrl}${uri}${queryStr}`;
console.log('url = ' + url);
xhr.open('GET', url, true);
xhr.send();
}
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