配置环境变量和axios,完善首页轮播效果和全局查询
parent
655ecc7a9e
commit
d3bfe7a66e
@ -0,0 +1,41 @@
|
||||
import axios from 'axios'
|
||||
import * as logger from "echarts/types/src/util/log.js";
|
||||
|
||||
const request =axios.create({
|
||||
baseURL: import.meta.env.BASE_URL,
|
||||
timeout: import.meta.env.VITE_API_TIMEOUT || 50000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
request.interceptors.request.use(function (config) {
|
||||
const token = localStorage.getItem('token')
|
||||
if(token){
|
||||
config.headers['Authorization'] = token
|
||||
}
|
||||
// 在发送请求之前做些什么
|
||||
return config;
|
||||
}, function (error) {
|
||||
logger.error('请求错误:', error);
|
||||
// 对请求错误做些什么
|
||||
return Promise.reject(error);
|
||||
})
|
||||
|
||||
// 响应拦截器
|
||||
request.interceptors.response.use(function (res) {
|
||||
// 对响应数据做点什么
|
||||
if(res.data.code === 401){
|
||||
logger.error('未授权,请重新登录');
|
||||
// 清除token
|
||||
localStorage.removeItem('token');
|
||||
// 跳转到登录页
|
||||
window.location.href = '/login';
|
||||
//刷新当前页面
|
||||
window.location.reload();
|
||||
}
|
||||
return res;
|
||||
})
|
||||
|
||||
export default request
|
||||
Loading…
Reference in New Issue