Blame view

src/permission.js 1.06 KB
赵千 committed
1 2
import router from './router'
import store from './store'
任超 committed
3
import { getMenuInfo } from '@/api/user'
赵千 committed
4 5 6 7 8 9 10 11 12 13
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
import Cookies from 'js-cookie'

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

router.beforeEach(async (to, from, next) => {
    NProgress.start()
    document.title = getPageTitle(to.meta.title)
任超 committed
14
    let hasAddDict = store.state.dict.addDict
任超 committed
15
    let hasAddRoute = store.state.permission.addRoutes
任超 committed
16 17
    if (!hasAddDict) {
        store.dispatch('dict/generateDic')
赵千 committed
18
    }
任超 committed
19 20 21 22
    if (hasAddRoute) {
        next()
    } else {
        const { result: getMenuData } = await getMenuInfo()
任超 committed
23 24 25
        const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData)
        router.addRoutes(accessRoutes)
        next({ ...to, replace: true })
任超 committed
26
    }
任超 committed
27
    NProgress.done()
任超 committed
28

赵千 committed
29 30 31 32 33 34
})
router.afterEach(to => {
    // 解决刷新页面报404问题
    Cookies.set("routerTo", to.fullPath)
    NProgress.done()
})