2018-03-04 13:49:35 +08:00
|
|
|
import router from './router/router'
|
2017-11-02 14:41:37 +08:00
|
|
|
import store from './store'
|
2018-03-26 13:12:08 +08:00
|
|
|
import NProgress from 'nprogress' // progress bar
|
2018-05-12 09:20:42 +08:00
|
|
|
import 'nprogress/nprogress.css' // progress bar style
|
2018-05-14 22:29:39 +08:00
|
|
|
import {
|
|
|
|
getToken
|
|
|
|
} from '@/util/auth'
|
|
|
|
import {
|
|
|
|
setTitle
|
|
|
|
} from '@/util/util'
|
|
|
|
import {
|
|
|
|
validatenull
|
|
|
|
} from '@/util/validate'
|
2018-05-12 09:20:42 +08:00
|
|
|
|
|
|
|
// NProgress Configuration
|
|
|
|
NProgress.configure({
|
|
|
|
showSpinner: false
|
2018-05-14 22:29:39 +08:00
|
|
|
})
|
2018-05-12 09:20:42 +08:00
|
|
|
|
2017-11-02 14:41:37 +08:00
|
|
|
function hasPermission(roles, permissionRoles) {
|
2018-05-12 09:20:42 +08:00
|
|
|
if (!permissionRoles) return true
|
|
|
|
return roles.some(role => permissionRoles.indexOf(role) >= 0)
|
2017-11-02 14:41:37 +08:00
|
|
|
}
|
2018-03-26 13:12:08 +08:00
|
|
|
const whiteList = ['/login', '/404', '/401', '/lock']
|
2018-03-04 13:49:35 +08:00
|
|
|
const lockPage = '/lock'
|
2018-05-12 09:20:42 +08:00
|
|
|
|
2018-03-04 13:49:35 +08:00
|
|
|
router.beforeEach((to, from, next) => {
|
2018-05-12 09:20:42 +08:00
|
|
|
// start progress bar
|
|
|
|
NProgress.start()
|
|
|
|
const value = to.query.src ? to.query.src : to.path
|
|
|
|
const label = to.query.name ? to.query.name : to.name
|
|
|
|
if (whiteList.indexOf(value) === -1) {
|
|
|
|
store.commit('ADD_TAG', {
|
|
|
|
label: label,
|
|
|
|
value: value,
|
|
|
|
query: to.query
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (store.getters.access_token) { // determine if there has token
|
|
|
|
/* has token*/
|
|
|
|
if (store.getters.isLock && to.path !== lockPage) {
|
|
|
|
next({
|
|
|
|
path: lockPage
|
|
|
|
})
|
|
|
|
NProgress.done()
|
|
|
|
} else if (to.path === '/login') {
|
|
|
|
next({
|
|
|
|
path: '/'
|
|
|
|
})
|
|
|
|
NProgress.done()
|
|
|
|
} else {
|
|
|
|
if (store.getters.roles.length === 0) {
|
|
|
|
store.dispatch('GetUserInfo').then(res => {
|
|
|
|
const roles = res.roles
|
|
|
|
next({ ...to,
|
|
|
|
replace: true
|
|
|
|
})
|
|
|
|
}).catch(() => {
|
|
|
|
store.dispatch('FedLogOut').then(() => {
|
|
|
|
next({
|
|
|
|
path: '/login'
|
|
|
|
})
|
|
|
|
NProgress.done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
2018-03-26 13:12:08 +08:00
|
|
|
}
|
2018-05-12 09:20:42 +08:00
|
|
|
} else {
|
|
|
|
/* has no token*/
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
next()
|
2017-11-02 14:41:37 +08:00
|
|
|
} else {
|
2018-05-12 09:20:42 +08:00
|
|
|
next('/login')
|
|
|
|
NProgress.done()
|
2017-11-02 14:41:37 +08:00
|
|
|
}
|
2018-05-12 09:20:42 +08:00
|
|
|
}
|
2017-11-02 14:41:37 +08:00
|
|
|
})
|
|
|
|
|
2018-05-12 09:20:42 +08:00
|
|
|
// 寻找子菜单的父类
|
2018-03-22 16:28:16 +08:00
|
|
|
function findMenuParent(tag) {
|
2018-05-12 09:20:42 +08:00
|
|
|
let tagCurrent = []
|
|
|
|
const menu = store.getters.menu
|
|
|
|
tagCurrent.push(tag)
|
|
|
|
return tagCurrent
|
|
|
|
// //如果是一级菜单直接返回
|
|
|
|
// for (let i = 0, j = menu.length; i < j; i++) {
|
|
|
|
// if (menu[i].href == tag.value) {
|
|
|
|
// tagCurrent.push(tag);
|
|
|
|
// return tagCurrent;
|
|
|
|
// }
|
|
|
|
// }
|
2018-03-22 16:28:16 +08:00
|
|
|
|
2018-05-12 09:20:42 +08:00
|
|
|
// let currentPathObj = menu.filter(item => {
|
|
|
|
// if (item.children.length == 1) {
|
|
|
|
// return item.children[0].href === tag.value;
|
|
|
|
// } else {
|
|
|
|
// let i = 0;
|
|
|
|
// let childArr = item.children;
|
|
|
|
// let len = childArr.length;
|
|
|
|
// while (i < len) {
|
|
|
|
// if (childArr[i].href === tag.value) {
|
|
|
|
// return true;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// i++;
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// })[0];
|
|
|
|
// tagCurrent.push({
|
|
|
|
// label: currentPathObj.label,
|
|
|
|
// value: currentPathObj.href
|
|
|
|
// });
|
|
|
|
// tagCurrent.push(tag);
|
|
|
|
// return tagCurrent;
|
2018-03-04 13:49:35 +08:00
|
|
|
}
|
2018-05-12 09:20:42 +08:00
|
|
|
|
2018-03-04 13:49:35 +08:00
|
|
|
router.afterEach((to, from) => {
|
2018-05-12 09:20:42 +08:00
|
|
|
NProgress.done()
|
|
|
|
setTimeout(() => {
|
|
|
|
const tag = store.getters.tag
|
|
|
|
setTitle(tag.label)
|
|
|
|
store.commit('SET_TAG_CURRENT', findMenuParent(tag))
|
|
|
|
}, 0)
|
2018-03-22 22:04:33 +08:00
|
|
|
})
|