fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * @Author: 张嘉彬
 * @Date: 2021-10-13 15:15:36
 * @Description:
 */
import Cookie from 'js-cookie'
import router from './index'
import store from '../store'
import config from '@/app.config.js'
import NProgress from 'nprogress' // 进度条
import 'nprogress/nprogress.css'
const xsrfHeaderName = 'Authorization'
// 简单配置
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })
router.beforeEach(async (to, from, next) => {
    NProgress.start()
    const token = Cookie.get(xsrfHeaderName);
    //系统title设置
    if (to.path === '/' || to.path === '/platform' || to.path === '/accountManage') {
        document.title = "五粮液新零售"
    } else {
        //进入首页更新未读消息数量
        if (to.path === '/dash/home' && Cookie.get(xsrfHeaderName)) {
            async function getNoReadMsgCount() {
                await store.dispatch('getNoReadMsgCount');
            }
            getNoReadMsgCount();
        }
        document.title = '垂直生态赋能平台'
    }
 
    childRouter(to)
    const btnStyleVal = localStorage.getItem('btnStyleVal') ? JSON.parse(localStorage.getItem('btnStyleVal')) : null
    store.commit('setBtnStyle', btnStyleVal)
 
    if (!store.state.token && token) {
        store.commit('setToken', { token })
    }
    const isChildApp = config.apps.filter((v) => {
        return to.path !== '/' && v.activeRule.indexOf(`/#${to.path}`)
    }).length
    const isAuth = (to.matched && to.matched.length && to.matched[0].meta.auth) || isChildApp
    if (isAuth) {
        if (!store.state.token) {
            next('/')
            return
        }
        // 路由权限校验,目前路由meta元信息带有history的执行校验
        if (store.state.token && !store.state.userTree.length) {
            await store.dispatch('getUserTree').catch(err => {
                console.log(err)
            })
        }
 
    }
    // 刷新的初始化已经记录的tab路由历史
    const routerHistory = window.sessionStorage.getItem('routerHistory')
    if (routerHistory && !store.state.routerHistory.length) {
        store.commit('setRouterHistory', JSON.parse(routerHistory))
    }
 
    // 增加tab路由历史
    if (store.state.token) {
        const menu = store.state.userTreeFlat.filter(v => {
            return v.url === to.path
        })
        if (menu.length) {
            if ((to.meta.history || config.childPush.indexOf(to.path) > -1)) {
                store.dispatch('pushRouterHistory', menu[0])
            }
        } else {
            if (to.path !== '/abnormalPermissions' && to.path !== '/404' && to.path !== '/' && to.path !== '/dash' && to.path !== '/platform' && to.path !== '/accountManage') {
                if (to.path === '/dash/home' && !store.state.userTree.length) {
                    next({ path: '/abnormalPermissions' })
                } else {
                    next({
                        path: '/404',
                        replace: true
                    })
                }
            }
        }
    }
    const userInfo = localStorage.getItem("userInfo");
    if (!store.state.userInfo && userInfo) {
        store.commit('setUserInfo', JSON.parse(userInfo))
    }
    next()
})
router.afterEach((to, from) => {
    // todo
    NProgress.done()
})
/**
 *设置子路由的menuPath,使侧边栏菜单被选中
 * @param {*} to
 */
function childRouter(to) {
    if (to.path === '/dash/diy/add' || to.path === '/dash/diy/update') {
        to.meta.menuPath = '/dash/diy/list'
    } else if (to.path === '/dash/activityCenter/add' || to.path === '/dash/activityCenter/edit') {
        to.meta.menuPath = '/dash/activityCenter/list'
    }
}