添加路由权限
This commit is contained in:
+26
-16
@@ -7,22 +7,32 @@ NProgress.configure({
|
||||
speed: 500,
|
||||
});
|
||||
|
||||
const writeNames = ["/login"];
|
||||
// router.beforeEach((to, from, next) => {
|
||||
// NProgress.start();
|
||||
// if (sessionStorage.getItem("token")) {
|
||||
// if (to.path === "/login") {
|
||||
// next("/");
|
||||
// }
|
||||
// return next();
|
||||
// } else {
|
||||
// if (writeNames.includes(to.path)) {
|
||||
// next();
|
||||
// } else {
|
||||
// next("/login");
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// 路由守卫
|
||||
router.beforeEach((to, from, next) => {
|
||||
// 获取token和用户信息
|
||||
const token = sessionStorage.getItem('token')
|
||||
const userInfo = JSON.parse(sessionStorage.getItem('userInfo') || '{}')
|
||||
|
||||
// 白名单路由(不需要登录就可以访问的路由)
|
||||
const whiteList = ['/login']
|
||||
|
||||
// 如果在白名单中,直接放行
|
||||
if (whiteList.includes(to.path)) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是否登录
|
||||
if (!token || !userInfo.userid) {
|
||||
// 未登录,跳转到登录页
|
||||
next('/login')
|
||||
return
|
||||
}
|
||||
|
||||
// 已登录,放行
|
||||
next()
|
||||
})
|
||||
|
||||
router.afterEach((transition) => {
|
||||
NProgress.done();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user