/*
|
* @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'
|
}
|
}
|