Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): 插件接入支持 qiankun 模式 #11259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ui/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default {
'@umijs/plugins/dist/valtio',
'@umijs/plugins/dist/styled-components',
'@umijs/plugins/dist/react-query',
'@umijs/plugins/dist/qiankun',
],
publicPath: '/__umi_ui/entry/',
base: '/__umi_ui/entry',
Expand All @@ -28,4 +29,7 @@ export default {
'local:moon',
],
},
qiankun: {
master: {},
},
};
59 changes: 54 additions & 5 deletions ui/app.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PluginContainer } from '@/components/PluginContainer';
import { IAppData } from '@/hooks/useAppData';
import { globalCSS } from '@/utils/globalCSS';
import { createGlobalStyle } from 'umi';
import { createGlobalStyle, MicroApp } from 'umi';

export const styledComponents = {
GlobalStyle: createGlobalStyle`
Expand Down Expand Up @@ -46,6 +46,10 @@ body {
color: var(--text-color);
}

#root-master, .qiankun-micro-app-container, #__qiankun_microapp_wrapper_for_qiankun__ {
height: 100%;
}

a {
color: var(--text-color);
text-decoration: none;
Expand Down Expand Up @@ -73,9 +77,54 @@ export async function patchClientRoutes({ routes }) {
const uiMenusAdded = modules.map((module) => module.menus || []).flat();

routes[0].routes?.unshift(
...uiMenusAdded.map((menu: any) => ({
path: menu.path,
element: <PluginContainer url={menu.url} name={menu.name} />,
})),
...uiMenusAdded.map((menu) => {
const { renderType } = menu;

if (renderType === 'qiankun') {
return {
path: `${menu.path}/*`,
// 防止 fixed 定位影响到外部布局
element: (
<div style={{ transform: 'scale(1)', height: '100%' }}>
<MicroApp
name={menu.name}
base={`/__umi_ui/entry/${menu.path}`}
/>
</div>
),
};
}

return {
path: `${menu.path}/*`,
element: <PluginContainer url={menu.url} name={menu.name} />,
};
}),
);
}

export async function qiankun() {
const umiInfo = await fetch('/__umi/api/app-data').then(
(res) => res.json() as Promise<IAppData>,
);

const modules = umiInfo?.ui?.modules || [];

// 提供给子应用 umi 的信息和自定义属性
const uiMenusQiankunAdded = modules
.map((module) => module.menus || [])
.flat()
.filter((module) => module.renderType === 'qiankun')
.map(({ name, meta, url }) => ({
name,
entry: url,
props: {
...meta,
umiInfo,
},
}));

return {
apps: uiMenusQiankunAdded,
};
}
7 changes: 6 additions & 1 deletion ui/components/PluginContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export const PluginContainer = (props: { url: string; name: string }) => {
return <iframe style={{ width: '100%', height: '100%' }} src={props.url} />;
return (
<iframe
style={{ width: '100%', height: '100%', border: 0 }}
src={props.url}
/>
);
};
2 changes: 2 additions & 0 deletions ui/hooks/useAppData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export interface IAppData {
url: string;
icon: string;
name: string;
renderType?: string; // 可以选择配置 qiankun,默认 iframe
meta?: any; // 配置乾坤后可以传递的props,比如 yuyanId?
}[];
[key: string]: any;
}[];
Expand Down
1 change: 0 additions & 1 deletion ui/layouts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Helmet, Outlet, styled } from 'umi';

const Wrapper = styled.div`
display: flex;
border: 1px solid var(--subtle-color);
height: 100%;
aside {
min-width: 200px;
Expand Down
2 changes: 1 addition & 1 deletion ui/models/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export const actions = {
const mode = state.mode === 'light' ? 'dark' : 'light';
state.mode = mode;
// 是否需要 localstorage 保存上次选择 mode
document.querySelector('html').classList.toggle('dark');
document.querySelector('html')!.classList.toggle('dark');
},
};