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
|
2021-12-29 10:57:46 +08:00
|
|
|
|
import {
|
|
|
|
|
getToken
|
|
|
|
|
} from '@/utils/auth' // get token from cookie
|
2021-03-03 15:06:52 +08:00
|
|
|
|
import getPageTitle from '@/utils/get-page-title'
|
2021-12-29 10:57:46 +08:00
|
|
|
|
import {
|
|
|
|
|
buildMenus
|
|
|
|
|
} from '@/api/system/menu'
|
|
|
|
|
import {
|
|
|
|
|
filterAsyncRouter
|
|
|
|
|
} from '@/store/modules/permission'
|
|
|
|
|
import {
|
|
|
|
|
isMobile
|
|
|
|
|
} from '@/utils/index'
|
2022-01-13 15:10:46 +08:00
|
|
|
|
import Layout from '@/layout/index'
|
2021-09-03 14:40:47 +08:00
|
|
|
|
// import bus from './utils/bus'
|
2021-03-03 15:06:52 +08:00
|
|
|
|
|
2022-04-13 13:39:29 +08:00
|
|
|
|
import { getSocket } from '@/websocket'
|
|
|
|
|
|
2021-12-29 10:57:46 +08:00
|
|
|
|
NProgress.configure({
|
|
|
|
|
showSpinner: false
|
|
|
|
|
}) // NProgress Configuration
|
2021-03-03 15:06:52 +08:00
|
|
|
|
|
2021-11-26 11:38:25 +08:00
|
|
|
|
const whiteList = ['/login', '/401', '/404', '/delink', '/nolic'] // no redirect whitelist
|
2021-03-03 15:06:52 +08:00
|
|
|
|
|
|
|
|
|
router.beforeEach(async(to, from, next) => {
|
|
|
|
|
// start progress bar
|
|
|
|
|
NProgress.start()
|
2022-01-06 23:53:45 +08:00
|
|
|
|
const mobileIgnores = ['/delink']
|
|
|
|
|
const mobilePreview = '/preview/'
|
2021-12-29 14:36:52 +08:00
|
|
|
|
|
2022-01-06 23:53:45 +08:00
|
|
|
|
if (isMobile() && !to.path.includes(mobilePreview) && mobileIgnores.indexOf(to.path) === -1) {
|
2021-12-29 10:57:46 +08:00
|
|
|
|
window.location.href = window.origin + '/app.html'
|
|
|
|
|
NProgress.done()
|
|
|
|
|
}
|
2021-03-03 15:06:52 +08:00
|
|
|
|
|
|
|
|
|
// 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
|
2021-12-29 10:57:46 +08:00
|
|
|
|
next({
|
|
|
|
|
path: '/'
|
|
|
|
|
})
|
2021-03-03 15:06:52 +08:00
|
|
|
|
NProgress.done()
|
|
|
|
|
} else {
|
|
|
|
|
const hasGetUserInfo = store.getters.name
|
2021-12-21 16:05:14 +08:00
|
|
|
|
if (hasGetUserInfo || to.path.indexOf('/previewScreenShot/') > -1 || to.path.indexOf('/preview/') > -1 || to.path.indexOf('/delink') > -1 || to.path.indexOf('/nolic') > -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(() => {
|
2022-04-13 13:39:29 +08:00
|
|
|
|
const deWebsocket = getSocket()
|
|
|
|
|
deWebsocket && deWebsocket.reconnect && deWebsocket.reconnect()
|
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-12-01 10:48:06 +08:00
|
|
|
|
const datas = res.data
|
|
|
|
|
const filterDatas = filterRouter(datas)
|
2021-03-05 16:07:44 +08:00
|
|
|
|
const asyncRouter = filterAsyncRouter(filterDatas)
|
2022-01-13 15:10:46 +08:00
|
|
|
|
// 如果包含首页 则默认页面是 首页 否则默认页面是仪表板页面
|
|
|
|
|
if (JSON.stringify(datas).indexOf('wizard') > -1) {
|
|
|
|
|
asyncRouter.push({
|
|
|
|
|
path: '/',
|
|
|
|
|
component: Layout,
|
|
|
|
|
redirect: '/wizard/index',
|
|
|
|
|
hidden: true
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
asyncRouter.push({
|
|
|
|
|
path: '/',
|
|
|
|
|
component: Layout,
|
|
|
|
|
redirect: '/panel/index',
|
|
|
|
|
hidden: true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:57:46 +08:00
|
|
|
|
asyncRouter.push({
|
|
|
|
|
path: '*',
|
|
|
|
|
redirect: '/404',
|
|
|
|
|
hidden: true
|
|
|
|
|
})
|
2021-03-03 15:06:52 +08:00
|
|
|
|
store.dispatch('permission/GenerateRoutes', asyncRouter).then(() => { // 存储路由
|
|
|
|
|
router.addRoutes(asyncRouter)
|
2021-05-12 18:26:18 +08:00
|
|
|
|
if (pathValid(to.path, asyncRouter)) {
|
2021-12-29 10:57:46 +08:00
|
|
|
|
next({
|
|
|
|
|
...to,
|
|
|
|
|
replace: true
|
|
|
|
|
})
|
2021-05-12 18:26:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
next('/')
|
|
|
|
|
}
|
2021-03-03 15:06:52 +08:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-07-02 19:19:38 +08:00
|
|
|
|
|
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()
|
|
|
|
|
})
|