以下是untils中request里判断过期及全局请求处理
import axios from 'axios'
import { useRouter } from "vue-router";
// import store from '../store/index'
import Setting from './setting'
import { ElMessage } from "element-plus";
const router = useRouter()
let service = axios.create({
baseURL: Setting.baseURL,
timeout: 5000,
})
service.interceptors.request.use((config: any) => {
let token = localStorage.getItem('token')
if (token !== "") {
config.headers['Authorization'] = token
return config
} else {
router.push("/")
}
// console.log(token);
}, (error: any) => {
return Promise.reject(error)
})
// 添加响应拦截器
service.interceptors.response.use(
function (response) {
// 对响应数据进行操作
if (response.data.code == 401) {
ElMessage.error('登录已过期,请重新登陆~')
setTimeout(() => {
window.location.replace("/")
}, 500);
}
return response;
},
function (error) {
// 对响应错误进行操作
ElMessage.error(error.message)
// console.log(error);
return Promise.reject(error);
}
);