From 86f5504c11c4ef1459636038b8d62a543584b0f5 Mon Sep 17 00:00:00 2001 From: wanchun <445436867@qq.com> Date: Tue, 11 Apr 2023 11:35:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DaccessApi.setAccess['/?= =?UTF-8?q?403']=E6=89=A7=E8=A1=8C=E5=A4=9A=E6=AC=A1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fes-plugin-access/src/runtime/core.tpl | 20 ++++++++---------- .../src/runtime/helpers/pluginAccess.js | 21 ++++++++----------- .../fes-plugin-layout/src/runtime/runtime.js | 5 +++-- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/packages/fes-plugin-access/src/runtime/core.tpl b/packages/fes-plugin-access/src/runtime/core.tpl index 12d5f794..5164b130 100644 --- a/packages/fes-plugin-access/src/runtime/core.tpl +++ b/packages/fes-plugin-access/src/runtime/core.tpl @@ -3,8 +3,6 @@ import createDirective from "./createDirective"; import createComponent from "./createComponent"; import {isPlainObject} from "{{{ lodashPath }}}"; -const accessKey = Symbol("plugin-access"); - function isPromise(obj) { return ( !!obj && @@ -132,14 +130,6 @@ const hasAccess = async (path) => { }; export const install = (app) => { - const allowPageIds = computed(getAllowAccessIds); - const useAccess = (path) => { - const result = computed(() => { - return match(unref(path), allowPageIds.value); - }); - return result; - }; - app.provide(accessKey, useAccess); app.directive("access", createDirective(useAccess)); app.component("Access", createComponent(useAccess)); }; @@ -153,6 +143,14 @@ export const access = { getAccess: getAllowAccessIds, }; +export const hasAccessSync = (path) => { + return match(unref(path), getAllowAccessIds()); +} + export const useAccess = (path) => { - return inject(accessKey)(path); + const allowPageIds = computed(getAllowAccessIds); + const result = computed(() => { + return match(unref(path), allowPageIds.value); + }); + return result; }; diff --git a/packages/fes-plugin-layout/src/runtime/helpers/pluginAccess.js b/packages/fes-plugin-layout/src/runtime/helpers/pluginAccess.js index a18b90bd..23f52022 100644 --- a/packages/fes-plugin-layout/src/runtime/helpers/pluginAccess.js +++ b/packages/fes-plugin-layout/src/runtime/helpers/pluginAccess.js @@ -1,32 +1,29 @@ -import { computed, ref } from 'vue'; // eslint-disable-next-line -import { useAccess } from '../../plugin-access/core'; +import { hasAccessSync } from '../../plugin-access/core'; -if (!useAccess) { +if (!hasAccessSync) { throw new Error('[plugin-layout]: pLugin-layout depends on plugin-access,please install plugin-access first!'); } export const hasAccessByMenuItem = (item) => { const hasChild = item.children && item.children.length; if (item.path && !hasChild) { - return useAccess(item.path); + return hasAccessSync(item.path); } if (hasChild) { - return computed(() => - item.children.some((child) => { - const rst = hasAccessByMenuItem(child); - return rst && rst.value; - }), - ); + return item.children.some((child) => { + const rst = hasAccessByMenuItem(child); + return rst; + }); } - return ref(true); + return true; }; export const transform = (menus) => menus .map((menu) => { const hasAccess = hasAccessByMenuItem(menu); - if (!hasAccess.value) { + if (!hasAccess) { return false; } if (menu.children) { diff --git a/packages/fes-plugin-layout/src/runtime/runtime.js b/packages/fes-plugin-layout/src/runtime/runtime.js index de9ab180..e5022259 100644 --- a/packages/fes-plugin-layout/src/runtime/runtime.js +++ b/packages/fes-plugin-layout/src/runtime/runtime.js @@ -7,10 +7,11 @@ if (!accessApi) { throw new Error('[plugin-layout]: plugin-layout depends on plugin-access,please install plugin-access first!'); } +const accessIds = accessApi.getAccess(); +accessApi.setAccess(accessIds.concat(['/403', '/404'])); + export const access = (memo) => { const runtimeConfig = getConfig(); - const accessIds = accessApi.getAccess(); - accessApi.setAccess(accessIds.concat(['/403', '/404'])); return { unAccessHandler({ router, to, from, next }) { if (runtimeConfig.unAccessHandler && typeof runtimeConfig.unAccessHandler === 'function') {