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.

105 lines
3.1 KiB

6 years ago
/*
* 路由配置
*
* 需要鉴权的路由!meta.noAuth
6 years ago
* 左侧菜单显示name && !hidden && meta.title
* 左侧菜单排序能在左侧菜单中显示 && sort升序排列
6 years ago
* 左侧菜单不折叠只有一个children的路由alwaysShow
* 面包屑显示meta.title || meta.dynamicTitle
6 years ago
* 搜索选项显示name && meta.title
* tab栏显示name && (meta.title || meta.dynamicTitle)
6 years ago
* tab栏固定显示meta.affix
* 页面不缓存!name && !meta.isDetailPage || meta.noCache
6 years ago
* 打开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 "@/util/storage"
import {auth, needAuth} from "@/util/auth"
import {setPageTitle, specifyRouteTitle, 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})
metaExtend(constantRoutes)
metaExtend(authorityRoutes)
6 years ago
const endRoute = [{path: '*', redirect: '/404', hidden: true}]
const whiteList = transformWhiteList(['/login', '/register', '/404', '/403'])
6 years ago
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
if (needExtraRedirect(to, from)) {
//这里vue-router会报redirect错误,已在main.js中忽略
return next(`/redirect${to.fullPath}`)
}
specifyRouteTitle(to, from)
setPageTitle(to)
6 years ago
//白名单内不需要进行权限控制
if (whiteList.some(reg => reg.test(to.path))) return next()
6 years ago
//未登录时返回登录页
if (!isUserExist()) return next({path: '/login', query: {redirect: to.fullPath}})
6 years ago
await initMenuAndResource()
6 years ago
//页面不需要鉴权或有访问权限时通过
if (!needAuth(to) || auth(to.path)) {
iframeControl(to, from)
6 years ago
return next()
}
6 years ago
//用户无权限访问时的动作
next('/403')
6 years ago
})
router.afterEach(() => NProgress.done())
6 years ago
//判断是否需要一次额外的redirect跳转
function needExtraRedirect(to, from) {
//若是详情页之间的跳转,借助redirect避免组件复用
const isToDetailPage = to.meta.isDetailPage,
isFromDetailPage = from.meta.isDetailPage
return isToDetailPage !== undefined && isToDetailPage === isFromDetailPage
}
6 years ago
//初始化菜单和权限
function initMenuAndResource() {
6 years ago
if (!store.state.resource.hasInitRoutes) {
return store.dispatch('resource/init', store.state.user)
}
return Promise.resolve()
}
//判断是否需要打开iframe
function iframeControl(to, from) {
let iframe = to.meta.iframe
const operate = iframe ? 'open' : 'close'
if (to.path === `/redirect${from.path}`) {
iframe = from.meta.iframe
}
return store.dispatch(`iframe/${operate}`, iframe)
6 years ago
}
export default router