forked from drone/command-center-dashboard

罗广辉
2025-04-21 2800fa4f32f3900509cb4d6eefaf2bfaf54efdd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import router from './router/'
import store from './store'
import { getToken } from '@/utils/auth'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
NProgress.configure({ showSpinner: false })
const lockPage = '/lock' //锁屏页
router.beforeEach((to, from, next) => {
    const meta = to.meta || {}
    const isMenu = meta.menu === undefined ? to.query.menu : meta.menu
    store.commit('SET_IS_MENU', isMenu === undefined)
    if (getToken()) {
        if (store.getters.isLock && to.path !== lockPage) {
            //如果系统激活锁屏,全部跳转到锁屏页
            next({ path: lockPage })
        } else if (to.path === '/login') {
            //如果登录成功访问登录页跳转到主页
            next({ path: '/' })
        } else {
            if (store.getters.token.length === 0) {
                store.dispatch('FedLogOut').then(() => {
                    next({ path: '/login' })
                })
            } else {
                const meta = to.meta || {}
                const query = to.query || {}
                if (meta.target) {
                    window.open(query.url.replace(/#/g, '&'))
                    return
                } else if (meta.isTab !== false) {
                    store.commit('ADD_TAG', {
                        name: query.name || to.name,
                        path: to.path,
                        fullPath: to.path,
                        params: to.params,
                        query: to.query,
                        meta: meta,
                    })
                }
                next()
            }
        }
    } else {
        //判断是否需要认证,没有登录访问去登录页
        if (meta.isAuth === false) {
            next()
        } else {
            next('/login')
        }
    }
})
 
router.afterEach(to => {
    NProgress.done()
    let title = router.$avueRouter.generateTitle(to, { label: 'name' })
    router.$avueRouter.setTitle(title)
    store.commit('SET_IS_SEARCH', false)
})