perf: 修改路由过滤 (#1443)

* perf: 修改路由过滤

* fix: 去掉 debug

Co-authored-by: ibuler <ibuler@qq.com>
This commit is contained in:
fit2bot
2022-03-10 20:18:58 +08:00
committed by GitHub
parent a8beac0b7a
commit be118172e2
6 changed files with 31 additions and 23 deletions

View File

@@ -4,7 +4,8 @@ import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { startup } from '@/utils/startup'
import store from '@/store'
import { isSameView } from '@/utils/jms'
import { getPropView, hasRouteViewPerm, isSameView } from '@/utils/jms'
import Vue from 'vue'
NProgress.configure({
showSpinner: false
@@ -29,6 +30,21 @@ function generateViewRoutesIfChange({ to, from }) {
}
}
async function changeCurrentViewIfNeed({ to, from, next }) {
if (!to.path || isSameView(to, from)) {
return
}
const hasPerm = hasRouteViewPerm(to)
Vue.$log.debug('Change has current view, has perm: ', hasPerm)
if (hasPerm) {
Vue.$log.debug('Has current view perm')
return
}
const view = getPropView()
Vue.$log.debug('Get prop view and goto: ', view)
next(`/${view}`)
}
function setPageTitle() {
const currentRoute = router.currentRoute
const loginTitle = store.getters.publicSettings['LOGIN_TITLE']
@@ -38,7 +54,13 @@ function setPageTitle() {
}
}
router.afterEach(async(to, from, next) => {
router.beforeResolve(async(to, from, next) => {
/* must call `next` */
await changeCurrentViewIfNeed({ to, from, next })
next()
})
router.afterEach(async(to, from) => {
// finish progress bar
await setPageTitle()
await generateViewRoutesIfChange({ to, from })