@fle-ui/plus-table
Version:
176 lines • 8.8 kB
JavaScript
import "antd/es/modal/style";
import _Modal from "antd/es/modal";
import "antd/es/notification/style";
import _notification from "antd/es/notification";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import Cookies from 'js-cookie';
import axios from 'axios';
import { getAePrefix, getAppInfo } from '@fle-tools/env';
var DATA_CODE = new Map([['SUCCESS', '2000'], ['LOGIN_ERROR', '100003']]);
// 登录失效
var CancelToken = axios.CancelToken;
var source = CancelToken.source();
var pending = [];
// 移除重复请求
var removePending = function removePending(config) {
for (var key in pending) {
var item = +key;
var list = pending[key];
// 当前请求在数组中存在时执行函数体
if (list.url === config.url && list.method === config.method && JSON.stringify(list.params) === JSON.stringify(config.params) && JSON.stringify(list.data) === JSON.stringify(config.data)) {
// 执行取消操作
list.cancel('操作太频繁,请稍后再试');
// 从数组中移除记录
pending.splice(item, 1);
}
}
};
// 默认配置
var initAxiosConfig = {
baseUrl: undefined,
timeout: 10000,
tokenKey: 'token',
token: undefined,
loginErrUrl: undefined,
loginErrFn: undefined,
noNotificationCodes: [],
errUrlFn: undefined,
formDataUrls: [] // 需要用formData提交的接口列表
};
var Request = function Request(configParams) {
configParams = _objectSpread(_objectSpread({}, initAxiosConfig), configParams);
// console.log(configParams, 'config')
var service = axios.create({
baseURL: configParams.baseUrl,
timeout: configParams.timeout
});
service.interceptors.request.use(function (config) {
var _configParams$formDat;
removePending(config);
config.headers['authority_token'] = configParams.token || Cookies.get(configParams.tokenKey);
config.url = "".concat(config.url);
if ((_configParams$formDat = configParams.formDataUrls) === null || _configParams$formDat === void 0 ? void 0 : _configParams$formDat.includes(config.url)) {
config.headers['Content-Type'] = 'multipart/form-data';
config.headers['Accept'] = '*/*';
}
return _objectSpread(_objectSpread({}, config), {}, {
cancelToken: source.token
});
}, function (err) {
var _configParams$excepti;
if ((_configParams$excepti = configParams.exceptionErrFn) === null || _configParams$excepti === void 0 ? void 0 : _configParams$excepti.notification) {
var _configParams$excepti2;
(_configParams$excepti2 = configParams.exceptionErrFn) === null || _configParams$excepti2 === void 0 ? void 0 : _configParams$excepti2.notification('提示', err.toString());
} else {
_notification.error({
message: '提示',
description: err.toString()
});
}
return Promise.reject(_objectSpread(_objectSpread({}, err), {}, {
data: null
}));
});
service.interceptors.response.use(function (res) {
var status = res.status,
data = res.data,
statusText = res.statusText,
config = res.config;
removePending(config);
return new Promise(function (resolve, reject) {
if (status < 200 || status > 300) {
var _configParams$excepti3;
if ((_configParams$excepti3 = configParams.exceptionErrFn) === null || _configParams$excepti3 === void 0 ? void 0 : _configParams$excepti3.notification) {
var _configParams$excepti4;
(_configParams$excepti4 = configParams.exceptionErrFn) === null || _configParams$excepti4 === void 0 ? void 0 : _configParams$excepti4.notification("\u8BF7\u6C42\u9519\u8BEF: ".concat(status), statusText);
} else {
_notification.error({
message: "\u8BF7\u6C42\u9519\u8BEF: ".concat(status),
description: statusText
});
}
reject(data);
} else if (!data.success) {
if (data.code === DATA_CODE.get('LOGIN_ERROR')) {
// 登录失效后,取消后续的http请求
source.cancel('登录失效');
if (configParams.loginErrFn) {
configParams.loginErrFn();
} else if (configParams.loginErrUrl) {
_Modal.warning({
title: '登录失效',
content: '点击确定重新登录',
okText: '确定',
onOk: function onOk() {
var _getAppInfo;
window.location.href = configParams.loginErrUrl || ((_getAppInfo = getAppInfo()) === null || _getAppInfo === void 0 ? void 0 : _getAppInfo.__fle_loginErrUrl__);
}
});
}
} else {
var _configParams$noNotif;
if (!((_configParams$noNotif = configParams.noNotificationCodes) === null || _configParams$noNotif === void 0 ? void 0 : _configParams$noNotif.includes(data.code))) {
var _configParams$excepti5;
if ((_configParams$excepti5 = configParams.exceptionErrFn) === null || _configParams$excepti5 === void 0 ? void 0 : _configParams$excepti5.notification) {
var _configParams$excepti6;
(_configParams$excepti6 = configParams.exceptionErrFn) === null || _configParams$excepti6 === void 0 ? void 0 : _configParams$excepti6.notification('提示', data.message);
} else {
_notification.error({
message: '提示',
description: data.message
});
}
}
}
reject(data);
} else {
resolve(data);
}
});
}, function (err) {
var _err$message;
if (err.message === 'Request failed with status code 404') {
if (configParams.errUrlFn) {
configParams.errUrlFn();
return;
}
}
if (err.message === '登录失效') {
return;
}
if ((_err$message = err.message) === null || _err$message === void 0 ? void 0 : _err$message.includes('timeout')) {
var _configParams$excepti7;
if ((_configParams$excepti7 = configParams.exceptionErrFn) === null || _configParams$excepti7 === void 0 ? void 0 : _configParams$excepti7.notification) {
var _configParams$excepti8;
(_configParams$excepti8 = configParams.exceptionErrFn) === null || _configParams$excepti8 === void 0 ? void 0 : _configParams$excepti8.notification('提示', '请求超时~');
} else {
_notification.error({
message: '提示',
description: '请求超时~'
});
}
} else {
var _configParams$excepti9;
if ((_configParams$excepti9 = configParams.exceptionErrFn) === null || _configParams$excepti9 === void 0 ? void 0 : _configParams$excepti9.notification) {
var _configParams$excepti10;
(_configParams$excepti10 = configParams.exceptionErrFn) === null || _configParams$excepti10 === void 0 ? void 0 : _configParams$excepti10.notification('提示', err.toString());
} else {
_notification.error({
message: '提示',
description: err.toString()
});
}
}
return Promise.reject(err);
});
return service;
};
export var request = Request({
baseUrl: getAePrefix()
});
export default Request;