@ecoding/helper.request
v0.2.2
Published
dev request
Downloads
36
Readme
import axios from 'axios'; import { getCookie } from '@ecoding/helper.cookie'; import { Loading, Toast, http } from '@ecoding/components.antd'; import R from '@ecoding/helper.request'; import { getAppCode } from "@sciecomm/portal.global"; import { localLocale } from '@/locales'; export const ignoreTokenInterFace: string[] = [];
const originPrefix = { "localhost:8081": "http://192.168.2.29:8302", // 本地纯前端环境 'local': 'http://api.sciecomm.cn', //本地环境bff 'test': 'http://192.168.2.139:8302', //测试环境 'prod': 'http://api.sciecomm.cn', //线上环境 };
const request = new R(axios);
request.setFinallyHook(() => {
Loading.hide();
});
request.setResTranslateHook((out, res) => {
if (res.code == -10001) { // 登录失效
setTimeout(() => {
// window.location.href = /user/login?redirect=${encodeURIComponent(window.location.href)}
;
}, 1500);
}
return {
success: res.successed,
code: res.code,
msg: res.message,
message: res.message,
data: res.model,
};
});
request.setReqHook(async (options) => {
const url = options.url;
const host = location.host;
let domain;
if (/^/api?//.test(url)) {
domain = originPrefix[host] || originPrefix[window.PRELOAD_STATE?.ctxEnv || process.env.NODE_ENV];
} else {
domain = "";
}
if (!ignoreTokenInterFace.includes(url)) {
const token = getCookie('accessToken');
if (token) {
options.headers = Object.assign(options.headers || {}, {
accessToken: token,
appCode: getAppCode(url)
});
} else {
// Toast.error({
// title: '登录信息失效,请重新登录'
// });
// setTimeout(() => {
// // location.href = "/user/login";
// }, 1500);
}
}
options.headers = Object.assign(options.headers || {}, {
language: localLocale
});
options.url = ${domain}${url}
;
// options.params = Object.assign(options.params || {}, { locale: localLocale });
return Promise.resolve(options);
});
request.setErrorUiHook((err) => {
Toast.error({
mask: true,
duration: 20000,
title: String(err.message || err.msg)
});
});
http.setOutRequest(request);
export const r = request;