You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9 KiB

6 years ago
/*
* 路由配置
*
* 需要鉴权的路由!meta.noAuth
6 years ago
* 左侧菜单显示name && !hidden && meta.title
* 左侧菜单排序能显示 && sort升序排列
6 years ago
* 左侧菜单不折叠只有一个children的路由alwaysShow
* 面包屑显示meta.title
* 搜索选项显示name && meta.title
* tab栏显示name && meta.title
6 years ago
* tab栏固定显示meta.affix
* 页面不缓存!name || meta.noCache
* 打开iframemeta.iframe不会重复打开相同src的iframe
* */
import Vue from 'vue'
import Router from 'vue-router'
import store from "@/store"
6 years ago
import NProgress from 'nprogress'
import {isUserExist} from "@/utils/storage"
6 years ago
import {auth, needAuth} from "@/utils/auth"
import {getPageTitle, transformWhiteList, metaExtend} from './util'
import {contextPath, routerMode} from '@/config'
import constantRoutes from '@/router/constant'
import authorityRoutes from '@/router/authority'
6 years ago
Vue.use(Router)
NProgress.configure({showSpinner: false})
const endRoute = [{path: '*', redirect: '/404', hidden: true}]
const whiteList = transformWhiteList(['/login', '/register', '/404', '/403'])
6 years ago
metaExtend(constantRoutes)
metaExtend(authorityRoutes)
const router = new Router({
base: contextPath,
mode: routerMode,
6 years ago
scrollBehavior: () => ({y: 0}),
routes: constantRoutes.concat(authorityRoutes, endRoute)
})
router.beforeEach(async (to, from, next) => {
//使用redirect进行跳转时不显示进度条
!to.path.startsWith('/redirect') && NProgress.start()
6 years ago
//若是详情页之间的跳转,借助redirect避免组件复用
const isToDetailPage = to.meta && to.meta.isDetailPage,
isFromDetailPage = from.meta && from.meta.isDetailPage
if (isToDetailPage !== undefined && isToDetailPage === isFromDetailPage) {
//这里vue-router会报redirect错误
return next({path: `/redirect${to.path}`, query: to.query})
}
document.title = getPageTitle(to)
6 years ago
//白名单内不需要进行权限控制
if (whiteList.some(reg => reg.test(to.path))) return next()
6 years ago
const isLogin = isUserExist()
//未登录时返回登录页
if (!isLogin) return next({path: '/login', query: {redirect: to.fullPath}})
//初始化菜单
await initMenu()
//已登录时访问登录页则跳转至首页
if (to.path === '/login') return next({path: '/'})
//页面不需要鉴权或有访问权限时通过
if (!needAuth(to) || auth(to.path)) {
iframeControl(to)
return next()
}
6 years ago
//用户无权限访问时的动作
next({path: '/403'})
})
router.afterEach(() => NProgress.done())
6 years ago
//初始化菜单和权限
function initMenu() {
if (!store.state.resource.hasInitRoutes) {
return store.dispatch('resource/init', store.state.user)
}
return Promise.resolve()
}
//判断是否需要打开iframe
function iframeControl(route) {
const operate = route.meta.iframe ? 'open' : 'close'
6 years ago
store.dispatch(`iframe/${operate}`, route.meta.iframe)
}
export default router