Newer
Older
dcms_front / src / permission.js
import router from './router'
import { createRouter } from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar 进度条
import 'nprogress/nprogress.css' // progress bar style 进度条样式
import { Message } from 'element-ui'
import { getToken } from '@/utils/auth' // getToken from cookie
import { getCurrentSys } from './utils/auth'

NProgress.configure({ showSpinner: false })// NProgress configuration

const whiteList = ['/login', 'config/baseconfig'] // 不重定向白名单
// 全局钩子
router.beforeEach((to, from, next) => {
  NProgress.start() // 加载进度条
  // 如果有token
  if (getToken()) {
    console.log('to Path:' + to.path)
    // 登录后进入登录页
    if (to.path === '/login') {
      next({ path: '/login' })
      NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
    } else if (to.path === '/dashboard') {
      next()
      NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
    } else {
      // 当进入非登陆页或非主页时
      if (store.getters.roleList.length === 0) { // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(res => { // 拉取用户信息
          // 如果已有当前可进入的子系统,刷新子系统菜单
          const currentSys = getCurrentSys()
          if (currentSys) {
            // 远程访问获取权限列表(菜单&按钮)
            store.dispatch('GetMenus', currentSys).then(() => {
              store.commit('SET_CHANGEFLAG', '0')
              router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
              next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
            })
          } else { // 没有当前子系统信息,跳转到选择子系统页面
            next({ path: '/dashboard' })
          }
        }).catch((err) => {
          store.dispatch('FedLogOut').then(() => {
            Message.error(err || '权限验证失败,请重新登录系统')
            next({ path: '/' })
          })
        })
      } else {
        // console.log('************************')
        // console.log(store.getters.changeFlag)
        // if (store.getters.changeFlag === '1') {
        //   console.log('监测到子系统切换:')
        //   const currentSys = getCurrentSys()
        //   if (currentSys) {
        //     // 远程访问获取权限列表(菜单&按钮)
        //     store.dispatch('GetMenus', currentSys).then(() => {
        //       debugger
        //       console.log('========获取菜单成功========')
        //       store.commit('SET_CHANGEFLAG', '0')
        //       router.matcher = createRouter().matcher
        //       router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
        //       console.log(router.routes)
        //       console.log(to)
        //       next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
        //       // next() // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
        //     })
        //   } else { // 没有当前子系统信息,跳转到选择子系统页面
        //     next({ path: '/dashboard' })
        //   }
        // }
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) { // 免登录白名单,直接进入
      next()
    } else {
      // next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
      next('/login') // 否则全部重定向到登录页
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})