mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
feat: plugin-access添加无权限处理器
This commit is contained in:
parent
6cbdb70b44
commit
050e4a7b10
@ -1,81 +1,3 @@
|
|||||||
// import { reactive } from 'vue';
|
|
||||||
|
|
||||||
|
|
||||||
// let accessElementTags = null;
|
|
||||||
|
|
||||||
// // TODO 支持异步函数
|
|
||||||
// export const addAccessElementTags = (elementTags) => {
|
|
||||||
// if (accessElementTags) {
|
|
||||||
// if (typeof elementTags === 'string') {
|
|
||||||
// accessElementTags.push(elementTags);
|
|
||||||
// } else {
|
|
||||||
// accessElementTags.push(...elementTags);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// // TODO 移除权限 + 支持异步函数
|
|
||||||
// export const delAccessElementTags = () => {
|
|
||||||
// console.log('todo');
|
|
||||||
// };
|
|
||||||
|
|
||||||
// // 验证一个资源是否可以访问
|
|
||||||
// export const accessValidator = (elementTag) => {
|
|
||||||
// elementTag = elementTag.split('?')[0];
|
|
||||||
// if (Array.isArray(accessElementTags) && accessElementTags.length > 0) {
|
|
||||||
// if (elementTag === '' && accessElementTags.includes('/')) return true;
|
|
||||||
// if (elementTag) {
|
|
||||||
// for (let i = 0; i < accessElementTags.length; i++) {
|
|
||||||
// if (elementTag === accessElementTags[i]) {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// // 支持*匹配
|
|
||||||
// const reg = new RegExp(`^${accessElementTags[i].replace('*', '.+')}$`);
|
|
||||||
// if (reg.test(elementTag)) {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return true;
|
|
||||||
// };
|
|
||||||
|
|
||||||
|
|
||||||
// export const createAccessHandler = () => ({
|
|
||||||
// async install(app, options, ctx) {
|
|
||||||
// try {
|
|
||||||
// if (typeof options.accessElementTags === 'function') {
|
|
||||||
// const elementTags = await options.accessElementTags(ctx);
|
|
||||||
// accessElementTags = reactive(elementTags || []);
|
|
||||||
// } else {
|
|
||||||
// accessElementTags = reactive(options.accessElementTags || []);
|
|
||||||
// }
|
|
||||||
// const elWeakMap = new WeakMap();
|
|
||||||
// app.directive('access', (el, binding) => {
|
|
||||||
// // TODO 当 accessElementTags 变更的时候调用 forceUpdate 更新组件
|
|
||||||
// if (!elWeakMap.has(el)) {
|
|
||||||
// elWeakMap.set(el, {
|
|
||||||
// display: el.style.display
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// const elementTags = Array.isArray(binding.value) ? binding.value : binding.value;
|
|
||||||
// if (elementTags.some(elementTag => accessValidator(elementTag))) {
|
|
||||||
// el.style.display = elWeakMap.get(el).display;
|
|
||||||
// } else {
|
|
||||||
// el.style.display = 'none';
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // TODO 异步权限
|
|
||||||
// ctx.router.beforeEach(to => accessValidator(to.path));
|
|
||||||
|
|
||||||
// ctx.accessValidator = accessValidator;
|
|
||||||
// ctx.accessElementTags = accessElementTags;
|
|
||||||
// } catch (err) {
|
|
||||||
// console.error(err);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
|
||||||
@ -127,5 +49,7 @@ export default (api) => {
|
|||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
api.addRuntimePluginKey(() => 'notAllowHandler');
|
||||||
|
|
||||||
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,7 @@ async function getInitialState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function onRouterCreated({ router }) {
|
export function onRouterCreated({ router }) {
|
||||||
router.beforeEach(async (to, from) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
let path;
|
let path;
|
||||||
if (to.matched.length === 1) {
|
if (to.matched.length === 1) {
|
||||||
path = to.matched[0].path;
|
path = to.matched[0].path;
|
||||||
@ -21,6 +21,18 @@ export function onRouterCreated({ router }) {
|
|||||||
// 等待初始化数据
|
// 等待初始化数据
|
||||||
await getInitialState();
|
await getInitialState();
|
||||||
const canRoute = await access.hasAccess(path);
|
const canRoute = await access.hasAccess(path);
|
||||||
return canRoute
|
if (canRoute) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
const notAllowHandler = plugin.applyPlugins({
|
||||||
|
key: "notAllowHandler",
|
||||||
|
type: ApplyPluginsType.modify,
|
||||||
|
initialValue: null,
|
||||||
|
});
|
||||||
|
if (notAllowHandler && typeof notAllowHandler === "function") {
|
||||||
|
notAllowHandler(router, to, from);
|
||||||
|
}
|
||||||
|
next(false);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
export default {
|
export default {
|
||||||
access: {
|
access: {
|
||||||
roles: {
|
roles: {
|
||||||
admin: ["/", "/onepiece"]
|
admin: ["/"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user