diff --git a/packages/create-fes-app/templates/app/pc/src/pages/index.vue b/packages/create-fes-app/templates/app/pc/src/pages/index.vue index 3a304634..ef41dd64 100644 --- a/packages/create-fes-app/templates/app/pc/src/pages/index.vue +++ b/packages/create-fes-app/templates/app/pc/src/pages/index.vue @@ -73,7 +73,6 @@ export default { locale.setLocale({ lang: 'en-US' }); locale.addLocale({ lang: 'ja-JP', messages: { test: 'テスト' } }); console.log(locale.getAllLocales()); - access.addAccess('/onepiece1'); }, 2000); setTimeout(() => { accessId.value = '11'; diff --git a/packages/fes-plugin-access/src/runtime/core.tpl b/packages/fes-plugin-access/src/runtime/core.tpl index 15d44688..cd0c9c3c 100644 --- a/packages/fes-plugin-access/src/runtime/core.tpl +++ b/packages/fes-plugin-access/src/runtime/core.tpl @@ -1,4 +1,4 @@ -import { reactive, computed, inject } from "vue"; +import { reactive, unref, computed, inject } from "vue"; import createDirective from "./createDirective"; import createComponent from "./createComponent"; @@ -56,12 +56,9 @@ const setAccess = (accessIds) => { state.currentAccessIds = accessIds; }; -const addAccess = (accessId) => { - if (typeof accessId !== 'string') { - throw new Error("[plugin-access]: argument to the addAccess() must be string"); - } - state.currentAccessIds.push(accessId); -}; +const getAccess = () => { + return state.currentAccessIds.slice(0) +} const _syncSetRoleId = (promise) => { rolePromiseList.push(promise); @@ -116,12 +113,12 @@ const match = (path, accessIds) => { return false; }; -const hasLoading = () => { +const isDataReady = () => { return rolePromiseList.length || accessPromiseList.length; }; const hasAccess = async (path) => { - if (!hasLoading()) { + if (!isDataReady()) { return match(path, getAllowAccessIds()); } await Promise.all(rolePromiseList.concat(accessPromiseList)); @@ -132,7 +129,7 @@ export const install = (app) => { const allowPageIds = computed(getAllowAccessIds); const useAccess = (path) => { const result = computed(() => { - return match(path, allowPageIds.value); + return match(unref(path), allowPageIds.value); }); return result; }; @@ -143,10 +140,10 @@ export const install = (app) => { export const access = { hasAccess, - hasLoading, + isDataReady, setRole, setAccess, - addAccess + getAccess, }; export const useAccess = (path) => { diff --git a/packages/fes-plugin-access/src/runtime/runtime.js b/packages/fes-plugin-access/src/runtime/runtime.js index 01d19641..46a877d2 100644 --- a/packages/fes-plugin-access/src/runtime/runtime.js +++ b/packages/fes-plugin-access/src/runtime/runtime.js @@ -3,6 +3,18 @@ import { access, install } from './core'; export function onRouterCreated({ router }) { router.beforeEach(async (to, from, next) => { + const runtimeConfig = plugin.applyPlugins({ + key: 'access', + type: ApplyPluginsType.modify, + initialValue: {} + }); + if (to.matched.length === 0) { + if (runtimeConfig.noFoundHandler && typeof runtimeConfig.noFoundHandler === 'function') { + return runtimeConfig.noFoundHandler({ + router, to, from, next + }); + } + } let path; if (to.matched.length === 1) { path = to.matched[0].path; @@ -11,21 +23,14 @@ export function onRouterCreated({ router }) { } const canRoute = await access.hasAccess(path); if (canRoute) { - next(); - } else { - const runtimeConfig = plugin.applyPlugins({ - key: 'access', - type: ApplyPluginsType.modify, - initialValue: {} - }); - if (runtimeConfig.noAccessHandler && typeof runtimeConfig.noAccessHandler === 'function') { - runtimeConfig.noAccessHandler({ - router, to, from, next - }); - } else { - next(false); - } + return next(); } + if (runtimeConfig.unAccessHandler && typeof runtimeConfig.unAccessHandler === 'function') { + return runtimeConfig.unAccessHandler({ + router, to, from, next + }); + } + next(false); }); } diff --git a/packages/fes-plugin-layout/src/runtime/helpers/addAccessTag.js b/packages/fes-plugin-layout/src/runtime/helpers/pluginAccess.js similarity index 72% rename from packages/fes-plugin-layout/src/runtime/helpers/addAccessTag.js rename to packages/fes-plugin-layout/src/runtime/helpers/pluginAccess.js index ec962335..f1221540 100644 --- a/packages/fes-plugin-layout/src/runtime/helpers/addAccessTag.js +++ b/packages/fes-plugin-layout/src/runtime/helpers/pluginAccess.js @@ -7,29 +7,29 @@ if (!useAccess) { ); } -const hasAccess = (item) => { +export const hasAccessByMenuItem = (item) => { let res; if (item.path && (!item.children || item.children.length === 0)) { res = useAccess(item.path); } else if (item.children && item.children.length > 0) { - res = computed(() => item.children.some(child => hasAccess(child))); + res = computed(() => item.children.some(child => hasAccessByMenuItem(child))); } return res; }; -const addAcessTag = (arr) => { +const _addAccessTag = (arr) => { if (Array.isArray(arr)) { arr.forEach((item) => { - item.access = hasAccess(item); + item.access = hasAccessByMenuItem(item); if (item.children && item.children.length > 0) { - addAcessTag(item.children); + _addAccessTag(item.children); } }); } }; -export default function (menus) { +export const addAccessTag = (menus) => { const originData = unref(menus); - addAcessTag(originData); + _addAccessTag(originData); return originData; -} +}; diff --git a/packages/fes-plugin-layout/src/runtime/views/Menu.vue b/packages/fes-plugin-layout/src/runtime/views/Menu.vue index 5ee7d084..1240fdec 100644 --- a/packages/fes-plugin-layout/src/runtime/views/Menu.vue +++ b/packages/fes-plugin-layout/src/runtime/views/Menu.vue @@ -1,11 +1,11 @@