From be118172e278fcedcec7cec8ac24b59c837bc4ea Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Thu, 10 Mar 2022 20:18:58 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=20(#1443)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: 修改路由过滤 * fix: 去掉 debug Co-authored-by: ibuler --- src/components/ListTable/index.vue | 2 +- src/guards.js | 26 +++++++++++++++++-- src/router/workspace/index.js | 2 +- src/utils/jms.js | 3 +++ src/utils/startup.js | 19 +------------- src/views/assets/Asset/AssetDetail/Detail.vue | 2 +- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/components/ListTable/index.vue b/src/components/ListTable/index.vue index 68103e878..97369f53c 100644 --- a/src/components/ListTable/index.vue +++ b/src/components/ListTable/index.vue @@ -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 } }, diff --git a/src/guards.js b/src/guards.js index 0e0ecf4d5..b651a24fd 100644 --- a/src/guards.js +++ b/src/guards.js @@ -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 }) diff --git a/src/router/workspace/index.js b/src/router/workspace/index.js index abe50e5f0..558c33947 100644 --- a/src/router/workspace/index.js +++ b/src/router/workspace/index.js @@ -24,7 +24,7 @@ export default { name: 'MyHome', component: () => import('@/views/myhome'), meta: { - icon: 'dashboard', + icon: 'area-chart', title: i18n.t('route.Overview'), permissions: [] } diff --git a/src/utils/jms.js b/src/utils/jms.js index bfc57ff41..8a4d5d0a2 100644 --- a/src/utils/jms.js +++ b/src/utils/jms.js @@ -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) diff --git a/src/utils/startup.js b/src/utils/startup.js index 66cf1ee5f..6b8bd699e 100644 --- a/src/utils/startup.js +++ b/src/utils/startup.js @@ -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 } diff --git a/src/views/assets/Asset/AssetDetail/Detail.vue b/src/views/assets/Asset/AssetDetail/Detail.vue index 7c44add1f..332e15ccf 100644 --- a/src/views/assets/Asset/AssetDetail/Detail.vue +++ b/src/views/assets/Asset/AssetDetail/Detail.vue @@ -6,7 +6,7 @@ - +