fix: 修复accessApi.setAccess['/403']执行多次问题

This commit is contained in:
wanchun 2023-04-11 11:35:17 +08:00
parent 270d116dfd
commit 86f5504c11
3 changed files with 21 additions and 25 deletions

View File

@ -3,8 +3,6 @@ import createDirective from "./createDirective";
import createComponent from "./createComponent"; import createComponent from "./createComponent";
import {isPlainObject} from "{{{ lodashPath }}}"; import {isPlainObject} from "{{{ lodashPath }}}";
const accessKey = Symbol("plugin-access");
function isPromise(obj) { function isPromise(obj) {
return ( return (
!!obj && !!obj &&
@ -132,14 +130,6 @@ const hasAccess = async (path) => {
}; };
export const install = (app) => { 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.directive("access", createDirective(useAccess));
app.component("Access", createComponent(useAccess)); app.component("Access", createComponent(useAccess));
}; };
@ -153,6 +143,14 @@ export const access = {
getAccess: getAllowAccessIds, getAccess: getAllowAccessIds,
}; };
export const hasAccessSync = (path) => {
return match(unref(path), getAllowAccessIds());
}
export const useAccess = (path) => { export const useAccess = (path) => {
return inject(accessKey)(path); const allowPageIds = computed(getAllowAccessIds);
const result = computed(() => {
return match(unref(path), allowPageIds.value);
});
return result;
}; };

View File

@ -1,32 +1,29 @@
import { computed, ref } from 'vue';
// eslint-disable-next-line // 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-accessplease install plugin-access first'); throw new Error('[plugin-layout]: pLugin-layout depends on plugin-accessplease install plugin-access first');
} }
export const hasAccessByMenuItem = (item) => { export const hasAccessByMenuItem = (item) => {
const hasChild = item.children && item.children.length; const hasChild = item.children && item.children.length;
if (item.path && !hasChild) { if (item.path && !hasChild) {
return useAccess(item.path); return hasAccessSync(item.path);
} }
if (hasChild) { if (hasChild) {
return computed(() => return item.children.some((child) => {
item.children.some((child) => { const rst = hasAccessByMenuItem(child);
const rst = hasAccessByMenuItem(child); return rst;
return rst && rst.value; });
}),
);
} }
return ref(true); return true;
}; };
export const transform = (menus) => export const transform = (menus) =>
menus menus
.map((menu) => { .map((menu) => {
const hasAccess = hasAccessByMenuItem(menu); const hasAccess = hasAccessByMenuItem(menu);
if (!hasAccess.value) { if (!hasAccess) {
return false; return false;
} }
if (menu.children) { if (menu.children) {

View File

@ -7,10 +7,11 @@ if (!accessApi) {
throw new Error('[plugin-layout]: plugin-layout depends on plugin-accessplease install plugin-access first'); throw new Error('[plugin-layout]: plugin-layout depends on plugin-accessplease install plugin-access first');
} }
const accessIds = accessApi.getAccess();
accessApi.setAccess(accessIds.concat(['/403', '/404']));
export const access = (memo) => { export const access = (memo) => {
const runtimeConfig = getConfig(); const runtimeConfig = getConfig();
const accessIds = accessApi.getAccess();
accessApi.setAccess(accessIds.concat(['/403', '/404']));
return { return {
unAccessHandler({ router, to, from, next }) { unAccessHandler({ router, to, from, next }) {
if (runtimeConfig.unAccessHandler && typeof runtimeConfig.unAccessHandler === 'function') { if (runtimeConfig.unAccessHandler && typeof runtimeConfig.unAccessHandler === 'function') {