mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
feat: access 30%
This commit is contained in:
parent
57c0d5483f
commit
8fc1141056
@ -3,7 +3,8 @@ import { join } from 'path';
|
|||||||
|
|
||||||
// utils must build before core
|
// utils must build before core
|
||||||
// runtime must build before renderer-react
|
// runtime must build before renderer-react
|
||||||
const headPkgs = ['fes-runtime', 'fes-core', 'fes', 'fes-plugin-built-in', 'fes-plugin-request'];
|
|
||||||
|
const headPkgs = ['fes-runtime', 'fes-core', 'fes', 'fes-plugin-built-in', 'fes-plugin-request', 'fes-plugin-access'];
|
||||||
const tailPkgs = [];
|
const tailPkgs = [];
|
||||||
// const otherPkgs = readdirSync(join(__dirname, 'packages')).filter(
|
// const otherPkgs = readdirSync(join(__dirname, 'packages')).filter(
|
||||||
// (pkg) =>
|
// (pkg) =>
|
||||||
@ -14,7 +15,7 @@ const otherPkgs = [];
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
target: 'node',
|
target: 'node',
|
||||||
cjs: { type: 'babel', lazy: true },
|
cjs: { type: 'babel', lazy: false },
|
||||||
disableTypeCheck: true,
|
disableTypeCheck: true,
|
||||||
pkgs: [...headPkgs, ...otherPkgs, ...tailPkgs],
|
pkgs: [...headPkgs, ...otherPkgs, ...tailPkgs],
|
||||||
};
|
};
|
||||||
|
3
packages/fes-plugin-access/.fatherrc.js
Normal file
3
packages/fes-plugin-access/.fatherrc.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default {
|
||||||
|
disableTypeCheck: false,
|
||||||
|
};
|
@ -2,11 +2,18 @@
|
|||||||
"name": "@webank/fes-plugin-access",
|
"name": "@webank/fes-plugin-access",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "lib/index.js",
|
||||||
|
"files": [
|
||||||
|
"lib"
|
||||||
|
],
|
||||||
|
"module": "dist/index.esm.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"@webank/fes": "^2.0.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,3 +76,45 @@
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
|
const namespace = 'plugin-access';
|
||||||
|
|
||||||
|
export default (api) => {
|
||||||
|
const {
|
||||||
|
utils: { Mustache }
|
||||||
|
} = api;
|
||||||
|
|
||||||
|
api.addRuntimePluginKey(() => 'access');
|
||||||
|
|
||||||
|
const absoluteFilePath = join(namespace, 'core.js');
|
||||||
|
|
||||||
|
const absRuntimeFilePath = join(namespace, 'runtime.js');
|
||||||
|
|
||||||
|
api.onGenerateFiles(() => {
|
||||||
|
// 文件写出
|
||||||
|
const { roles = {} } = api.config.access || {};
|
||||||
|
|
||||||
|
api.writeTmpFile({
|
||||||
|
path: absoluteFilePath,
|
||||||
|
content: Mustache.render(readFileSync(join(__dirname, 'template/core.tpl'), 'utf-8'), {
|
||||||
|
REPLACE_ROLES: JSON.stringify(roles)
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
api.writeTmpFile({
|
||||||
|
path: absRuntimeFilePath,
|
||||||
|
content: readFileSync(join(__dirname, 'template/runtime.tpl'), 'utf-8')
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// api.addExports(() => [
|
||||||
|
// {
|
||||||
|
// exportAll: true,
|
||||||
|
// source: absoluteFilePath
|
||||||
|
// }
|
||||||
|
// ]);
|
||||||
|
|
||||||
|
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
||||||
|
};
|
18
packages/fes-plugin-access/src/runtime.js
Normal file
18
packages/fes-plugin-access/src/runtime.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { hasAccess } from './core';
|
||||||
|
|
||||||
|
export function onRouterCreated({ router }) {
|
||||||
|
router.beforeEach(async (to, from, next) => {
|
||||||
|
let path;
|
||||||
|
if (to.matched.length === 1) {
|
||||||
|
path = to.matched[0].path;
|
||||||
|
} else {
|
||||||
|
path = to.path;
|
||||||
|
}
|
||||||
|
const canRoute = await hasAccess(path);
|
||||||
|
if (canRoute) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
next(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
48
packages/fes-plugin-access/src/template/core.tpl
Normal file
48
packages/fes-plugin-access/src/template/core.tpl
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
const roles = {{{REPLACE_ROLES}}};
|
||||||
|
|
||||||
|
let allowPageIds = [];
|
||||||
|
|
||||||
|
const get = () => Promise.all(allowPageIds).then(data => data.reduce((merge, cur) => merge.concat(cur), []));
|
||||||
|
|
||||||
|
export const setPageIds = (pageIds) => {
|
||||||
|
if (Array.isArray(pageIds)) {
|
||||||
|
allowPageIds = pageIds.map(id => Promise.resolve(id));
|
||||||
|
} else {
|
||||||
|
allowPageIds = [Promise.resolve(pageIds)];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const setRoleId = async (roleId) => {
|
||||||
|
console.log('setRole');
|
||||||
|
const _roleId = await Promise.resolve(roleId);
|
||||||
|
if (typeof _roleId !== 'string') {
|
||||||
|
throw new Error(
|
||||||
|
'[plugin-access]: roleId必须是string或者Promise的结果必须是string',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
setPageIds(roles[_roleId]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const hasAccess = async (path) => {
|
||||||
|
const allowPage = await get();
|
||||||
|
if (!Array.isArray(allowPage) || allowPage.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
path = path.split('?')[0];
|
||||||
|
// 进入"/"路由时,此时path为“”
|
||||||
|
if (path === '') {
|
||||||
|
path = '/';
|
||||||
|
}
|
||||||
|
const len = allowPage.length;
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
if (path === allowPage[i]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// 支持*匹配
|
||||||
|
const reg = new RegExp(`^${allowPage[i].replace('*', '.+')}$`);
|
||||||
|
if (reg.test(path)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
18
packages/fes-plugin-access/src/template/runtime.tpl
Normal file
18
packages/fes-plugin-access/src/template/runtime.tpl
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { hasAccess } from './core';
|
||||||
|
|
||||||
|
export function onRouterCreated({ router }) {
|
||||||
|
router.beforeEach(async (to, from, next) => {
|
||||||
|
let path;
|
||||||
|
if (to.matched.length === 1) {
|
||||||
|
path = to.matched[0].path;
|
||||||
|
} else {
|
||||||
|
path = to.path;
|
||||||
|
}
|
||||||
|
const canRoute = await hasAccess(path);
|
||||||
|
if (canRoute) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
next(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
@ -18,7 +18,12 @@ export default function (api) {
|
|||||||
initialValue: [
|
initialValue: [
|
||||||
'modifyClientRenderOpts',
|
'modifyClientRenderOpts',
|
||||||
'rootContainer',
|
'rootContainer',
|
||||||
'render'
|
// 渲染
|
||||||
|
'render',
|
||||||
|
// 修改路由
|
||||||
|
'patchRoutes',
|
||||||
|
// 生成router时触发
|
||||||
|
'onRouterCreated'
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
const plugins = await api.applyPlugins({
|
const plugins = await api.applyPlugins({
|
||||||
|
@ -21,9 +21,4 @@ export default function (api) {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
api.addExports(() => ({
|
|
||||||
specifiers: ['router'],
|
|
||||||
source: absoluteFilePath
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,15 @@
|
|||||||
import { createRouter as createVueRouter, createWebHashHistory } from '{{{ runtimePath }}}';
|
import { createRouter as createVueRouter, createWebHashHistory, ApplyPluginsType } from '{{{ runtimePath }}}';
|
||||||
|
import { plugin } from './plugin';
|
||||||
|
|
||||||
export function getRoutes() {
|
export function getRoutes() {
|
||||||
const routes = {{{ routes }}};
|
const routes = {{{ routes }}};
|
||||||
// TODO 支持动态变更路由
|
|
||||||
|
plugin.applyPlugins({
|
||||||
|
key: 'patchRoutes',
|
||||||
|
type: ApplyPluginsType.event,
|
||||||
|
args: { routes },
|
||||||
|
});
|
||||||
|
|
||||||
return routes;
|
return routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,7 +23,11 @@ export const createRouter = () => {
|
|||||||
routes: getRoutes()
|
routes: getRoutes()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
plugin.applyPlugins({
|
||||||
|
key: 'onRouterCreated',
|
||||||
|
type: ApplyPluginsType.event,
|
||||||
|
args: { router },
|
||||||
|
});
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { router };
|
|
||||||
|
@ -13,13 +13,14 @@ import { createRouter, getRoutes } from './core/routes';
|
|||||||
{{{ entryCodeAhead }}}
|
{{{ entryCodeAhead }}}
|
||||||
|
|
||||||
const renderClient = (opts = {}) => {
|
const renderClient = (opts = {}) => {
|
||||||
const rootContainer = opts.plugin.applyPlugins({
|
const { plugin, routes, rootElement } = opts;
|
||||||
|
const rootContainer = plugin.applyPlugins({
|
||||||
type: 'modify',
|
type: 'modify',
|
||||||
key: 'rootContainer',
|
key: 'rootContainer',
|
||||||
initialValue: defineComponent(() => () => (<RouterView></RouterView>)),
|
initialValue: defineComponent(() => () => (<RouterView></RouterView>)),
|
||||||
args: {
|
args: {
|
||||||
routes: opts.routes,
|
routes: routes,
|
||||||
plugin: opts.plugin
|
plugin: plugin
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -28,8 +29,8 @@ const renderClient = (opts = {}) => {
|
|||||||
app.use(router);
|
app.use(router);
|
||||||
|
|
||||||
// TODO other plugins install
|
// TODO other plugins install
|
||||||
if (opts.rootElement) {
|
if (rootElement) {
|
||||||
app.mount(opts.rootElement);
|
app.mount(rootElement);
|
||||||
}
|
}
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@webank/fes-template",
|
"name": "@webank/fes-template",
|
||||||
"version": "0.2.1",
|
"version": "2.0.0",
|
||||||
"description": "fes项目模版",
|
"description": "fes项目模版",
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "fes build",
|
"build": "fes build",
|
||||||
"dev": "fes dev"
|
"dev": "fes dev"
|
||||||
@ -35,6 +34,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"vue": "^3.0.2",
|
"vue": "^3.0.2",
|
||||||
"@webank/fes": "^2.0.0",
|
"@webank/fes": "^2.0.0",
|
||||||
"@webank/fes-plugin-request": "^1.0.0"
|
"@webank/fes-plugin-request": "^1.0.0",
|
||||||
|
"@webank/fes-plugin-access": "^1.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,5 @@ export default {
|
|||||||
cjs: { type: 'babel', lazy: true },
|
cjs: { type: 'babel', lazy: true },
|
||||||
esm: { type: 'rollup' },
|
esm: { type: 'rollup' },
|
||||||
disableTypeCheck: false,
|
disableTypeCheck: false,
|
||||||
extraExternals: ['@@/core/fesExports'],
|
extraExternals: ['@@/core/exports'],
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user