102 lines
3.1 KiB
JavaScript
102 lines
3.1 KiB
JavaScript
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/index.tsx
|
|
var src_exports = {};
|
|
__export(src_exports, {
|
|
reqConfig: () => reqConfig
|
|
});
|
|
module.exports = __toCommonJS(src_exports);
|
|
var import_antd = require("antd");
|
|
var reqConfig = (config) => {
|
|
const {
|
|
authorization,
|
|
showMsg = true,
|
|
onError
|
|
} = config || {};
|
|
return {
|
|
timeout: 1e3,
|
|
baseURL: location.origin,
|
|
errorConfig: {
|
|
// @ts-ignore
|
|
errorHandler(error) {
|
|
onError == null ? void 0 : onError(error);
|
|
import_antd.message.error("网络异常");
|
|
},
|
|
// @ts-ignore
|
|
errorThrower(res) {
|
|
console.log("报错啦:", res);
|
|
}
|
|
},
|
|
// 请求
|
|
requestInterceptors: [
|
|
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
|
[
|
|
(url, options) => {
|
|
const Authorization = authorization || localStorage.getItem("ZHST_AUTH_TOKEN") || "";
|
|
return {
|
|
url,
|
|
options: {
|
|
...options,
|
|
headers: {
|
|
Authorization
|
|
}
|
|
}
|
|
};
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error);
|
|
}
|
|
]
|
|
],
|
|
// 返回
|
|
responseInterceptors: [
|
|
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
|
[
|
|
(response) => {
|
|
var _a;
|
|
const { code } = (response == null ? void 0 : response.data) || {};
|
|
if ([403, 401, 203].includes(code)) {
|
|
import_antd.message.error("登录过期,请重新登录");
|
|
onError == null ? void 0 : onError(response == null ? void 0 : response.data);
|
|
return {};
|
|
}
|
|
if (showMsg && code !== 200) {
|
|
import_antd.message.error(((_a = response.data) == null ? void 0 : _a.message) || "请求失败!");
|
|
return {};
|
|
}
|
|
return response.data || {};
|
|
},
|
|
(error) => {
|
|
var _a;
|
|
const { code } = ((_a = error.response) == null ? void 0 : _a.data) || {};
|
|
if (showMsg && code !== 200) {
|
|
import_antd.message.error(error.response.data.message || "请求失败!");
|
|
}
|
|
return Promise.reject(error.response);
|
|
}
|
|
]
|
|
],
|
|
...config
|
|
};
|
|
};
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
reqConfig
|
|
});
|