vue路由守卫配合权限,白名单
2024-08-27 11:05:55
router.beforeEach(async(to, from, next) => {
// 进度条开始
NProgress.start() // 确认用户是否已登录(获取它的token值,这里的getToken()是封装好的一个方法)
const hasToken = getToken() if (hasToken) {//如果有token,说明是登录状态
if (to.path === '/login') {//路由是/login页,那么直接跳转到首页
next({ path: '/' })
NProgress.done() //进度条结束
} else {//如果不是登录页
// 确定用户是否已通过getInfo获得其权限角色
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {//如果通过角色权限,继续访问
next()
} else {//如果没有通过角色权限
// alert('没有role')
console.log('获取角色')
try {
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
const { roles } = await store.dispatch('user/getInfo') // generate accessible routes map based on roles
const accessRoutes = await store.dispatch('permission/generateRoutes', roles) // dynamically add accessible routes
router.addRoutes(accessRoutes) // hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
next({ ...to, replace: true })
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {//如果没有token
/* has no token*/
// alert('没有token to.path=' + to.path) if (whiteList.indexOf(to.path) !== -1) {//白名单中有的路由,可以继续访问
// in the free login whitelist, go directly
next()
} else {//否则,白名单中没有的路由,跳回首页
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
最新文章
- Android Log Tag含义
- JS:offsetWidth\offsetleft 等图文解释
- linux命令-dd {拷贝并替换}
- 源代码版本管理与项目管理软件的认识与github的注册
- Myeclipse 操作数据库
- Java经典实例:纪元秒和本地日期时间互换
- We7<;001>;--We7 CMS之报错: HTTP 错误 404.0 - Not Found 您要找的资源已被删除、已更名或暂时不可用。
- kvm 克隆虚拟机
- UIView总结---对UIView头文件中的大部分信息进行中文注释
- bzoj1406: [AHOI2007]密码箱
- s3c2440栈分配情况(fl2440裸机 stack)
- hdu_1506:Largest Rectangle in a Histogram 【单调栈】
- css实现左侧固定宽,右侧自适应的7中方法
- tf.py_func
- Android MVP 架构一 View与Presenter
- 译:6.RabbitMQ Java Client 之 Remote procedure call (RPC,远程过程调用)
- [日常] Go语言圣经-函数递归习题
- Halcon开发环境和数据结构介绍——第1讲
- gj4 深入类和对象
- Could not update Activiti database schema: unknown version from database: &#39;5.20.0.1&#39;
热门文章
- 6353. 【NOIP2019模拟】给(ca)
- 代理修饰词weak/assign/strong的区别
- Ubuntu 16.04下使用docker部署rabbitmq
- [BZOJ3199][SDOI2013]escape:半平面交
- 基于vue模块化开发后台系统——构建项目
- Linux shell】grep命令精确匹配字符串查找
- Codeforces Gym 100269 Dwarf Tower (最短路)
- qbzt day7上午
- z-index的各种坑
- ConcurrentLinkedQueue 源码分析