Files
fe_service_ebtp_frontend/src/layouts/BasicLayout.tsx

249 lines
6.5 KiB
TypeScript
Raw Normal View History

2020-12-23 11:14:35 +08:00
import ProLayout, {
MenuDataItem,
BasicLayoutProps as ProLayoutProps,
Settings,
} from '@ant-design/pro-layout';
2022-03-10 14:24:13 +08:00
import React, { useEffect, useMemo, useRef, useState } from 'react';
2020-12-23 11:14:35 +08:00
import { Link, useIntl, connect, Dispatch, history } from 'umi';
2022-03-10 14:24:13 +08:00
import { Result, Button, message } from 'antd';
2020-12-23 11:14:35 +08:00
import Authorized from '@/utils/Authorized';
import RightContent from '@/components/GlobalHeader/RightContent';
import { ConnectState } from '@/models/connect';
import { getMatchMenu } from '@umijs/route-utils';
2022-03-10 14:24:13 +08:00
import { getMenu } from './services'
2020-12-23 11:14:35 +08:00
import logo from '../assets/logo.svg';
2022-03-10 14:24:13 +08:00
import { getSessionRoleData } from '@/utils/session';
2021-01-16 11:29:42 +08:00
2022-03-10 14:24:13 +08:00
import {
HomeOutlined,
DesktopOutlined,
NotificationOutlined,
BookOutlined,
ProfileOutlined,
CommentOutlined,
MacCommandOutlined,
ProjectOutlined,
PartitionOutlined,
PayCircleOutlined,
ContactsOutlined,
WalletOutlined,
AppstoreOutlined,
ShopOutlined,
} from '@ant-design/icons';
import { getAuthority } from '@/utils/authority';
import { JumpToOutside } from '@/utils/CommonUtils';
2020-12-23 11:14:35 +08:00
2022-03-10 14:24:13 +08:00
const menuImgList = [
{
key: 'HomeOutlined',
value: <HomeOutlined />
},
{
key: 'ContactsOutlined',
value: <ContactsOutlined />
},
{
key: 'DesktopOutlined',
value: <DesktopOutlined />
},
{
key: 'NotificationOutlined',
value: <NotificationOutlined />
},
{
key: 'BookOutlined',
value: <BookOutlined />
},
{
key: 'ProfileOutlined',
value: <ProfileOutlined />
},
{
key: 'CommentOutlined',
value: <CommentOutlined />
},
{
key: 'MacCommandOutlined',
value: <MacCommandOutlined />
},
{
key: 'ProjectOutlined',
value: <ProjectOutlined />
},
{
key: 'PartitionOutlined',
value: <PartitionOutlined />
},
{
key: 'PayCircleOutlined',
value: <PayCircleOutlined />
},
{
key: 'WalletOutlined',
value: <WalletOutlined />
},
{
key: 'AppstoreOutlined',
value: <AppstoreOutlined />
},
{
key: 'ShopOutlined',
value: <ShopOutlined />
},
]
2020-12-23 11:14:35 +08:00
const noMatch = (
<Result
status={403}
title="403"
2022-03-10 14:24:13 +08:00
subTitle="对不起,您没有访问本页面的权限。"
2020-12-23 11:14:35 +08:00
extra={
<Button type="primary">
2022-03-10 14:24:13 +08:00
{/* <Link to="/user/login">Go Login</Link> */}
<Link to="/usercooper/login"></Link>
2020-12-23 11:14:35 +08:00
</Button>
}
/>
);
2021-01-16 11:29:42 +08:00
2020-12-23 11:14:35 +08:00
export interface BasicLayoutProps extends ProLayoutProps {
breadcrumbNameMap: {
[path: string]: MenuDataItem;
};
route: ProLayoutProps['route'] & {
authority: string[];
};
settings: Settings;
dispatch: Dispatch;
}
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
breadcrumbNameMap: {
[path: string]: MenuDataItem;
};
};
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
menuList.map((item) => {
const localItem = {
...item,
children: item.children ? menuDataRender(item.children) : undefined,
};
return Authorized.check(item.authority, localItem, null) as MenuDataItem;
});
const BasicLayout: React.FC<BasicLayoutProps> = (props) => {
const {
dispatch,
children,
settings,
location = {
pathname: '/',
},
} = props;
2022-03-10 14:24:13 +08:00
const [menuData, setMenuData] = React.useState<any>();
const [menuShow, setmenuShow] = React.useState<any>(false);
2020-12-23 11:14:35 +08:00
const menuDataRef = useRef<MenuDataItem[]>([]);
useEffect(() => {
2022-03-10 14:24:13 +08:00
if (getSessionRoleData()?.roleId) {
let params = {
roleIdList: [getSessionRoleData()?.roleId]
}
getMenu(params).then(res => {
if (res.code == 1) {
setMenuData(res.data || [])
setmenuShow(true)
} else {
message.error("数据错误请联系管理员")
}
})
2020-12-23 11:14:35 +08:00
}
}, []);
2022-03-10 14:24:13 +08:00
function menuICon(menuData: any) {
if (menuData != undefined && menuData != null && menuData.length > 0) {
let data: any[] = [...menuData]
let arr3 = data.map((item: any) => {
menuImgList.forEach((item3: any) => {
if (item.icon == item3.key) {
item.icon = item3.value
}
})
return item
})
return arr3
} else {
return []
}
}
2020-12-23 11:14:35 +08:00
const handleMenuCollapse = (payload: boolean): void => {
if (dispatch) {
dispatch({
type: 'global/changeLayoutCollapsed',
payload,
});
}
};
const authorized = useMemo(
() =>
getMatchMenu(location.pathname || '/', menuDataRef.current).pop() || {
authority: undefined,
},
[location.pathname],
);
const { formatMessage } = useIntl();
2022-03-10 14:24:13 +08:00
return menuShow ? (<ProLayout
menuDataRender={() => menuICon(menuData)}
logo={logo}
// formatMessage={formatMessage}
onCollapse={handleMenuCollapse}
onMenuHeaderClick={() => history.push('/')}
menuItemRender={(menuItemProps, defaultDom) => {
if (menuItemProps.isUrl || !menuItemProps.path) {
return defaultDom;
}
if (menuItemProps.projectType != undefined) {
menuItemProps.path = menuItemProps.path + `?projectType=${menuItemProps.projectType}`;
}
if (menuItemProps.frame && menuItemProps.frame == 'N') {
2020-12-23 11:14:35 +08:00
return <Link to={menuItemProps.path}>{defaultDom}</Link>;
2022-03-10 14:24:13 +08:00
} else if (menuItemProps.frame && menuItemProps.frame == 'Y') {
return <Link onClick={() => window.open(menuItemProps.path)} to="#">{defaultDom}</Link>;
} else if (menuItemProps.frame && menuItemProps.frame == 'S') {
return <Link onClick={() => window.open(menuItemProps.path + '&mall3_token=' + sessionStorage.getItem('Authorization'))} to="#">{defaultDom}</Link>;
} else {
return <Link to={menuItemProps.path}>{defaultDom}</Link>;
}
}}
breadcrumbRender={(routers = []) => [
{
path: '/',
breadcrumbName: formatMessage({ id: 'menu.home' }),
},
...routers,
]}
itemRender={(route, params, routes, paths) => {
const first = routes.indexOf(route) === 0;
return first ? (
<Link to={paths.join('/')}>{route.breadcrumbName}</Link>
) : (
<span>{route.breadcrumbName}</span>
);
}}
rightContentRender={() => <RightContent />}
postMenuData={(menuData) => {
menuDataRef.current = menuData || [];
return menuData || [];
}}
{...props}
{...settings}
>
<Authorized authority={authorized!.authority} noMatch={noMatch}>
<div style={{ width: "100%" }}>
2020-12-23 11:14:35 +08:00
{children}
2022-03-10 14:24:13 +08:00
</div>
</Authorized>
</ProLayout>) : null
}
2020-12-23 11:14:35 +08:00
export default connect(({ global, settings }: ConnectState) => ({
collapsed: global.collapsed,
settings,
}))(BasicLayout);