|
|
|
import Vue from "vue";
|
|
|
|
import axios from "axios";
|
|
|
|
import utils from "@/utils/encode";
|
|
|
|
//加密白名单
|
|
|
|
const encryptWhite = [];
|
|
|
|
const env = process.env.VUE_APP_ENV;
|
|
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
baseURL: process.env.VUE_APP_BASE_API,
|
|
|
|
timeout: 20000,
|
|
|
|
});
|
|
|
|
// 请求拦截
|
|
|
|
service.interceptors.request.use(
|
|
|
|
(config) => {
|
|
|
|
let token = localStorage.getItem("businessToken");
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
config.headers["Authorization"] = token;
|
|
|
|
} else delete config.headers["Authorization"];
|
|
|
|
|
|
|
|
const JSESSIONID = utils.uuid();
|
|
|
|
config.headers["JSESSIONID"] = JSESSIONID;
|
|
|
|
config.headers["token"] = utils.bcrypt(JSESSIONID);
|
|
|
|
config.headers['dataSources'] = 'WEB'
|
|
|
|
if (env === "development") {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
if (env === "production" || ebv === "test") {
|
|
|
|
//加密
|
|
|
|
config.data = {
|
|
|
|
params: utils.encrypt(JSON.stringify(config.data)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.responseType === "blob" && !encryptWhite.includes(config.url)) {
|
|
|
|
const data = {
|
|
|
|
params: utils.encrypt(JSON.stringify(config.data)),
|
|
|
|
};
|
|
|
|
config.data = data;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
console.log("axios request error:", error);
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// 响应拦截
|
|
|
|
service.interceptors.response.use(
|
|
|
|
(response) => {
|
|
|
|
const res = response.data;
|
|
|
|
const contentType = response.headers["content-type"];
|
|
|
|
//流文件
|
|
|
|
if (!contentType.includes("application/json")) return res;
|
|
|
|
|
|
|
|
if (env === "production" || env === "test") {
|
|
|
|
if (res.encrypt === 1) {
|
|
|
|
const dataParam = JSON.parse(utils.decrypt(res.data));
|
|
|
|
res.data = JSON.stringify(dataParam) === "{}" ? null : dataParam;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res && res.code) {
|
|
|
|
if (res.code === 42011) {
|
|
|
|
Vue.prototype.$message.error(res.msg || "您的登录已失效,请重新登录");
|
|
|
|
localStorage.removeItem("businessToken");
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location.reload();
|
|
|
|
}, 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 白名单
|
|
|
|
if (![20000, 42014, 46001].includes(res.code)) {
|
|
|
|
console.log("code码:" + res.code);
|
|
|
|
Vue.prototype.$message.error(res.msg);
|
|
|
|
return Promise.reject();
|
|
|
|
} else {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
Vue.prototype.$message.error("请求失败");
|
|
|
|
console.log("axios response error:", error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export default service;
|