diff --git a/packages/fes-plugin-layout/src/index.js b/packages/fes-plugin-layout/src/index.js index ff6373a6..5c9eb985 100644 --- a/packages/fes-plugin-layout/src/index.js +++ b/packages/fes-plugin-layout/src/index.js @@ -23,6 +23,8 @@ export default (api) => { const absFilePath = join(namespace, 'index.js'); + const absRuntimeFilePath = join(namespace, 'runtime.js'); + api.onGenerateFiles(() => { const { name } = api.pkg; @@ -53,6 +55,7 @@ export default (api) => { }); }); + api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`); // 把BaseLayout插入到路由配置中,作为根路由 api.modifyRoutes(routes => [ diff --git a/packages/fes-plugin-layout/src/runtime/helpers/index.js b/packages/fes-plugin-layout/src/runtime/helpers/index.js index 484d4343..d21db0c0 100644 --- a/packages/fes-plugin-layout/src/runtime/helpers/index.js +++ b/packages/fes-plugin-layout/src/runtime/helpers/index.js @@ -27,7 +27,7 @@ const matchPath = (config, path) => { for (let i = 0; i < config.length; i++) { const item = config[i]; if (item.path && item.path === path) { - res = item.meta; + res = item.meta || {}; res.path = item.path; break; } diff --git a/packages/fes-plugin-layout/src/runtime/runtime.js b/packages/fes-plugin-layout/src/runtime/runtime.js new file mode 100644 index 00000000..c0e04ebc --- /dev/null +++ b/packages/fes-plugin-layout/src/runtime/runtime.js @@ -0,0 +1,69 @@ +import { plugin, ApplyPluginsType } from '@@/core/coreExports'; +import { access as accessApi } from '../plugin-access/core'; +import Exception404 from './views/404'; +import Exception403 from './views/403'; + +if (!accessApi) { + throw new Error( + '[plugin-layout]: pLugin-layout depends on plugin-access,please install plugin-access first!' + ); +} + +const handle = (type, router) => { + const accesssIds = accessApi.getAccess(); + const path = `/${type}`; + const name = `Exception${type}`; + const components = { + 404: Exception404, + 403: Exception403 + }; + if (!accesssIds.includes(path)) { + accessApi.setAccess(accesssIds.concat([path])); + } + if (!router.hasRoute(name)) { + router.addRoute({ path, name, component: components[type] }); + } +}; + +export const access = { + unAccessHandler({ + router, to, from, next + }) { + const runtimeConfig = plugin.applyPlugins({ + key: 'layout', + type: ApplyPluginsType.modify, + initialValue: {} + }); + if (runtimeConfig.unAccessHandler && typeof runtimeConfig.unAccessHandler === 'function') { + return runtimeConfig.unAccessHandler({ + router, to, from, next + }); + } + if (to.path === '/404') { + handle(404, router); + return next('/404'); + } + handle(403, router); + next('/403'); + }, + noFoundHandler({ + router, to, from, next + }) { + const runtimeConfig = plugin.applyPlugins({ + key: 'layout', + type: ApplyPluginsType.modify, + initialValue: {} + }); + if (runtimeConfig.noFoundHandler && typeof runtimeConfig.noFoundHandler === 'function') { + return runtimeConfig.noFoundHandler({ + router, to, from, next + }); + } + if (to.path === '/403') { + handle(403, router); + return next('/403'); + } + handle(404, router); + next('/404'); + } +}; diff --git a/packages/fes-template/src/pages/403.vue b/packages/fes-plugin-layout/src/runtime/views/403.vue similarity index 52% rename from packages/fes-template/src/pages/403.vue rename to packages/fes-plugin-layout/src/runtime/views/403.vue index 51ece736..b1c61d9d 100644 --- a/packages/fes-template/src/pages/403.vue +++ b/packages/fes-plugin-layout/src/runtime/views/403.vue @@ -1,7 +1,7 @@ @@ -11,6 +11,7 @@ } diff --git a/packages/fes-template/src/pages/404.vue b/packages/fes-plugin-layout/src/runtime/views/404.vue similarity index 52% rename from packages/fes-template/src/pages/404.vue rename to packages/fes-plugin-layout/src/runtime/views/404.vue index 29e834c6..2b1972fa 100644 --- a/packages/fes-template/src/pages/404.vue +++ b/packages/fes-plugin-layout/src/runtime/views/404.vue @@ -1,7 +1,7 @@ @@ -11,6 +11,7 @@ } diff --git a/packages/fes-template/src/app.js b/packages/fes-template/src/app.js index cb557d33..7009c2b4 100644 --- a/packages/fes-template/src/app.js +++ b/packages/fes-template/src/app.js @@ -21,25 +21,10 @@ export const beforeRender = { export const layout = { customHeader: -}; - -export const access = { - unAccessHandler({ to, next }) { - const accesssIds = accessApi.getAccess(); - if (to.path === '/404') { - accessApi.setAccess(accesssIds.concat(['/404'])); - return next('/404'); - } - if (!accesssIds.includes('/403')) { - accessApi.setAccess(accesssIds.concat(['/403'])); - } - next('/403'); - }, - noFoundHandler({ next }) { - const accesssIds = accessApi.getAccess(); - if (!accesssIds.includes('/404')) { - accessApi.setAccess(accesssIds.concat(['/404'])); - } - next('/404'); - } + // unAccessHandler({ next }) { + // next(false); + // }, + // noFoundHandler({ next }) { + // next(false); + // } };