2024-02-02 15:04:47 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 全站http配置
|
|
|
|
|
|
*
|
|
|
|
|
|
* axios参数说明
|
|
|
|
|
|
* isSerialize是否开启form表单提交
|
|
|
|
|
|
* isToken是否需要token
|
|
|
|
|
|
*/
|
2025-11-11 21:03:35 +08:00
|
|
|
|
import "nprogress/nprogress.css";
|
2024-02-02 15:04:47 +08:00
|
|
|
|
|
2025-11-11 21:03:35 +08:00
|
|
|
|
import website from "@/config/website";
|
|
|
|
|
|
import router from "@/router/router";
|
|
|
|
|
|
import store from "@/store/";
|
|
|
|
|
|
import { getToken } from "@/util/auth";
|
|
|
|
|
|
import { serialize } from "@/util/util";
|
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
import { Message } from "element-ui";
|
|
|
|
|
|
import { Base64 } from "js-base64";
|
|
|
|
|
|
import NProgress from "nprogress";
|
2024-02-02 15:04:47 +08:00
|
|
|
|
|
2024-07-30 16:53:48 +08:00
|
|
|
|
const service = axios.create({
|
|
|
|
|
|
// baseURL: 'https://jibaoadmin.qemcap.com/api',
|
|
|
|
|
|
timeout: 60000
|
2025-11-11 21:03:35 +08:00
|
|
|
|
});
|
|
|
|
|
|
const replaceStr = JSON.parse(process.env.VUE_APP_REPLACE_STR);
|
2024-02-02 15:04:47 +08:00
|
|
|
|
//默认超时时间
|
2024-07-30 16:53:48 +08:00
|
|
|
|
service.defaults.timeout = 60000;
|
2024-02-02 15:04:47 +08:00
|
|
|
|
//返回其他状态码
|
2025-11-11 21:03:35 +08:00
|
|
|
|
service.defaults.validateStatus = function(status) {
|
2024-02-02 15:04:47 +08:00
|
|
|
|
return status >= 200 && status <= 500;
|
|
|
|
|
|
};
|
|
|
|
|
|
//跨域请求,允许保存cookie
|
2024-07-30 16:53:48 +08:00
|
|
|
|
service.defaults.withCredentials = true;
|
2024-02-02 15:04:47 +08:00
|
|
|
|
// NProgress 配置
|
2025-11-11 21:03:35 +08:00
|
|
|
|
NProgress.configure({ showSpinner: false });
|
2024-02-02 15:04:47 +08:00
|
|
|
|
// http request拦截
|
2025-11-11 21:03:35 +08:00
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
|
config => {
|
|
|
|
|
|
//开启 progress bar
|
|
|
|
|
|
NProgress.start();
|
|
|
|
|
|
const meta = config.meta || {};
|
|
|
|
|
|
const isToken = meta.isToken === false;
|
|
|
|
|
|
config.headers["Authorization"] = `Basic ${Base64.encode(
|
|
|
|
|
|
`${website.clientId}:${website.clientSecret}`
|
|
|
|
|
|
)}`;
|
|
|
|
|
|
if (getToken() && !isToken) {
|
|
|
|
|
|
//让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
|
|
|
|
|
|
config.headers["Jobslink-Auth"] = "bearer " + getToken();
|
|
|
|
|
|
}
|
|
|
|
|
|
// headers中配置serialize为true开启序列化
|
|
|
|
|
|
if (config.method === "post" && meta.isSerialize === true) {
|
|
|
|
|
|
config.data = serialize(config.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
return Promise.reject(error);
|
2024-02-02 15:04:47 +08:00
|
|
|
|
}
|
2025-11-11 21:03:35 +08:00
|
|
|
|
);
|
2024-08-07 09:34:41 +08:00
|
|
|
|
|
2025-11-11 21:03:35 +08:00
|
|
|
|
axios.interceptors.request.use(
|
|
|
|
|
|
config => {
|
|
|
|
|
|
//开启 progress bar
|
|
|
|
|
|
NProgress.start();
|
|
|
|
|
|
const meta = config.meta || {};
|
|
|
|
|
|
const isToken = meta.isToken === false;
|
|
|
|
|
|
config.headers["Authorization"] = `Basic ${Base64.encode(
|
|
|
|
|
|
`${website.clientId}:${website.clientSecret}`
|
|
|
|
|
|
)}`;
|
|
|
|
|
|
if (getToken() && !isToken) {
|
|
|
|
|
|
//让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
|
|
|
|
|
|
config.headers["Jobslink-Auth"] = "bearer " + getToken();
|
|
|
|
|
|
}
|
|
|
|
|
|
// headers中配置serialize为true开启序列化
|
|
|
|
|
|
if (config.method === "post" && meta.isSerialize === true) {
|
|
|
|
|
|
config.data = serialize(config.data);
|
|
|
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
return Promise.reject(error);
|
2024-08-07 09:34:41 +08:00
|
|
|
|
}
|
2025-11-11 21:03:35 +08:00
|
|
|
|
);
|
2024-02-02 15:04:47 +08:00
|
|
|
|
// http response 拦截
|
2024-07-30 16:53:48 +08:00
|
|
|
|
service.interceptors.response.use(
|
2024-04-18 16:58:58 +08:00
|
|
|
|
res => {
|
|
|
|
|
|
//关闭 progress bar
|
|
|
|
|
|
NProgress.done();
|
|
|
|
|
|
//获取状态码
|
|
|
|
|
|
const status = res.data.code || res.status;
|
|
|
|
|
|
const statusWhiteList = website.statusWhiteList || [];
|
2025-11-11 21:03:35 +08:00
|
|
|
|
const message = res.data.msg || res.data.error_description || "未知错误";
|
2024-04-18 16:58:58 +08:00
|
|
|
|
//如果在白名单里则自行catch逻辑处理
|
|
|
|
|
|
if (statusWhiteList.includes(status)) return Promise.reject(res);
|
|
|
|
|
|
//如果是401则跳转到登录页面
|
2024-05-10 16:45:20 +08:00
|
|
|
|
if (status === 401) {
|
2025-11-11 21:03:35 +08:00
|
|
|
|
store.dispatch("FedLogOut").then(() => {
|
2025-11-14 10:11:44 +08:00
|
|
|
|
if (process.env.NODE_ENV !== "development") {
|
|
|
|
|
|
window.location.href = process.env.VUE_APP_LOGIN_NEXT;
|
|
|
|
|
|
}
|
2024-05-10 16:45:20 +08:00
|
|
|
|
});
|
|
|
|
|
|
// store.dispatch('FedLogOut').then(() => router.push({path: '/login'}));
|
|
|
|
|
|
}
|
2024-04-18 16:58:58 +08:00
|
|
|
|
// 如果请求为500统一处理
|
2025-11-11 21:03:35 +08:00
|
|
|
|
const err = new Error(message);
|
|
|
|
|
|
err.response = res;
|
|
|
|
|
|
err.status = status;
|
2024-04-18 16:58:58 +08:00
|
|
|
|
if (message.length > 100) {
|
2025-11-11 21:03:35 +08:00
|
|
|
|
Message({ message: "系统繁忙", type: "error" });
|
|
|
|
|
|
return Promise.reject(err);
|
2024-04-18 16:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 如果请求为非200否者默认统一处理
|
|
|
|
|
|
if (status !== 200) {
|
2025-11-11 21:03:35 +08:00
|
|
|
|
Message({ message: message, type: "error" });
|
|
|
|
|
|
return Promise.reject(err, res);
|
2024-04-18 16:58:58 +08:00
|
|
|
|
}
|
2024-04-29 21:40:01 +08:00
|
|
|
|
if (res.data instanceof Blob) {
|
2025-11-11 21:03:35 +08:00
|
|
|
|
return res;
|
2024-04-29 21:40:01 +08:00
|
|
|
|
}
|
2024-04-18 16:58:58 +08:00
|
|
|
|
return transformReplaceStr(res);
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
NProgress.done();
|
2025-11-11 21:03:35 +08:00
|
|
|
|
if (error.message.includes("timeout")) {
|
|
|
|
|
|
Message({ message: "请求超时", type: "error" });
|
2024-04-18 16:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(new Error(error), error);
|
2025-11-11 21:03:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
);
|
2024-04-18 16:58:58 +08:00
|
|
|
|
|
|
|
|
|
|
// 递归替换 url
|
|
|
|
|
|
function transformReplaceStr(props) {
|
2025-11-11 21:03:35 +08:00
|
|
|
|
if (typeof props !== "object" || props === null) {
|
2024-04-18 16:58:58 +08:00
|
|
|
|
for (const key in replaceStr) {
|
2025-11-11 21:03:35 +08:00
|
|
|
|
if (typeof props === "string") {
|
2024-04-18 16:58:58 +08:00
|
|
|
|
props = props.replace(key, replaceStr[key]);
|
2024-02-02 15:04:47 +08:00
|
|
|
|
}
|
2024-04-18 16:58:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
return props;
|
|
|
|
|
|
}
|
|
|
|
|
|
let result;
|
|
|
|
|
|
if (props instanceof Array) {
|
|
|
|
|
|
result = [];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
result = {};
|
|
|
|
|
|
}
|
|
|
|
|
|
for (let key in props) {
|
|
|
|
|
|
if (props.hasOwnProperty(key)) {
|
|
|
|
|
|
result[key] = transformReplaceStr(props[key]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2024-02-02 15:04:47 +08:00
|
|
|
|
|
2024-07-30 16:53:48 +08:00
|
|
|
|
export default service;
|