123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import router from './router'
- import store from './store'
- import { Message } from 'element-ui'
- import NProgress from 'nprogress'
- import 'nprogress/nprogress.css'
- import { getToken } from '@/utils/auth'
- import getPageTitle from '@/utils/get-page-title'
- NProgress.configure({ showSpinner: false })
- const whiteList = ['/login', '/auth-redirect']
- router.beforeEach(async(to, from, next) => {
- NProgress.start()
- document.title = getPageTitle(to.meta.title)
- const hasToken = getToken()
- if (hasToken) {
- if (to.path === '/login') {
- next({ path: '/power-change-monitoring' })
- NProgress.done()
- }
- else {
- const hasRoles = store.getters.roles && store.getters.roles.length > 0
- if (hasRoles) {
- next()
- } else {
- try {
- const rs = await store.dispatch('user/getInfo')
- let roles=[]
- if(rs.level === 1){
- roles=['admin']
- }else{
- roles=['manage']
- }
- const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
- router.addRoutes(accessRoutes)
- next({ ...to, replace: true })
- } catch (error) {
- await store.dispatch('user/resetToken')
- Message.error(error || 'Has Error')
- next(`/login?redirect=${to.path}`)
- NProgress.done()
- }
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- next()
- } else {
- next(`/login`)
- NProgress.done()
- }
- }
- })
- router.afterEach(() => {
- NProgress.done()
- })
|