|
|
@ -156,12 +156,13 @@ const mainRoutes = { |
|
|
{ path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } }, |
|
|
{ path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } }, |
|
|
], |
|
|
], |
|
|
beforeEnter (to, from, next) { |
|
|
beforeEnter (to, from, next) { |
|
|
let token =Vue.cookie.get('token') |
|
|
|
|
|
|
|
|
let token = Vue.cookie.get('token') |
|
|
if (!token || !/\S/.test(token)) { |
|
|
if (!token || !/\S/.test(token)) { |
|
|
clearLoginInfo() |
|
|
clearLoginInfo() |
|
|
next({ name: 'login' }) |
|
|
next({ name: 'login' }) |
|
|
|
|
|
} else { |
|
|
|
|
|
next() |
|
|
} |
|
|
} |
|
|
next() |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -175,6 +176,16 @@ const router = new Router({ |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
router.beforeEach((to, from, next) => { |
|
|
router.beforeEach((to, from, next) => { |
|
|
|
|
|
// 检查登录状态(除了登录页和404页)
|
|
|
|
|
|
if (to.name !== 'login' && to.name !== '404') { |
|
|
|
|
|
let token = Vue.cookie.get('token') |
|
|
|
|
|
if (!token || !/\S/.test(token)) { |
|
|
|
|
|
clearLoginInfo() |
|
|
|
|
|
next({ name: 'login' }) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 添加动态(菜单)路由
|
|
|
// 添加动态(菜单)路由
|
|
|
// 1. 已经添加 or 全局路由, 直接访问
|
|
|
// 1. 已经添加 or 全局路由, 直接访问
|
|
|
// 2. 获取菜单列表, 添加并保存本地存储
|
|
|
// 2. 获取菜单列表, 添加并保存本地存储
|
|
|
@ -192,10 +203,18 @@ router.beforeEach((to, from, next) => { |
|
|
sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]')) |
|
|
sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]')) |
|
|
sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]')) |
|
|
sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]')) |
|
|
//next({ ...to, replace: true })
|
|
|
//next({ ...to, replace: true })
|
|
|
|
|
|
} else if (data && data.code === 401) { |
|
|
|
|
|
// session过期
|
|
|
|
|
|
console.log('会话已过期,跳转至登录页') |
|
|
|
|
|
clearLoginInfo() |
|
|
|
|
|
next({ name: 'login' }) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
}).catch((e) => { |
|
|
}).catch((e) => { |
|
|
console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue') |
|
|
console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue') |
|
|
router.push({ name: 'login' }) |
|
|
|
|
|
|
|
|
clearLoginInfo() |
|
|
|
|
|
next({ name: 'login' }) |
|
|
|
|
|
return |
|
|
}) |
|
|
}) |
|
|
router.options.isAddDynamicMenuRoutes = true |
|
|
router.options.isAddDynamicMenuRoutes = true |
|
|
next({ ...to, replace: true }) |
|
|
next({ ...to, replace: true }) |
|
|
|