fix(plugin-access): 修复切换角色时,getAccess 不正确 (#240)

This commit is contained in:
1zumii 2024-04-29 10:47:09 +08:00 committed by GitHub
parent 5a041f0882
commit 1b0d3409bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 12 deletions

View File

@ -1,7 +1,7 @@
import { reactive, unref, computed, inject } from "vue";
import createDirective from "./createDirective";
import createComponent from "./createComponent";
import { computed, reactive, unref } from "vue";
import { isPlainObject } from "{{{ lodashPath }}}";
import createComponent from "./createComponent";
import createDirective from "./createDirective";
function isPromise(obj) {
return (
@ -20,12 +20,23 @@ const state = reactive({
const rolePromiseList = [];
const accessPromiseList = [];
// 预设的 accessId且不会被移除
const presetAccessIds = []
const setPresetAccess = (access) => {
const accessIds = Array.isArray(access) ? access : [access];
presetAccessIds.push(...accessIds.filter(id => !presetAccessIds.includes(id)));
}
const getAllowAccessIds = () => {
const result = [...presetAccessIds, ...state.currentAccessIds];
const roleAccessIds = state.roles[state.currentRoleId];
if (Array.isArray(roleAccessIds) && roleAccessIds.length > 0) {
return state.currentAccessIds.concat(roleAccessIds);
result.push(...roleAccessIds);
}
return state.currentAccessIds;
return result;
};
const _syncSetAccessIds = (promise) => {
@ -144,6 +155,7 @@ export const access = {
setAccess,
match,
getAccess: getAllowAccessIds,
setPresetAccess,
};
export const hasAccessSync = (path) => {

View File

@ -8,13 +8,8 @@ if (!accessApi) {
export const access = (memo) => {
const runtimeConfig = getConfig();
const accessIds = accessApi.getAccess();
if (!accessIds.includes('/403')) {
accessApi.setAccess(accessIds.concat('/403'));
}
if (!accessIds.includes('/404')) {
accessApi.setAccess(accessIds.concat('/404'));
}
accessApi.setPresetAccess(['/403', '/404']);
return {
unAccessHandler({ router, to, from, next }) {
if (runtimeConfig.unAccessHandler && typeof runtimeConfig.unAccessHandler === 'function') {