diff --git a/src/utils/request.js b/src/utils/request.js index 96f020a..8b48e20 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -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 @@ -35,7 +47,7 @@ service.interceptors.response.use( /** * If you want to get http information such as headers or status * Please return response => response - */ + */ /** * Determine the request status by custom code @@ -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 {