|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
import axios from 'axios'
|
|
|
|
|
import { Notification, MessageBox, Message, Loading } from 'element-ui'
|
|
|
|
|
import store from '@/store'
|
|
|
|
|
import { getToken } from '@/utils/auth'
|
|
|
|
|
import { getToken, setToken } from '@/utils/auth'
|
|
|
|
|
import errorCode from '@/utils/errorCode'
|
|
|
|
|
import { tansParams, blobValidate } from "@/utils/ruoyi";
|
|
|
|
|
import cache from '@/plugins/cache'
|
|
|
|
@ -11,6 +11,11 @@ let downloadLoadingInstance;
|
|
|
|
|
// 是否显示重新登录
|
|
|
|
|
export let isRelogin = { show: false };
|
|
|
|
|
|
|
|
|
|
// 是否正在刷新的标记
|
|
|
|
|
let isRefreshing = false
|
|
|
|
|
//重试队列
|
|
|
|
|
let requests = []
|
|
|
|
|
|
|
|
|
|
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
|
|
|
|
// 创建axios实例
|
|
|
|
|
const service = axios.create({
|
|
|
|
@ -67,6 +72,7 @@ service.interceptors.request.use(config => {
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
service.interceptors.response.use(res => {
|
|
|
|
|
console.log(res,'resres');
|
|
|
|
|
// 未设置状态码则默认成功状态
|
|
|
|
|
const code = res.data.code || 200;
|
|
|
|
|
// 获取错误信息
|
|
|
|
@ -78,16 +84,34 @@ service.interceptors.response.use(res => {
|
|
|
|
|
if (code === 401) {
|
|
|
|
|
if (!isRelogin.show) {
|
|
|
|
|
isRelogin.show = true;
|
|
|
|
|
MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
|
|
|
|
|
// MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
|
|
|
|
|
// isRelogin.show = false;
|
|
|
|
|
// store.dispatch('LogOut').then(() => {
|
|
|
|
|
// location.href = '/login';
|
|
|
|
|
// })
|
|
|
|
|
// }).catch(() => {
|
|
|
|
|
// isRelogin.show = false;
|
|
|
|
|
// });
|
|
|
|
|
// TODO:无感刷新token暂缺少不需验证码登录接口
|
|
|
|
|
store.dispatch("Login", this.loginForm).then(() => {
|
|
|
|
|
isRelogin.show = false;
|
|
|
|
|
res.headers.Authorization = getToken();
|
|
|
|
|
requests.forEach((cb) => cb(getToken()))
|
|
|
|
|
requests = [] // 重新请求完清空
|
|
|
|
|
return service(res.config)
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
isRelogin.show = false;
|
|
|
|
|
store.dispatch('LogOut').then(() => {
|
|
|
|
|
location.href = '/index';
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
// 用函数形式将 resolve 存入,等待刷新后再执行
|
|
|
|
|
requests.push(token => {
|
|
|
|
|
res.headers.Authorization = `${token}`
|
|
|
|
|
resolve(service(res.config))
|
|
|
|
|
})
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
isRelogin.show = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
|
|
|
|
|
} else if (code === 500) {
|
|
|
|
|
Message({ message: msg, type: 'error' })
|
|
|
|
|
return Promise.reject(new Error(msg))
|
|
|
|
@ -104,6 +128,9 @@ service.interceptors.response.use(res => {
|
|
|
|
|
error => {
|
|
|
|
|
console.log('err' + error)
|
|
|
|
|
let { message } = error;
|
|
|
|
|
const originalRequest = error.config;
|
|
|
|
|
console.log(originalRequest,'originalRequest');
|
|
|
|
|
console.log(error.response.status,'error.response.status');
|
|
|
|
|
if (message == "Network Error") {
|
|
|
|
|
message = "后端接口连接异常";
|
|
|
|
|
} else if (message.includes("timeout")) {
|
|
|
|
|