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> <title><%= htmlWebpackPlugin.options.title %></title>
<script> <script>
var authConfig = { var authConfig = {
version: '1.1.0', version: '1.1.1',
roots: [ roots: [
{ {
id: "", id: "",

View File

@ -1,26 +1,48 @@
import axios from "axios"; import axios from "axios";
import store from '@/store' import store from "@/store";
import router from "@/router";
// 创建一个 axios 实例 // 创建一个 axios 实例
const service = axios.create({ const service = axios.create({
baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "", baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "",
// timeout: 30000, // timeout: 30000,
}); });
// 请求拦截器 // 请求拦截器
service.interceptors.request.use( service.interceptors.request.use(
config => { (config) => {
// 在发送请求设置cancel token // 在发送请求设置cancel token
config.cancelToken = new axios.CancelToken(cancel => { config.cancelToken = new axios.CancelToken((cancel) => {
store.dispatch("acrou/cancelToken/push", cancel) store.dispatch("acrou/cancelToken/push", cancel);
}) });
return config return config;
}, },
error => { (error) => {
// 发送失败 // 发送失败
console.log(error) console.log(error);
return Promise.reject(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; export default service;