401错误返回上一页

This commit is contained in:
Aicirou 2020-06-02 23:16:53 +08:00
parent bcce066a72
commit 3b0cd0ada9
2 changed files with 40 additions and 18 deletions

View File

@ -14,7 +14,7 @@
<title><%= htmlWebpackPlugin.options.title %></title>
<script>
var authConfig = {
version: '1.1.0',
version: '1.1.1',
roots: [
{
id: "",

View File

@ -1,26 +1,48 @@
import axios from "axios";
import store from '@/store'
import store from "@/store";
import router from "@/router";
// 创建一个 axios 实例
const service = axios.create({
baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "",
// timeout: 30000,
baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "",
// timeout: 30000,
});
// 请求拦截器
service.interceptors.request.use(
config => {
// 在发送请求设置cancel token
config.cancelToken = new axios.CancelToken(cancel => {
store.dispatch("acrou/cancelToken/push", cancel)
})
return config
},
error => {
// 发送失败
console.log(error)
return Promise.reject(error)
(config) => {
// 在发送请求设置cancel token
config.cancelToken = new axios.CancelToken((cancel) => {
store.dispatch("acrou/cancelToken/push", cancel);
});
return config;
},
(error) => {
// 发送失败
console.log(error);
return Promise.reject(error);
}
);
// 响应拦截器
service.interceptors.response.use(
(response) => {
return response;
},
(error) => {
if (error && error.response) {
switch (error.response.status) {
case 401:
error.message = "未授权,请登录";
break;
default:
break;
}
}
)
console.log(error);
router.app.$router.go(-1);
return Promise.reject(error);
}
);
export default service;