refactor(src/utils/request.js): 优化401弹框

duizhaopin_ui
duan 2 years ago
parent ca9f9c1e70
commit 115f71d306

@ -2,6 +2,7 @@ import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import router from '@/router'
import { throttle } from '@/utils/index'
// create an axios instance
const service = axios.create({
@ -10,11 +11,22 @@ const service = axios.create({
timeout: 5000 // request timeout
})
const reLogin = throttle(function() {
MessageBox.confirm('登录超时请重新登录', '返回登录', {
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.commit('user/RESET_STATE')
store.commit('permission/RESET_STATE', [])
router.push('/login')
})
}, 1000)
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
@ -56,24 +68,7 @@ service.interceptors.response.use(
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.status === 401) {
// to re-login
// MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
// confirmButtonText: 'Re-Login',
// cancelButtonText: 'Cancel',
// type: 'warning'
// }).then(() => {
// store.dispatch('userinfo/resetToken').then(() => {
// location.reload()
// })
// })
MessageBox.confirm('登录超时请重新登录', '返回登录', {
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.commit('user/RESET_STATE')
store.commit('permission/RESET_STATE', [])
router.push('/login')
})
reLogin()
}
return Promise.reject(new Error(res.msg || 'Error'))
} else {

Loading…
Cancel
Save