You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
273 lines
8.7 KiB
273 lines
8.7 KiB
7 months ago
|
"use strict";
|
||
|
var __defProp = Object.defineProperty;
|
||
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
||
|
var __publicField = (obj, key, value) => {
|
||
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
||
|
return value;
|
||
|
};
|
||
|
const common_vendor = require("../../common/vendor.js");
|
||
|
const enums_requestEnums = require("../../enums/requestEnums.js");
|
||
|
const utils_request_cancel = require("./cancel.js");
|
||
|
function decodeUTF8(arr) {
|
||
|
let str = "";
|
||
|
for (let i = 0; i < arr.length; i++) {
|
||
|
str += String.fromCharCode(arr[i]);
|
||
|
}
|
||
|
return decodeURIComponent(escape(str));
|
||
|
}
|
||
|
function isStreamResponse(header) {
|
||
|
const contentType = (header == null ? void 0 : header["Content-Type"]) || (header == null ? void 0 : header["content-type"]) || header.get("content-type");
|
||
|
if (typeof contentType !== "string")
|
||
|
return false;
|
||
|
return contentType.includes("text/event-stream");
|
||
|
}
|
||
|
class HttpRequest {
|
||
|
constructor(options) {
|
||
|
__publicField(this, "options");
|
||
|
this.options = options;
|
||
|
}
|
||
|
/**
|
||
|
* @description 重新请求
|
||
|
*/
|
||
|
retryRequest(options, config) {
|
||
|
var _a;
|
||
|
const { retryCount, retryTimeout } = config;
|
||
|
if (!retryCount || ((_a = options.method) == null ? void 0 : _a.toUpperCase()) == enums_requestEnums.RequestMethodsEnum.POST) {
|
||
|
return Promise.reject();
|
||
|
}
|
||
|
common_vendor.index.showLoading({ title: "加载中..." });
|
||
|
config.hasRetryCount = config.hasRetryCount || 0;
|
||
|
if (config.hasRetryCount >= retryCount) {
|
||
|
return Promise.reject();
|
||
|
}
|
||
|
config.hasRetryCount++;
|
||
|
config.requestHooks.requestInterceptorsHook = (options2) => options2;
|
||
|
return new Promise((resolve) => setTimeout(resolve, retryTimeout)).then(() => this.request(options, config)).finally(() => common_vendor.index.hideLoading());
|
||
|
}
|
||
|
/**
|
||
|
* @description get请求
|
||
|
*/
|
||
|
get(options, config) {
|
||
|
return this.request(
|
||
|
{ ...options, method: enums_requestEnums.RequestMethodsEnum.GET },
|
||
|
config
|
||
|
);
|
||
|
}
|
||
|
/**
|
||
|
* @description post请求
|
||
|
*/
|
||
|
post(options, config) {
|
||
|
return this.request(
|
||
|
{ ...options, method: enums_requestEnums.RequestMethodsEnum.POST },
|
||
|
config
|
||
|
);
|
||
|
}
|
||
|
/**
|
||
|
* @description 事件流
|
||
|
*/
|
||
|
eventStream(options, config) {
|
||
|
let mergeOptions = common_vendor.merge(
|
||
|
{},
|
||
|
this.options.requestOptions,
|
||
|
options
|
||
|
);
|
||
|
const mergeConfig = common_vendor.merge(
|
||
|
{},
|
||
|
this.options,
|
||
|
config
|
||
|
);
|
||
|
const { requestInterceptorsHook, responseInterceptorsHook } = mergeConfig.requestHooks || {};
|
||
|
if (requestInterceptorsHook && common_vendor.isFunction(requestInterceptorsHook)) {
|
||
|
mergeOptions = requestInterceptorsHook(
|
||
|
mergeOptions,
|
||
|
mergeConfig
|
||
|
);
|
||
|
}
|
||
|
const { onmessage, onclose, onstart } = mergeConfig;
|
||
|
return new Promise((resolve, reject) => {
|
||
|
try {
|
||
|
let header = {};
|
||
|
const requestTask = common_vendor.index.request({
|
||
|
...mergeOptions,
|
||
|
enableChunked: true,
|
||
|
responseType: "arraybuffer",
|
||
|
async success(response) {
|
||
|
if (response.statusCode !== 200) {
|
||
|
reject(response);
|
||
|
return;
|
||
|
}
|
||
|
resolve(response);
|
||
|
},
|
||
|
fail(error) {
|
||
|
reject(error);
|
||
|
},
|
||
|
complete() {
|
||
|
onclose == null ? void 0 : onclose();
|
||
|
}
|
||
|
});
|
||
|
onstart == null ? void 0 : onstart(requestTask);
|
||
|
requestTask.onHeadersReceived((response) => {
|
||
|
header = response.header;
|
||
|
});
|
||
|
requestTask.onChunkReceived(async (response) => {
|
||
|
const arrayBuffer = response.data;
|
||
|
const uint8Array = new Uint8Array(arrayBuffer);
|
||
|
const str = decodeUTF8(uint8Array);
|
||
|
if (isStreamResponse(header)) {
|
||
|
onmessage == null ? void 0 : onmessage(str);
|
||
|
} else {
|
||
|
const data = common_vendor.parse(str);
|
||
|
if (responseInterceptorsHook && common_vendor.isFunction(responseInterceptorsHook)) {
|
||
|
try {
|
||
|
const response2 = await responseInterceptorsHook(
|
||
|
{ data, statusCode: 200 },
|
||
|
mergeConfig,
|
||
|
mergeOptions
|
||
|
);
|
||
|
resolve(response2);
|
||
|
} catch (error) {
|
||
|
reject(error);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
} catch (e) {
|
||
|
reject();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
/**
|
||
|
* @description 上传图片
|
||
|
*/
|
||
|
uploadFile(options, config) {
|
||
|
let mergeOptions = common_vendor.merge(
|
||
|
{},
|
||
|
this.options.requestOptions,
|
||
|
options
|
||
|
);
|
||
|
const mergeConfig = common_vendor.merge({}, this.options, config);
|
||
|
const {
|
||
|
requestInterceptorsHook,
|
||
|
responseInterceptorsHook,
|
||
|
responseInterceptorsCatchHook
|
||
|
} = mergeConfig.requestHooks || {};
|
||
|
if (requestInterceptorsHook && common_vendor.isFunction(requestInterceptorsHook)) {
|
||
|
mergeOptions = requestInterceptorsHook(mergeOptions, mergeConfig);
|
||
|
}
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const task = common_vendor.index.uploadFile({
|
||
|
...mergeOptions,
|
||
|
header: {
|
||
|
...mergeOptions.header
|
||
|
},
|
||
|
async success(response) {
|
||
|
if (response.statusCode == 200) {
|
||
|
response.data = common_vendor.parse(response.data);
|
||
|
if (responseInterceptorsHook && common_vendor.isFunction(responseInterceptorsHook)) {
|
||
|
try {
|
||
|
response = await responseInterceptorsHook(
|
||
|
response,
|
||
|
mergeConfig,
|
||
|
mergeOptions
|
||
|
);
|
||
|
resolve(response);
|
||
|
} catch (error) {
|
||
|
reject(error);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
resolve(response);
|
||
|
} else {
|
||
|
reject(response.errMsg);
|
||
|
}
|
||
|
},
|
||
|
fail(err) {
|
||
|
if (err.errMsg == enums_requestEnums.RequestErrMsgEnum.ABORT) {
|
||
|
return;
|
||
|
}
|
||
|
if (responseInterceptorsCatchHook && common_vendor.isFunction(responseInterceptorsCatchHook)) {
|
||
|
reject(responseInterceptorsCatchHook(mergeOptions, err));
|
||
|
return;
|
||
|
}
|
||
|
reject(err.errMsg || err);
|
||
|
}
|
||
|
});
|
||
|
task.onProgressUpdate(({ progress }) => {
|
||
|
var _a;
|
||
|
(_a = mergeConfig.onProgress) == null ? void 0 : _a.call(mergeConfig, progress);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
/**
|
||
|
* @description 请求函数
|
||
|
*/
|
||
|
async request(options, config) {
|
||
|
let mergeOptions = common_vendor.merge(
|
||
|
{},
|
||
|
this.options.requestOptions,
|
||
|
options
|
||
|
);
|
||
|
const mergeConfig = common_vendor.merge({}, this.options, config);
|
||
|
const {
|
||
|
requestInterceptorsHook,
|
||
|
responseInterceptorsHook,
|
||
|
responseInterceptorsCatchHook
|
||
|
} = mergeConfig.requestHooks || {};
|
||
|
if (requestInterceptorsHook && common_vendor.isFunction(requestInterceptorsHook)) {
|
||
|
mergeOptions = requestInterceptorsHook(mergeOptions, mergeConfig);
|
||
|
}
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const requestTask = common_vendor.index.request({
|
||
|
...mergeOptions,
|
||
|
async success(response) {
|
||
|
if (response.statusCode !== 200) {
|
||
|
return reject();
|
||
|
}
|
||
|
if (responseInterceptorsHook && common_vendor.isFunction(responseInterceptorsHook)) {
|
||
|
try {
|
||
|
response = await responseInterceptorsHook(
|
||
|
response,
|
||
|
mergeConfig,
|
||
|
mergeOptions
|
||
|
);
|
||
|
resolve(response);
|
||
|
} catch (error) {
|
||
|
reject(error);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
resolve(response);
|
||
|
},
|
||
|
fail: async (err) => {
|
||
|
if (err.errMsg == enums_requestEnums.RequestErrMsgEnum.TIMEOUT) {
|
||
|
this.retryRequest(mergeOptions, mergeConfig).then((res) => resolve(res)).catch((err2) => reject(err2));
|
||
|
return;
|
||
|
}
|
||
|
if (err.errMsg == enums_requestEnums.RequestErrMsgEnum.ABORT) {
|
||
|
return;
|
||
|
}
|
||
|
if (responseInterceptorsCatchHook && common_vendor.isFunction(responseInterceptorsCatchHook)) {
|
||
|
reject(
|
||
|
await responseInterceptorsCatchHook(
|
||
|
mergeOptions,
|
||
|
err
|
||
|
)
|
||
|
);
|
||
|
return;
|
||
|
}
|
||
|
reject(err);
|
||
|
},
|
||
|
complete(err) {
|
||
|
if (err.errMsg !== enums_requestEnums.RequestErrMsgEnum.ABORT) {
|
||
|
utils_request_cancel.requestCancel.remove(options.url);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
const { ignoreCancel } = mergeConfig;
|
||
|
!ignoreCancel && utils_request_cancel.requestCancel.add(options.url, requestTask);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
exports.HttpRequest = HttpRequest;
|