2021-03-03 15:06:52 +08:00
|
|
|
|
import router from '@/router'
|
|
|
|
|
import store from './store'
|
|
|
|
|
// import { Message } from 'element-ui'
|
|
|
|
|
import NProgress from 'nprogress' // progress bar
|
|
|
|
|
import 'nprogress/nprogress.css' // progress bar style
|
|
|
|
|
import { getToken } from '@/utils/auth' // get token from cookie
|
|
|
|
|
import getPageTitle from '@/utils/get-page-title'
|
|
|
|
|
import { buildMenus } from '@/api/system/menu'
|
|
|
|
|
import { filterAsyncRouter } from '@/store/modules/permission'
|
|
|
|
|
|
|
|
|
|
NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
|
|
|
|
2021-03-08 18:19:57 +08:00
|
|
|
|
const whiteList = ['/login', '/401', '/404'] // no redirect whitelist
|
2021-03-03 15:06:52 +08:00
|
|
|
|
|
|
|
|
|
router.beforeEach(async(to, from, next) => {
|
|
|
|
|
// start progress bar
|
|
|
|
|
NProgress.start()
|
|
|
|
|
|
|
|
|
|
// set page title
|
|
|
|
|
document.title = getPageTitle(to.meta.title)
|
|
|
|
|
|
|
|
|
|
// determine whether the user has logged in
|
|
|
|
|
const hasToken = getToken()
|
|
|
|
|
|
|
|
|
|
if (hasToken) {
|
|
|
|
|
if (to.path === '/login') {
|
|
|
|
|
// if is logged in, redirect to the home page
|
|
|
|
|
next({ path: '/' })
|
|
|
|
|
NProgress.done()
|
|
|
|
|
} else {
|
|
|
|
|
const hasGetUserInfo = store.getters.name
|
2021-05-21 11:19:15 +08:00
|
|
|
|
if (hasGetUserInfo || to.path.indexOf('/preview/') > -1) {
|
2021-03-03 15:06:52 +08:00
|
|
|
|
next()
|
2021-03-04 14:58:52 +08:00
|
|
|
|
store.dispatch('permission/setCurrentPath', to.path)
|
2021-03-03 15:06:52 +08:00
|
|
|
|
} else {
|
|
|
|
|
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
|
|
|
|
|
// get user info
|
|
|
|
|
store.dispatch('user/getInfo').then(() => {
|
2021-05-12 16:19:41 +08:00
|
|
|
|
store.dispatch('lic/getLicInfo').then(() => {
|
|
|
|
|
loadMenus(next, to)
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
loadMenus(next, to)
|
|
|
|
|
})
|
2021-03-03 15:06:52 +08:00
|
|
|
|
}).catch(() => {
|
2021-03-03 18:20:59 +08:00
|
|
|
|
store.dispatch('user/logout').then(() => {
|
2021-03-03 15:06:52 +08:00
|
|
|
|
location.reload() // 为了重新实例化vue-router对象 避免bug
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
} else if (store.getters.loadMenus) {
|
|
|
|
|
// 修改成false,防止死循环
|
2021-03-09 15:38:36 +08:00
|
|
|
|
store.dispatch('user/updateLoadMenus')
|
2021-05-12 16:19:41 +08:00
|
|
|
|
store.dispatch('lic/getLicInfo').then(() => {
|
|
|
|
|
loadMenus(next, to)
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
loadMenus(next, to)
|
|
|
|
|
})
|
2021-03-03 15:06:52 +08:00
|
|
|
|
} else {
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* has no token*/
|
|
|
|
|
|
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
|
// in the free login whitelist, go directly
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
// other pages that do not have permission to access are redirected to the login page.
|
|
|
|
|
next(`/login?redirect=${to.path}`)
|
|
|
|
|
NProgress.done()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
export const loadMenus = (next, to) => {
|
|
|
|
|
buildMenus().then(res => {
|
2021-03-05 16:07:44 +08:00
|
|
|
|
const filterDatas = filterRouter(res.data)
|
|
|
|
|
const asyncRouter = filterAsyncRouter(filterDatas)
|
2021-07-05 18:18:06 +08:00
|
|
|
|
// addMsgMenu(asyncRouter)
|
2021-03-03 15:06:52 +08:00
|
|
|
|
asyncRouter.push({ path: '*', redirect: '/404', hidden: true })
|
|
|
|
|
store.dispatch('permission/GenerateRoutes', asyncRouter).then(() => { // 存储路由
|
|
|
|
|
router.addRoutes(asyncRouter)
|
2021-05-12 18:26:18 +08:00
|
|
|
|
if (pathValid(to.path, asyncRouter)) {
|
|
|
|
|
next({ ...to, replace: true })
|
|
|
|
|
} else {
|
|
|
|
|
next('/')
|
|
|
|
|
}
|
2021-03-03 15:06:52 +08:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-07-02 19:19:38 +08:00
|
|
|
|
|
|
|
|
|
export const addMsgMenu = asyncRouter => {
|
|
|
|
|
const menu = {
|
|
|
|
|
path: 'system-msg-web',
|
|
|
|
|
component: () => import('@/views/msg/index'),
|
|
|
|
|
name: 'sys-msg-web',
|
|
|
|
|
meta: { title: '站内消息', icon: 'all-msg' },
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: 'all',
|
|
|
|
|
component: () => import('@/views/msg/all'),
|
|
|
|
|
name: 'sys-msg-web-all',
|
|
|
|
|
meta: { title: '所有消息', icon: 'web-msg' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'unread',
|
|
|
|
|
component: () => import('@/views/msg/unread'),
|
|
|
|
|
name: 'sys-msg-web-unread',
|
|
|
|
|
meta: { title: '未读消息', icon: 'unread-msg' }
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'readed',
|
|
|
|
|
component: () => import('@/views/msg/readed'),
|
|
|
|
|
name: 'sys-msg-web-readed',
|
|
|
|
|
meta: { title: '已读消息', icon: 'readed-msg' }
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
asyncRouter.forEach(element => {
|
|
|
|
|
if (element.name === 'system') {
|
|
|
|
|
if (element.children) {
|
|
|
|
|
element.children.splice(0, 0, menu)
|
|
|
|
|
}
|
|
|
|
|
// element.children.push(menu)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-05-12 18:33:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* 验证path是否有效
|
|
|
|
|
* @param {*} path
|
|
|
|
|
* @param {*} routers
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2021-05-12 18:26:18 +08:00
|
|
|
|
const pathValid = (path, routers) => {
|
|
|
|
|
const temp = path.startsWith('/') ? path.substr(1) : path
|
|
|
|
|
const locations = temp.split('/')
|
|
|
|
|
if (locations.length === 0) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hasCurrentRouter(locations, routers, 0)
|
|
|
|
|
}
|
2021-05-12 18:33:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* 递归验证every level
|
|
|
|
|
* @param {*} locations
|
|
|
|
|
* @param {*} routers
|
|
|
|
|
* @param {*} index
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2021-05-12 18:26:18 +08:00
|
|
|
|
const hasCurrentRouter = (locations, routers, index) => {
|
|
|
|
|
const location = locations[index]
|
|
|
|
|
let kids = []
|
2021-05-12 18:33:09 +08:00
|
|
|
|
const isvalid = routers.some(router => {
|
2021-05-12 18:26:18 +08:00
|
|
|
|
kids = router.children
|
|
|
|
|
return (router.path === location || ('/' + location) === router.path)
|
|
|
|
|
})
|
2021-05-12 18:33:09 +08:00
|
|
|
|
if (isvalid && index < locations.length - 1) {
|
2021-05-12 18:26:18 +08:00
|
|
|
|
return hasCurrentRouter(locations, kids, index + 1)
|
|
|
|
|
}
|
2021-05-12 18:33:09 +08:00
|
|
|
|
return isvalid
|
2021-05-12 18:26:18 +08:00
|
|
|
|
}
|
2021-03-05 16:07:44 +08:00
|
|
|
|
// 根据权限过滤菜单
|
|
|
|
|
const filterRouter = routers => {
|
|
|
|
|
const user_permissions = store.getters.permissions
|
2021-06-03 16:23:56 +08:00
|
|
|
|
// if (!user_permissions || user_permissions.length === 0) {
|
|
|
|
|
// return routers
|
|
|
|
|
// }
|
2021-03-05 16:07:44 +08:00
|
|
|
|
const tempResults = routers.filter(router => hasPermission(router, user_permissions))
|
|
|
|
|
// 如果是一级菜单(目录) 没有字菜单 那就移除
|
2021-05-06 14:43:57 +08:00
|
|
|
|
return tempResults.filter(item => {
|
|
|
|
|
if (item.type === 0 && (!item.children || item.children.length === 0)) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
2021-03-05 16:07:44 +08:00
|
|
|
|
}
|
|
|
|
|
const hasPermission = (router, user_permissions) => {
|
|
|
|
|
// 菜单要求权限 但是当前用户权限没有包含菜单权限
|
|
|
|
|
if (router.permission && !user_permissions.includes(router.permission)) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2021-05-12 16:19:41 +08:00
|
|
|
|
if (!filterLic(router)) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2021-03-05 16:07:44 +08:00
|
|
|
|
// 如果有字菜单 则 判断是否满足 ‘任意一个子菜单有权限’
|
|
|
|
|
if (router.children && router.children.length) {
|
|
|
|
|
const permissionChilds = router.children.filter(item => hasPermission(item, user_permissions))
|
|
|
|
|
router.children = permissionChilds
|
|
|
|
|
return router.children.length > 0
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2021-05-12 16:19:41 +08:00
|
|
|
|
const filterLic = (router) => {
|
2021-06-01 15:20:26 +08:00
|
|
|
|
return !router.isPlugin || store.getters.validate
|
2021-05-12 16:19:41 +08:00
|
|
|
|
}
|
2021-03-03 15:06:52 +08:00
|
|
|
|
router.afterEach(() => {
|
|
|
|
|
// finish progress bar
|
|
|
|
|
NProgress.done()
|
|
|
|
|
})
|