Blame view

src/permission.js 1.33 KB
任超 committed
1 2 3 4 5 6 7 8 9 10 11 12
import router from './router'
import store from './store'
import { getMenuInfo } from '@/api/user'
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
13
    let hasAddDict = store.state.dict.addDict
任超 committed
14
    let hasAddRoute = store.state.permission.addRoutes
任超 committed
15 16 17
    if (!hasAddDict) {
        store.dispatch('dict/generateDic')
    }
任超 committed
18 19
    if (hasAddRoute) {
        next()
任超 committed
20 21 22 23 24 25 26 27 28 29
    } else {
        const { result: getMenuData } = await getMenuInfo()
        const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData)
        // 获取用户信息
        await store.dispatch('user/getUserInfo')
        router.addRoutes([...accessRoutes, { path: '*', redirect: '/404', hidden: true }])
        const routeTo = Cookies.get('routerTo')
        if (routeTo && routeTo !== '/') {
            next({ ...to, replace: true })
        } else {
任超 committed
30
            next('/home')
任超 committed
31
        }
任超 committed
32 33 34 35 36 37 38 39
    }
    NProgress.done()
})
router.afterEach(to => {
    // 解决刷新页面报404问题
    Cookies.set("routerTo", to.fullPath)
    NProgress.done()
})