66 lines
1.9 KiB
JavaScript
66 lines
1.9 KiB
JavaScript
/* eslint-disable no-param-reassign */
|
|
import axios from "axios";
|
|
// import { Notification, MessageBox, Message } from "element-ui";
|
|
// import router from "@/router/index";
|
|
import store from "../store";
|
|
import { showToast, showDialog } from "vant";
|
|
import { userLogin } from "@/utils/login";
|
|
import router from "@/router/index";
|
|
import { terminalType } from "@/utils/index";
|
|
|
|
// 创建axios实例
|
|
const service = axios.create({
|
|
baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
|
|
timeout: 5000, // 请求超时时间
|
|
});
|
|
|
|
// request拦截器
|
|
service.interceptors.request.use(
|
|
(config) => {
|
|
// 问卷页面不传token
|
|
if (
|
|
store.state.token &&
|
|
!["/previewQuestion"].includes(router.currentRoute.value.path)
|
|
) {
|
|
config.headers.token = `${store.state.token}`; // 让每个请求携带自定义token 请根据实际情况自行修改
|
|
}
|
|
return Promise.resolve(config);
|
|
},
|
|
(error) => {
|
|
// Do something with request error
|
|
console.log(error); // for debug
|
|
// Promise.reject(error)
|
|
Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
// response 拦截器
|
|
service.interceptors.response.use(
|
|
(response) => {
|
|
if ([2000, 2007].includes(response.data.code)) {
|
|
// 需要重新登录的code
|
|
// router.push(`/login?redirect=${router.currentRoute.value.fullPath}`);
|
|
if (["App", "PcClient"].includes(terminalType)) {
|
|
showDialog({
|
|
message: response.data.message,
|
|
showConfirmButton: "重新登陆",
|
|
}).then(() => {
|
|
userLogin();
|
|
});
|
|
return response.data;
|
|
} else {
|
|
userLogin();
|
|
return response.data;
|
|
}
|
|
} else if (response.data.code !== 0) {
|
|
showToast(response.data.message);
|
|
}
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
error && error.message && showToast(`${error.config.url}-${error.message}`);
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
export default service;
|