BasicLayout.jsx
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
* You can view component api by:
* https://github.com/ant-design/ant-design-pro-layout
*/
import ProLayout from '@ant-design/pro-layout';
import { Link, connect, useIntl, history } from 'umi';
import React from 'react';
import RightContent from '@/components/GlobalHeader/RightContent';
import AuthRouter from '@/components/Auth/AuthRouter';
import PageCommons from '@/components/PageCommons';
import YH from '@/assets/yh-logo.png';
import {
DesktopOutlined,
WarningOutlined,
SelectOutlined,
BarsOutlined,
SettingOutlined,
} from '@ant-design/icons';
import { StyledTitle } from '@/components/style';
import { codeMappingForResource } from '../../config/routerConfig';
const IconMap = {
screen: <DesktopOutlined />,
risk: <WarningOutlined />,
event: <SelectOutlined />,
data: <BarsOutlined />,
system: <SettingOutlined />,
};
const BasicLayout = props => {
const intl = useIntl();
const { formatMessage } = intl;
const {
dispatch,
children,
settings,
location: { pathname },
loading,
urlFileExport,
currentUser: { menus },
} = props;
const handleMenuCollapse = payload => {
if (dispatch) {
dispatch({
type: 'global/changeLayoutCollapsed',
payload,
});
}
};
// 重定向处理
// const isRedirect = pathname === '/';
// if (isRedirect && dataMenus.length) {
// history.replace({ pathname: dataMenus[0].path });
// };
// 菜单 loop
const loopMenuItem = menus => {
// console.log(menus, 'menus', codeMappingForResource);
return menus?.map(({ icon, children, ...item }) => ({
...(codeMappingForResource.get(item.resourceCode) || {}),
icon: icon && IconMap[icon],
routes: children && loopMenuItem(children),
}));
};
return (
<ProLayout
formatMessage={formatMessage}
menuHeaderRender={(logoDom, titleDom) => (
<Link to="/">
<img src={YH} />
<StyledTitle>{titleDom}</StyledTitle>
</Link>
)}
onCollapse={handleMenuCollapse}
menuItemRender={(menuItemProps, defaultDom) => {
if (menuItemProps.isUrl || menuItemProps.children || !menuItemProps.path) {
return defaultDom;
}
return (
<Link to={menuItemProps.path}>
{IconMap[menuItemProps.icons]}
{defaultDom}
</Link>
);
}}
subMenuItemRender={subProps => {
return (
<span className="ant-pro-menu-item">
{IconMap[subProps.icons]}
<span>{subProps.name}</span>
</span>
);
}}
menuDataRender={() => loopMenuItem(menus)}
rightContentRender={() => <RightContent />}
{...props}
{...settings}
>
{/* 路由权限 */}
<AuthRouter path={pathname}>{children}</AuthRouter>
<PageCommons loading={loading} urlFileExport={urlFileExport} dispatch={dispatch} />
</ProLayout>
);
};
export default connect(({ global, settings, user }) => ({
settings,
loading: global.loading,
collapsed: global.collapsed,
urlFileExport: global.urlFileExport,
currentUser: user.currentUser,
}))(BasicLayout);