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()
}
}
})

最新文章

  1. Android Log Tag含义
  2. JS:offsetWidth\offsetleft 等图文解释
  3. linux命令-dd {拷贝并替换}
  4. 源代码版本管理与项目管理软件的认识与github的注册
  5. Myeclipse 操作数据库
  6. Java经典实例:纪元秒和本地日期时间互换
  7. We7<001>--We7 CMS之报错: HTTP 错误 404.0 - Not Found 您要找的资源已被删除、已更名或暂时不可用。
  8. kvm 克隆虚拟机
  9. UIView总结---对UIView头文件中的大部分信息进行中文注释
  10. bzoj1406: [AHOI2007]密码箱
  11. s3c2440栈分配情况(fl2440裸机 stack)
  12. hdu_1506:Largest Rectangle in a Histogram 【单调栈】
  13. css实现左侧固定宽,右侧自适应的7中方法
  14. tf.py_func
  15. Android MVP 架构一 View与Presenter
  16. 译:6.RabbitMQ Java Client 之 Remote procedure call (RPC,远程过程调用)
  17. [日常] Go语言圣经-函数递归习题
  18. Halcon开发环境和数据结构介绍——第1讲
  19. gj4 深入类和对象
  20. Could not update Activiti database schema: unknown version from database: '5.20.0.1'

热门文章

  1. 6353. 【NOIP2019模拟】给(ca)
  2. 代理修饰词weak/assign/strong的区别
  3. Ubuntu 16.04下使用docker部署rabbitmq
  4. [BZOJ3199][SDOI2013]escape:半平面交
  5. 基于vue模块化开发后台系统——构建项目
  6. Linux shell】grep命令精确匹配字符串查找
  7. Codeforces Gym 100269 Dwarf Tower (最短路)
  8. qbzt day7上午
  9. z-index的各种坑
  10. ConcurrentLinkedQueue 源码分析