/**
|
* 全站权限配置
|
*
|
*/
|
import router from './router/router'
|
import store from '@/store'
|
import { validatenull } from '@/util/validate'
|
import NProgress from 'nprogress' // progress bar
|
import 'nprogress/nprogress.css' // progress bar style
|
NProgress.configure({ showSpinner: false })
|
|
router.beforeEach((to, from, next) => {
|
NProgress.start()
|
const meta = to.meta || {}
|
const whiteList = ['/login', '/404', '/401', '/lock', '/pack']
|
if (store.getters.access_token) {
|
if (store.getters.isLock && to.path !== '/lock') {
|
next({ path: '/lock' })
|
} else if (to.path === '/login') {
|
next({ path: '/' })
|
} else {
|
const value = to.query.src || to.fullPath
|
const label = to.query.name || to.name
|
if (
|
meta.isTab !== false &&
|
!validatenull(value) &&
|
!validatenull(label)
|
) {
|
store.commit('ADD_TAG', {
|
label: label,
|
value: value,
|
params: to.params,
|
query: to.query,
|
group: router.$avueRouter.group || []
|
})
|
}
|
next()
|
}
|
} else {
|
// 页面多开:载入localStorage的auth参数 By Luxn
|
let auth = JSON.parse(localStorage.getItem('authorization') || '{}')
|
if (meta.isAuth === false) {
|
//没有token的情况
|
if (whiteList.indexOf(to.path) !== -1) {
|
// 如果在白名单内则直接跳转
|
next()
|
} else {
|
// 其余页面重定向到登录页
|
next('/login')
|
}
|
// next();
|
} else {
|
if (auth.date_time) {
|
let key = [
|
'access_token',
|
'refresh_token',
|
'expires_in',
|
'user_info',
|
'language'
|
]
|
key.forEach((item, i) => {
|
store.commit('SET_' + item.toUpperCase(), auth[item])
|
store.commit('SET_PERMISSIONS', auth.user_info.authorities || [])
|
})
|
// 获取左侧导航,打开Path页面 By Luxn
|
store
|
.dispatch('GetMenu', { language: auth.language, type: true })
|
.then((data) => {
|
let path = window.location.hash.substr(1)
|
if (data.length > 0 && path.length > 1) {
|
router.$avueRouter.formatRoutes(data, true)
|
next({ path: path })
|
} else {
|
next()
|
}
|
})
|
} else {
|
next('/login')
|
}
|
}
|
}
|
})
|
|
router.afterEach((to, from) => {
|
NProgress.done()
|
const title = store.getters.tag.label
|
router.$avueRouter.setTitle(title)
|
// 打开新页面标题不对,根据router配置的title进行替换
|
if (to.meta.title) {
|
router.$avueRouter.setTitle(to.meta.title)
|
}
|
})
|