mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
feat: route插件优化
This commit is contained in:
parent
9e0ae6ec82
commit
60f2cbb1f9
@ -86,7 +86,18 @@ export default (api) => {
|
|||||||
utils: { Mustache }
|
utils: { Mustache }
|
||||||
} = api;
|
} = api;
|
||||||
|
|
||||||
api.addRuntimePluginKey(() => 'access');
|
api.describe({
|
||||||
|
config: {
|
||||||
|
schema(joi) {
|
||||||
|
return joi.object({
|
||||||
|
roles: joi
|
||||||
|
.object()
|
||||||
|
});
|
||||||
|
},
|
||||||
|
default: {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const absoluteFilePath = join(namespace, 'core.js');
|
const absoluteFilePath = join(namespace, 'core.js');
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ function isPromise(obj) {
|
|||||||
const state = reactive({
|
const state = reactive({
|
||||||
roles: {{{REPLACE_ROLES}}},
|
roles: {{{REPLACE_ROLES}}},
|
||||||
currentRoleId: "",
|
currentRoleId: "",
|
||||||
currentAccessIds: ["/"],
|
currentAccessIds: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const rolePromiseList = [];
|
const rolePromiseList = [];
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { access } from './core';
|
import { access } from "./core";
|
||||||
|
|
||||||
export function onRouterCreated({ router }) {
|
export function onRouterCreated({ router }) {
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
|
@ -5,11 +5,10 @@ export default [
|
|||||||
require.resolve('./plugins/registerMethods'),
|
require.resolve('./plugins/registerMethods'),
|
||||||
|
|
||||||
// misc
|
// misc
|
||||||
require.resolve('./plugins/misc/routes'),
|
require.resolve('./plugins/misc/route'),
|
||||||
|
|
||||||
// generate files
|
// generate files
|
||||||
require.resolve('./plugins/generateFiles/core/plugin'),
|
require.resolve('./plugins/generateFiles/core/plugin'),
|
||||||
require.resolve('./plugins/generateFiles/core/routes'),
|
|
||||||
require.resolve('./plugins/generateFiles/core/exports/coreExports'),
|
require.resolve('./plugins/generateFiles/core/exports/coreExports'),
|
||||||
require.resolve('./plugins/generateFiles/core/exports/pluginExports'),
|
require.resolve('./plugins/generateFiles/core/exports/pluginExports'),
|
||||||
require.resolve('./plugins/generateFiles/fes'),
|
require.resolve('./plugins/generateFiles/fes'),
|
||||||
|
@ -18,6 +18,8 @@ export default function (api) {
|
|||||||
initialValue: [
|
initialValue: [
|
||||||
'modifyClientRenderOpts',
|
'modifyClientRenderOpts',
|
||||||
'rootContainer',
|
'rootContainer',
|
||||||
|
// app生成时触发
|
||||||
|
'onAppCreated',
|
||||||
// 渲染
|
// 渲染
|
||||||
'render',
|
'render',
|
||||||
// 修改路由
|
// 修改路由
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
import { readFileSync } from 'fs';
|
|
||||||
import { join } from 'path';
|
|
||||||
import { runtimePath } from '../../../../utils/constants';
|
|
||||||
|
|
||||||
export default function (api) {
|
|
||||||
const {
|
|
||||||
utils: { Mustache }
|
|
||||||
} = api;
|
|
||||||
|
|
||||||
const absoluteFilePath = 'core/routes.js';
|
|
||||||
|
|
||||||
api.onGenerateFiles(async () => {
|
|
||||||
const routesTpl = readFileSync(join(__dirname, 'routes.tpl'), 'utf-8');
|
|
||||||
const routes = await api.getRoutesJSON();
|
|
||||||
api.writeTmpFile({
|
|
||||||
path: absoluteFilePath,
|
|
||||||
content: Mustache.render(routesTpl, {
|
|
||||||
runtimePath,
|
|
||||||
routes,
|
|
||||||
config: api.config
|
|
||||||
})
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
api.addCoreExports(() => [
|
|
||||||
{
|
|
||||||
specifiers: ['router'],
|
|
||||||
source: absoluteFilePath
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
}
|
|
@ -7,7 +7,7 @@ import {
|
|||||||
import { plugin } from './core/plugin';
|
import { plugin } from './core/plugin';
|
||||||
import './core/pluginRegister';
|
import './core/pluginRegister';
|
||||||
import { ApplyPluginsType } from '{{{ runtimePath }}}';
|
import { ApplyPluginsType } from '{{{ runtimePath }}}';
|
||||||
import { createRouter, getRoutes } from './core/routes';
|
import { getRoutes } from './core/routes/routes';
|
||||||
{{{ imports }}}
|
{{{ imports }}}
|
||||||
|
|
||||||
{{{ entryCodeAhead }}}
|
{{{ entryCodeAhead }}}
|
||||||
@ -24,9 +24,13 @@ const renderClient = (opts = {}) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const router = createRouter();
|
|
||||||
const app = createApp(rootContainer);
|
const app = createApp(rootContainer);
|
||||||
app.use(router);
|
|
||||||
|
plugin.applyPlugins({
|
||||||
|
key: 'onAppCreated',
|
||||||
|
type: ApplyPluginsType.event,
|
||||||
|
args: { app },
|
||||||
|
});
|
||||||
|
|
||||||
// TODO other plugins install
|
// TODO other plugins install
|
||||||
if (rootElement) {
|
if (rootElement) {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { readdirSync, statSync } from 'fs';
|
import { readdirSync, statSync, readFileSync } from 'fs';
|
||||||
import {
|
import {
|
||||||
join, extname, posix, basename
|
join, extname, posix, basename
|
||||||
} from 'path';
|
} from 'path';
|
||||||
import { lodash } from '@umijs/utils';
|
import { lodash } from '@umijs/utils';
|
||||||
|
import { runtimePath } from '../../../utils/constants';
|
||||||
|
|
||||||
// pages
|
// pages
|
||||||
// ├── index.vue # 根路由页面 路径 /
|
// ├── index.vue # 根路由页面 路径 /
|
||||||
@ -230,4 +231,34 @@ export default function (api) {
|
|||||||
return getRoutesJSON({ routes, config: api.config });
|
return getRoutesJSON({ routes, config: api.config });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const {
|
||||||
|
utils: { Mustache }
|
||||||
|
} = api;
|
||||||
|
|
||||||
|
const namespace = 'core/routes';
|
||||||
|
|
||||||
|
const absCoreFilePath = join(namespace, 'routes.js');
|
||||||
|
|
||||||
|
const absRuntimeFilePath = join(namespace, 'runtime.js');
|
||||||
|
|
||||||
|
api.onGenerateFiles(async () => {
|
||||||
|
const routesTpl = readFileSync(join(__dirname, 'template/routes.tpl'), 'utf-8');
|
||||||
|
const routes = await api.getRoutesJSON();
|
||||||
|
api.writeTmpFile({
|
||||||
|
path: absCoreFilePath,
|
||||||
|
content: Mustache.render(routesTpl, {
|
||||||
|
runtimePath,
|
||||||
|
routes,
|
||||||
|
config: api.config
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
api.writeTmpFile({
|
||||||
|
path: absRuntimeFilePath,
|
||||||
|
content: readFileSync(join(__dirname, 'template/runtime.tpl'), 'utf-8')
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import { createRouter as createVueRouter, createWebHashHistory, ApplyPluginsType } from '{{{ runtimePath }}}';
|
import { createRouter as createVueRouter, createWebHashHistory, ApplyPluginsType } from '{{{ runtimePath }}}';
|
||||||
import { plugin } from './plugin';
|
import { plugin } from '@@/core/coreExports';
|
||||||
|
|
||||||
export function getRoutes() {
|
export function getRoutes() {
|
||||||
const routes = {{{ routes }}};
|
const routes = {{{ routes }}};
|
||||||
@ -31,5 +31,3 @@ export const createRouter = () => {
|
|||||||
|
|
||||||
return router;
|
return router;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { router }
|
|
@ -0,0 +1,6 @@
|
|||||||
|
import { createRouter } from "./routes";
|
||||||
|
|
||||||
|
export function onAppCreated({ app }) {
|
||||||
|
const router = createRouter();
|
||||||
|
app.use(router);
|
||||||
|
}
|
@ -2,4 +2,9 @@
|
|||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
access: {
|
||||||
|
roles: {
|
||||||
|
admin: ["/", "/onepiece"]
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
import { access } from '@webank/fes';
|
||||||
import PageLoading from '@/components/PageLoading.vue';
|
import PageLoading from '@/components/PageLoading.vue';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户信息比较慢的时候会展示一个 loading
|
* 获取用户信息比较慢的时候会展示一个 loading
|
||||||
*/
|
*/
|
||||||
@ -12,8 +12,10 @@ export const initialStateConfig = {
|
|||||||
* 设置页面初始化状态
|
* 设置页面初始化状态
|
||||||
*/
|
*/
|
||||||
export async function getInitialState() {
|
export async function getInitialState() {
|
||||||
|
const { setRole } = access;
|
||||||
const syncFunc = () => new Promise((resolve) => {
|
const syncFunc = () => new Promise((resolve) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
setRole('admin');
|
||||||
resolve({
|
resolve({
|
||||||
a: 1,
|
a: 1,
|
||||||
b: 2
|
b: 2
|
||||||
|
@ -8,28 +8,24 @@
|
|||||||
<script>
|
<script>
|
||||||
import { ref, onMounted } from 'vue';
|
import { ref, onMounted } from 'vue';
|
||||||
import {
|
import {
|
||||||
useAccess, access, router, useModel
|
useAccess, useModel, useRouter
|
||||||
} from '@webank/fes';
|
} from '@webank/fes';
|
||||||
|
|
||||||
const { setAccess } = access;
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const fes = ref('fes upgrade to vue3');
|
const fes = ref('fes upgrade to vue3');
|
||||||
const accessOnepicess = useAccess('/onepiece');
|
const accessOnepicess = useAccess('/onepiece');
|
||||||
const { initialState } = useModel('@@initialState');
|
const { initialState } = useModel('@@initialState');
|
||||||
|
const router = useRouter();
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
console.log(initialState);
|
|
||||||
console.log('mounted1!!!');
|
|
||||||
console.log(router);
|
console.log(router);
|
||||||
setAccess(new Promise((resolve) => {
|
console.log(initialState.value);
|
||||||
setTimeout(() => {
|
console.log('mounted1!!');
|
||||||
resolve(['/', '/onepiece']);
|
|
||||||
}, 3000);
|
|
||||||
}));
|
|
||||||
// router.push('/onepiece');
|
// router.push('/onepiece');
|
||||||
});
|
});
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
console.log('mounted2!!!');
|
console.log('mounted2!!');
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
fes,
|
fes,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user