You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.6 KiB

import axios from 'axios';
6 months ago
import { baseUrl } from "../config/env";
import { $globalStore } from "../common/globalStore"
import { $globalRouter } from "../common/globalRouter"
import { showToast } from 'vant';
import 'vant/es/toast/style';
// import {cache} from '@/store/store';
// axios.defaults.withCredentials = true
//const source = axios.CancelToken.source()
const http = axios.create({
6 months ago
baseURL: baseUrl,
traditional: true,
contentType : "application/json",
timeout: 30000, // 30 秒
});
const router = $globalRouter;
//请求拦截器添加head
http.interceptors.request.use(
config => {
4 months ago
// FormData 必须由浏览器/axios 自动设置 multipart boundary
if (config.data instanceof FormData) {
delete config.headers['Content-Type'];
} else if(!config.headers['Content-Type']){
config.headers['Content-Type'] = 'application/json'
}
const token = $globalStore.useCommon.token;
if (token) {
config.headers['Authorization'] = 'Bearer ' + token;
}
config.headers['clientid'] = '9e6eb24cd4e169a98b0d2cd8034d877b';
return config
},
err => {
return Promise.reject(err)
}
)
//返回拦截器
http.interceptors.response.use(
response => {
if (response.data.code === 500) {
if (response.data.message) {
showToast(response.data.message);
}
if (response.data.msg) {
showToast(response.data.msg);
}
}
if (response.data.code === 401) {
showToast('没有操作该功能权限,请联系管理员添加权限');
}
if (response.data.msg === '未登录或登录超时。请重新登录') {
showToast('未登录或登录超时,请重新登录');
router.push({ path: "/login" });
}
return response;
},
error => {
if (!error.response) {
showToast('网络异常,请检查网络连接');
return Promise.reject(error);
}
if (error.response.status === 401) {
showToast('没有操作该功能权限,请联系管理员添加权限');
}
if (error.response.status === 403) {
if ('#/login' !== window.location.hash) {
showToast('登录过期,请先登录');
let target = window.location.hash;
router.push({ path: "/login", params: { target: encodeURIComponent(target) } });
}
}
return Promise.reject(error);
}
)
export default http