85 lines
2.9 KiB
TypeScript
85 lines
2.9 KiB
TypeScript
import { Dictionary, RawResponse } from './Result';
|
|
|
|
export class APIUtil {
|
|
public static test(): void {
|
|
var api = new APIUtil();
|
|
var queryMap = new Dictionary();
|
|
var url = "https://liap.condowlogp.com/api/getUserInfo?merchantId=&serviceId=";
|
|
var authorization = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNWY5MTFmNWMtMGZmYS00NjhlLTgxYjgtOWQ4ODUxMjdmYmYzIiwidG9rZW5faWQiOiJlZTFlMTgwNC03YWIxLTQyN2MtYWM2Zi1iYjEwMzFiZTczYjIiLCJpYXQiOjE2MDYyNjkyNzN9.I6U2c3tBwMWM5ZcFg3-N_uIFontQzX85blEw2h2L4esfFRa6R4oCXBibEGF9xITcMIJTxICI3renSbFVnOACM_9TAMIdbE-W6pdtfKqRTlpL2fXRITZMAN69_anAr5Y1PJGtgzslDEE1rFlAmL-aOjoaserBHA8XuD8TRYz9VlL4gGPu7sNdiFmhtHF2iATcmgeif8kLpodAXK3zPvWEqXIhXALZTK31U6GQo0WUwm7DELReuYUcDlLWROsLdHpI913EvJY7bY9T1NzSEDDpHXgCnFVvtOpix0-Ip7RGYTzQd4bYxx3pFJmU8qLPi1Rugw4CsbWVi7CdQIcZnEr-qg";
|
|
|
|
api.requestAPI(
|
|
url, queryMap, authorization, "", _ => {
|
|
console.log(_);
|
|
});
|
|
}
|
|
|
|
public requestAPI(
|
|
url: string,
|
|
queryStringMap: Dictionary,
|
|
authorization: string,
|
|
body: string,
|
|
callback: (result: RawResponse) => void): void
|
|
{
|
|
var _url = url;
|
|
var buf = "";
|
|
// 將query 拼接成字串
|
|
if (Object.keys(queryStringMap).length > 0)
|
|
{
|
|
for (var key in queryStringMap) {
|
|
var value = queryStringMap[key];
|
|
|
|
if (buf.length > 0) {
|
|
buf = buf + "&";
|
|
}
|
|
|
|
buf = `${buf}${key}=${value}`;
|
|
}
|
|
|
|
_url = `${_url}?${buf}`;
|
|
}
|
|
|
|
var method = body.length == 0 ? "GET" : "POST";
|
|
|
|
var xhr = cc.loader.getXMLHttpRequest();
|
|
xhr.onreadystatechange = function() {
|
|
if (xhr.readyState !== 4)
|
|
return;
|
|
|
|
//cc.log('xhr.readyState=' + xhr.readyState + ' xhr.status=' + xhr.status);
|
|
|
|
var rawResponse = new RawResponse();
|
|
rawResponse.StatusCode = xhr.status;
|
|
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
rawResponse.Exception = "";
|
|
rawResponse.Data = xhr.responseText;
|
|
}
|
|
else {
|
|
rawResponse.Exception = xhr.statusText;
|
|
rawResponse.Data = "";
|
|
}
|
|
|
|
callback(rawResponse);
|
|
};
|
|
xhr.onerror = function() {
|
|
var rawResponse = new RawResponse();
|
|
rawResponse.StatusCode = xhr.status;
|
|
rawResponse.Exception = xhr.statusText;
|
|
rawResponse.Data = "";
|
|
|
|
callback(rawResponse);
|
|
};
|
|
xhr.open(method, _url, true);
|
|
|
|
if (authorization.length > 0) {
|
|
xhr.setRequestHeader("Authorization", authorization);
|
|
}
|
|
|
|
if (body.length > 0) {
|
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
}
|
|
|
|
xhr.send(body);
|
|
}
|
|
}
|