作者 积硅步,至千里

修改登录超时刷新页面跳转登录页面还提示重新登录问题

1 -import router from './router'  
2 -import store from './store'  
3 -import { Message } from 'element-ui'  
4 -import NProgress from 'nprogress'  
5 -import 'nprogress/nprogress.css'  
6 -import { getToken } from '@/utils/auth'  
7 -  
8 -NProgress.configure({ showSpinner: false })  
9 -  
10 -const whiteList = ['/login', '/auth-redirect', '/bind', '/register']  
11 -  
12 -router.beforeEach((to, from, next) => {  
13 - NProgress.start()  
14 - if (getToken()) {  
15 - to.meta.title && store.dispatch('settings/setTitle', to.meta.title)  
16 - /* has token*/  
17 - if (to.path === '/login') {  
18 - next({ path: '/' })  
19 - NProgress.done()  
20 - } else {  
21 - if (store.getters.roles.length === 0) {  
22 - // 判断当前用户是否已拉取完user_info信息  
23 - store.dispatch('GetInfo').then(() => {  
24 - store.dispatch('GenerateRoutes').then(accessRoutes => {  
25 - // 根据roles权限生成可访问的路由表  
26 - router.addRoutes(accessRoutes) // 动态添加可访问路由表  
27 - next({ ...to, replace: true }) // hack方法 确保addRoutes已完成  
28 - })  
29 - }).catch(err => {  
30 - store.dispatch('LogOut').then(() => {  
31 - Message.error(err)  
32 - next({ path: '/' })  
33 - })  
34 - })  
35 - } else {  
36 - next()  
37 - }  
38 - }  
39 - } else {  
40 - // 没有token  
41 - if (whiteList.indexOf(to.path) !== -1) {  
42 - // 在免登录白名单,直接进入  
43 - next()  
44 - } else {  
45 - next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页  
46 - NProgress.done()  
47 - }  
48 - }  
49 -})  
50 -  
51 -router.afterEach(() => {  
52 - NProgress.done()  
53 -}) 1 +import router from './router'
  2 +import store from './store'
  3 +import { Message } from 'element-ui'
  4 +import NProgress from 'nprogress'
  5 +import 'nprogress/nprogress.css'
  6 +import { getToken } from '@/utils/auth'
  7 +import { isRelogin } from '@/utils/request'
  8 +
  9 +NProgress.configure({ showSpinner: false })
  10 +
  11 +const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
  12 +
  13 +router.beforeEach((to, from, next) => {
  14 + NProgress.start()
  15 + if (getToken()) {
  16 + to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  17 + /* has token*/
  18 + if (to.path === '/login') {
  19 + next({ path: '/' })
  20 + NProgress.done()
  21 + } else {
  22 + if (store.getters.roles.length === 0) {
  23 + isRelogin.show = true
  24 + // 判断当前用户是否已拉取完user_info信息
  25 + store.dispatch('GetInfo').then(() => {
  26 + isRelogin.show = false
  27 + store.dispatch('GenerateRoutes').then(accessRoutes => {
  28 + // 根据roles权限生成可访问的路由表
  29 + router.addRoutes(accessRoutes) // 动态添加可访问路由表
  30 + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  31 + })
  32 + }).catch(err => {
  33 + store.dispatch('LogOut').then(() => {
  34 + Message.error(err)
  35 + next({ path: '/' })
  36 + })
  37 + })
  38 + } else {
  39 + next()
  40 + }
  41 + }
  42 + } else {
  43 + // 没有token
  44 + if (whiteList.indexOf(to.path) !== -1) {
  45 + // 在免登录白名单,直接进入
  46 + next()
  47 + } else {
  48 + next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  49 + NProgress.done()
  50 + }
  51 + }
  52 +})
  53 +
  54 +router.afterEach(() => {
  55 + NProgress.done()
  56 +})
@@ -9,7 +9,7 @@ import { saveAs } from 'file-saver' @@ -9,7 +9,7 @@ import { saveAs } from 'file-saver'
9 9
10 let downloadLoadingInstance; 10 let downloadLoadingInstance;
11 // 是否显示重新登录 11 // 是否显示重新登录
12 -let isReloginShow; 12 +export let isRelogin = { show: false };
13 13
14 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' 14 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
15 // 创建axios实例 15 // 创建axios实例
@@ -76,23 +76,20 @@ service.interceptors.response.use(res => { @@ -76,23 +76,20 @@ service.interceptors.response.use(res => {
76 return res.data 76 return res.data
77 } 77 }
78 if (code === 401) { 78 if (code === 401) {
79 - if (!isReloginShow) {  
80 - isReloginShow = true; 79 + if (!isRelogin.show) {
  80 + isRelogin.show = true;
81 MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { 81 MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
82 confirmButtonText: '重新登录', 82 confirmButtonText: '重新登录',
83 cancelButtonText: '取消', 83 cancelButtonText: '取消',
84 type: 'warning' 84 type: 'warning'
85 } 85 }
86 ).then(() => { 86 ).then(() => {
87 - isReloginShow = false; 87 + isRelogin.show = false;
88 store.dispatch('LogOut').then(() => { 88 store.dispatch('LogOut').then(() => {
89 - // 如果是登录页面不需要重新加载  
90 - if (window.location.hash.indexOf("#/login") != 0) {  
91 - location.href = '/index';  
92 - } 89 + location.href = '/index';
93 }) 90 })
94 }).catch(() => { 91 }).catch(() => {
95 - isReloginShow = false; 92 + isRelogin.show = false;
96 }); 93 });
97 } 94 }
98 return Promise.reject('无效的会话,或者会话已过期,请重新登录。') 95 return Promise.reject('无效的会话,或者会话已过期,请重新登录。')