Newer
Older
BigScreenDatav / src / permission.js
import router from './router'
import store from './store'
import { Message } from 'ant-design-vue'
import { getToken } from '@/utils/auth' // getToken from cookie


const whiteList = ['/login', 'config/baseconfig','/map','/main'] // 不重定向白名单
// 全局钩子
router.beforeEach((to, from, next) => {
  // 如果有token
  if (getToken()) {
    // 登录后进入登录页
    if (to.path === '/login') {
      next()
    } else {
      // 当进入非登陆页时
      if (store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(res => { // 拉取用户信息
          console.log(res.data)
          next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
        }).catch((err) => {
          store.dispatch('FedLogOut').then(() => {
            Message.error(err || '权限验证失败,请重新登录系统')
            next({ path: '/' })
          })
        })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) { // 免登录白名单,直接进入
      next()
    } else {
      next(`/login`) // 否则全部重定向到登录页
    }
  }
})

router.afterEach(() => {
})