diff --git a/packages/fes-plugin-access/src/index.js b/packages/fes-plugin-access/src/index.js index f7471110..0b1224c8 100644 --- a/packages/fes-plugin-access/src/index.js +++ b/packages/fes-plugin-access/src/index.js @@ -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 { join } from 'path'; @@ -127,5 +49,7 @@ export default (api) => { } ]); + api.addRuntimePluginKey(() => 'notAllowHandler'); + api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`); }; diff --git a/packages/fes-plugin-access/src/template/runtime.tpl b/packages/fes-plugin-access/src/template/runtime.tpl index d1104464..379cfe33 100644 --- a/packages/fes-plugin-access/src/template/runtime.tpl +++ b/packages/fes-plugin-access/src/template/runtime.tpl @@ -11,7 +11,7 @@ async function getInitialState() { } export function onRouterCreated({ router }) { - router.beforeEach(async (to, from) => { + router.beforeEach(async (to, from, next) => { let path; if (to.matched.length === 1) { path = to.matched[0].path; @@ -21,6 +21,18 @@ export function onRouterCreated({ router }) { // 等待初始化数据 await getInitialState(); 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); + } }); } diff --git a/packages/fes-template/.fes.js b/packages/fes-template/.fes.js index f386ba0c..91fb8cea 100644 --- a/packages/fes-template/.fes.js +++ b/packages/fes-template/.fes.js @@ -4,7 +4,7 @@ export default { access: { roles: { - admin: ["/", "/onepiece"] + admin: ["/"] } } };