dounai1306 / vue

vue的一些相关记录
0 stars 0 forks source link

导航守卫 #17

Open dounai1306 opened 6 years ago

dounai1306 commented 6 years ago
// 通过 route meta 上的 requiresAuth 判断目标页面是否需要进行权限验证
// 判断 cookie是否存在,global.cookie 是多环境的指定cookie名,cookie不存在跳转到登录页面
// 校验本地权限
// 遍历每一层的权限,结合后端返回的权限判断是否放行,如果权限为flas跳转到无权限的页面
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    let cookie = Vue.cookie.get(global.cookie)
    if (cookie) {
      if (Vue.localStorage.get('Permission')) {
        let Permission = JSON.parse(Vue.localStorage.get('Permission'))
        for (let x in Permission) {
          let keyName = to.meta.auth[0]
          let keyPer = to.meta.auth[1]
          if (x === keyName) {
            let routerPer = Permission[x][keyPer]
            if (routerPer) {
              next()
            } else {
              next({path: '/permission'})
            }
          }
        }
      }
      next()
    } else {
      window.location.href = global.LoginUrl
    }
  } else {
    next()
  }
})