diff --git a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl index 82896558..19b7da96 100644 --- a/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl +++ b/packages/plugin-qiankun/src/master/masterRuntimePlugin.ts.tpl @@ -12,9 +12,10 @@ import { defaultHistoryType } from './constants'; import { getMasterOptions, setMasterOptions } from './masterOptions'; // @ts-ignore import { deferred } from './qiankunDefer.js'; -import { App, HistoryType, MasterOptions, MicroAppRoute } from './types'; +import { App, HistoryType, MasterOptions, MicroAppRoute, Menu } from './types'; let microAppRuntimeRoutes: MicroAppRoute[]; +let microAppRuntimeMenus: Menu[]; async function getMasterRuntime() { const config = await plugin.applyPlugins({ @@ -106,8 +107,9 @@ export async function render(oldRender: typeof noop) { // 更新 master options setMasterOptions(masterOptions); - const { apps = [], routes, ...options } = getMasterOptions(); + const { apps = [], routes, menus, ...options } = getMasterOptions(); microAppRuntimeRoutes = routes; + microAppRuntimeMenus = menus; // 主应用相关的配置注册完毕后即可开启渲染 oldRender(); @@ -138,6 +140,38 @@ export function patchRoutes({ routes }: { routes: IRouteProps[] }) { } } +export function layout(initConfig) { + const loopMenusAndDoSomeThing = (menus, callback: (targetNode, key?: string) => void, key: string = '0'): void => { + for (let i = 0; i < menus.length; i++) { + if (Array.isArray(menus[i].children) && (menus[i].children).length > 0) { + loopMenusAndDoSomeThing((menus[i].children), callback, `${key}-${i}`); + } + callback(menus[i], `${key}-${i}`); + } + } + return { + ...initConfig, + patchMenus: function (menus, initialInfo) { + // 尝试使用站点菜单配置 + if (_.isArray(microAppRuntimeMenus) && microAppRuntimeMenus.length > 0) { + loopMenusAndDoSomeThing(microAppRuntimeMenus, (menuItem, key) => { + menuItem.key = key; // 给菜单项唯一key + if (menuItem.target) { // 处理外链 + menuItem.path = menuItem.target; + menuItem.isUrl = true; + } + }) + menus.length = 0; + menus.push(...microAppRuntimeMenus); + } + if (_.isFunction(initConfig.patchMenus)) { + initConfig.patchMenus(menus, initialInfo); + } + return menus; + }, + } +}; + function mergeExtraQiankunConfig(masterOptions: MasterOptions, extraQiankunConfig: BaseIConfig): BaseIConfig { function removeDuplicateApps(apps = new Array(), extraAppsNameSet?: Set): App[] { let _extraAppsNameSet = extraAppsNameSet || new Set( diff --git a/packages/plugin-qiankun/src/types.ts b/packages/plugin-qiankun/src/types.ts index ffec346f..392f498b 100644 --- a/packages/plugin-qiankun/src/types.ts +++ b/packages/plugin-qiankun/src/types.ts @@ -22,6 +22,14 @@ export type MicroAppRoute = { microApp: string; } & Record; +export type Menu = { + children?: Menu[], + name?: string, + icon?: string, + path?: string, + target?: string, +} & Record; + export type MasterOptions = { enable?: boolean; apps?: App[];