laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
121 lines (120 loc) • 4.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpRequest = void 0;
const Event_1 = require("../events/Event");
const EventDispatcher_1 = require("../events/EventDispatcher");
const Utils_1 = require("../utils/Utils");
const Browser_1 = require("../utils/Browser");
class HttpRequest extends EventDispatcher_1.EventDispatcher {
constructor() {
super(...arguments);
this._http = new XMLHttpRequest();
}
send(url, data = null, method = "get", responseType = "text", headers = null) {
this._responseType = responseType;
this._data = null;
if (Browser_1.Browser.onVVMiniGame || Browser_1.Browser.onQGMiniGame || Browser_1.Browser.onQQMiniGame || Browser_1.Browser.onAlipayMiniGame || Browser_1.Browser.onBLMiniGame || Browser_1.Browser.onHWMiniGame || Browser_1.Browser.onTTMiniGame || Browser_1.Browser.onTBMiniGame) {
url = HttpRequest._urlEncode(url);
}
this._url = url;
var _this = this;
var http = this._http;
http.open(method, url, true);
let isJson = false;
if (headers) {
for (var i = 0; i < headers.length; i++) {
http.setRequestHeader(headers[i++], headers[i]);
}
}
else if (!(window.conch)) {
if (!data || typeof (data) == 'string')
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
else {
http.setRequestHeader("Content-Type", "application/json");
if (!(data instanceof ArrayBuffer) && typeof data !== "string") {
isJson = true;
}
}
}
let restype = responseType !== "arraybuffer" ? "text" : "arraybuffer";
http.responseType = restype;
if (http.dataType) {
http.dataType = restype;
}
http.onerror = function (e) {
_this._onError(e);
};
http.onabort = function (e) {
_this._onAbort(e);
};
http.onprogress = function (e) {
_this._onProgress(e);
};
http.onload = function (e) {
_this._onLoad(e);
};
if (Browser_1.Browser.onBLMiniGame && Browser_1.Browser.onAndroid && !data)
data = {};
http.send(isJson ? JSON.stringify(data) : data);
}
_onProgress(e) {
if (e && e.lengthComputable)
this.event(Event_1.Event.PROGRESS, e.loaded / e.total);
}
_onAbort(e) {
this.error("Request was aborted by user");
}
_onError(e) {
this.error("Request failed Status:" + this._http.status + " text:" + this._http.statusText);
}
_onLoad(e) {
var http = this._http;
var status = http.status !== undefined ? http.status : 200;
if (status === 200 || status === 204 || status === 0) {
this.complete();
}
else {
this.error("[" + http.status + "]" + http.statusText + ":" + http.responseURL);
}
}
error(message) {
this.clear();
console.warn(this.url, message);
this.event(Event_1.Event.ERROR, message);
}
complete() {
this.clear();
var flag = true;
try {
if (this._responseType === "json") {
this._data = JSON.parse(this._http.responseText);
}
else if (this._responseType === "xml") {
this._data = Utils_1.Utils.parseXMLFromString(this._http.responseText);
}
else {
this._data = this._http.response || this._http.responseText;
}
}
catch (e) {
flag = false;
this.error(e.message);
}
flag && this.event(Event_1.Event.COMPLETE, this._data instanceof Array ? [this._data] : this._data);
}
clear() {
var http = this._http;
http.onerror = http.onabort = http.onprogress = http.onload = null;
}
get url() {
return this._url;
}
get data() {
return this._data;
}
get http() {
return this._http;
}
}
exports.HttpRequest = HttpRequest;
HttpRequest._urlEncode = encodeURI;