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

@@ -113,7 +113,7 @@ export default {
return result
}, {})
const perms = Object.assign(defaultPermissions, permissions)
this.$log.debug('Permissions: ', perms)
// this.$log.debug('Permissions: ', perms)
return perms
}
},

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 })

View File

@@ -24,7 +24,7 @@ export default {
name: 'MyHome',
component: () => import('@/views/myhome'),
meta: {
icon: 'dashboard',
icon: 'area-chart',
title: i18n.t('route.Overview'),
permissions: []
}

View File

@@ -131,6 +131,9 @@ export function getRouteViewRequirePerms(route) {
}
export function hasRouteViewPerm(route) {
if (route.name) {
return hasPermission(route.meta.permissions)
}
const viewName = route.path.split('/')[1]
const perms = getViewRequirePerms(viewName)
return hasPermission(perms)

View File

@@ -7,7 +7,6 @@ import 'nprogress/nprogress.css' // progress bar style
import { getTokenFromCookie } from '@/utils/auth'
import orgUtil from '@/utils/org'
import { getCurrentOrg } from '@/api/orgs'
import { getPropView, hasRouteViewPerm, isSameView } from '@/utils/jms'
const whiteList = ['/login', process.env.VUE_APP_LOGIN_PATH] // no redirect whitelist
let initial = false
@@ -108,21 +107,6 @@ export async function checkUserFirstLogin({ to, from, next }) {
}
}
export async function changeCurrentViewIfNeed({ to, from, next }) {
if (!to.path || isSameView(to, from)) {
return
}
const hasPerm = hasRouteViewPerm(to)
Vue.$log.debug('Change current view if need: ', 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}`)
}
export async function startup({ to, from, next }) {
if (initial) {
return true
@@ -133,9 +117,8 @@ export async function startup({ to, from, next }) {
await getPublicSetting({ to, from, next })
// await setHeadTitle({ to, from, next })
await checkLogin({ to, from, next })
await changeCurrentOrgIfNeed({ to, from, next })
await generatePageRoutes({ to, from, next })
await changeCurrentViewIfNeed({ to, from, next })
await changeCurrentOrgIfNeed({ to, from, next })
await checkUserFirstLogin({ to, from, next })
return true
}

View File

@@ -6,7 +6,7 @@
<el-col :span="10">
<QuickActions type="primary" :actions="quickActions" />
<RelationCard ref="NodeRelation" v-perms="'assets.change_asset'" type="info" style="margin-top: 15px" v-bind="nodeRelationConfig" />
<LabelCard type="warning" style="margin-top: 15px" v-bind="labelConfig" />
<LabelCard v-perms="'assets.view_label'" type="warning" style="margin-top: 15px" v-bind="labelConfig" />
</el-col>
</el-row>
</template>