97 lines
4.8 KiB
JavaScript
97 lines
4.8 KiB
JavaScript
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 : String(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 { message } from 'antd';
|
|
export var reqConfig = function reqConfig(config) {
|
|
var _ref = config || {},
|
|
authorization = _ref.authorization,
|
|
showMsg = _ref.showMsg,
|
|
onError = _ref.onError;
|
|
return _objectSpread({
|
|
timeout: 1000,
|
|
baseURL: location.origin,
|
|
errorConfig: {
|
|
// @ts-ignore
|
|
errorHandler: function errorHandler(error, opts) {
|
|
if ([403, 401].includes(error.request.status)) {
|
|
message.error('登录过期,请重新登录');
|
|
onError === null || onError === void 0 || onError(error);
|
|
return;
|
|
}
|
|
// if (opts?.skipErrorHandler) throw error
|
|
// try {
|
|
// const { res } = ctx;
|
|
// const d = await res.text();
|
|
|
|
// if (res.status === 401 && store.user.isLogin) {
|
|
// store.user.resetLoginState();
|
|
// message.warning('登录过期,请重新登录!');
|
|
// return;
|
|
// }
|
|
// const isEmptyRes = d === '' || d.replace(/\s/g,"")=== 'tokenisinvalid'; //有些后端接口成功会返回空 做下兼容
|
|
// const body = !isEmptyRes ? JSON.parse(d || '{}') : d;
|
|
// const sessionCode = sessionStorage.getItem('zhst_errcode') || '{}'
|
|
// const ERROR_CODE = JSON.parse(sessionCode);
|
|
|
|
// if (Number(res.status) === 200) {
|
|
// ctx.res = body;
|
|
// } else {
|
|
// // 先判断Grpc-Metadata-Errorx-Message
|
|
// let errMsg = ERROR_CODE[body.code]?.value || body.message || '您的网络发生异常,无法连接服务器'
|
|
// toast && message.error(errMsg || body);
|
|
// ctx.res = {
|
|
// code: body.code,
|
|
// message: errMsg
|
|
// };
|
|
// }
|
|
|
|
// } catch (error) {
|
|
// if (get(error, 'type') !== 'CustomError') {
|
|
// toast && message.error('您的网络发生异常,无法连接服务器');
|
|
// }
|
|
// throw error;
|
|
// }
|
|
},
|
|
// @ts-ignore
|
|
errorThrower: function errorThrower(res) {
|
|
console.log('报错啦:', res);
|
|
}
|
|
},
|
|
// 请求
|
|
requestInterceptors: [
|
|
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
|
[function (url, options) {
|
|
var Authorization = authorization || localStorage.getItem('ZHST_AUTH_TOKEN') || '';
|
|
return {
|
|
url: url,
|
|
options: _objectSpread(_objectSpread({}, options), {}, {
|
|
headers: {
|
|
Authorization: Authorization
|
|
}
|
|
})
|
|
};
|
|
}, function (error) {
|
|
return Promise.reject(error);
|
|
}]],
|
|
// 返回
|
|
responseInterceptors: [
|
|
// 一个二元组,第一个元素是 request 拦截器,第二个元素是错误处理
|
|
[function (response) {
|
|
if (response.status !== 200) {
|
|
throw Error(JSON.stringify(response.data));
|
|
}
|
|
var _ref2 = response.data || {},
|
|
code = _ref2.code;
|
|
if (showMsg && code !== 200) {
|
|
message.error(response.data.message || '请求失败!');
|
|
return null;
|
|
}
|
|
return response.data;
|
|
}, function (error) {
|
|
return Promise.reject(error);
|
|
}]]
|
|
}, config);
|
|
}; |