import ProLayout, {
MenuDataItem,
BasicLayoutProps as ProLayoutProps,
Settings,
} from '@ant-design/pro-layout';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { Link, useIntl, connect, Dispatch, history } from 'umi';
import { Result, Button, message } from 'antd';
import Authorized from '@/utils/Authorized';
import RightContent from '@/components/GlobalHeader/RightContent';
import { ConnectState } from '@/models/connect';
import { getMatchMenu } from '@umijs/route-utils';
import { getMenu } from './services'
import logo from '../assets/logo.svg';
import { getSessionRoleData } from '@/utils/session';
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';
const menuImgList = [
{
key: 'HomeOutlined',
value:
},
{
key: 'ContactsOutlined',
value:
},
{
key: 'DesktopOutlined',
value:
},
{
key: 'NotificationOutlined',
value:
},
{
key: 'BookOutlined',
value:
},
{
key: 'ProfileOutlined',
value:
},
{
key: 'CommentOutlined',
value:
},
{
key: 'MacCommandOutlined',
value:
},
{
key: 'ProjectOutlined',
value:
},
{
key: 'PartitionOutlined',
value:
},
{
key: 'PayCircleOutlined',
value:
},
{
key: 'WalletOutlined',
value:
},
{
key: 'AppstoreOutlined',
value:
},
{
key: 'ShopOutlined',
value:
},
]
const noMatch = (
{/* Go Login */}
登录
}
/>
);
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 = (props) => {
const {
dispatch,
children,
settings,
location = {
pathname: '/',
},
} = props;
const [menuData, setMenuData] = React.useState();
const [menuShow, setmenuShow] = React.useState(false);
const menuDataRef = useRef([]);
useEffect(() => {
if (getSessionRoleData()?.roleId) {
let params = {
roleIdList: [getSessionRoleData()?.roleId]
}
getMenu(params).then(res => {
if (res.code == 1) {
setMenuData(res.data || [])
setmenuShow(true)
} else {
message.error("数据错误请联系管理员")
}
})
}
}, []);
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 []
}
}
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();
return menuShow ? ( 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') {
return {defaultDom};
} else if (menuItemProps.frame && menuItemProps.frame == 'Y') {
return window.open(menuItemProps.path)} to="#">{defaultDom};
} else if (menuItemProps.frame && menuItemProps.frame == 'S') {
return window.open(menuItemProps.path + '&mall3_token=' + sessionStorage.getItem('Authorization'))} to="#">{defaultDom};
} else {
return {defaultDom};
}
}}
breadcrumbRender={(routers = []) => [
{
path: '/',
breadcrumbName: formatMessage({ id: 'menu.home' }),
},
...routers,
]}
itemRender={(route, params, routes, paths) => {
const first = routes.indexOf(route) === 0;
return first ? (
{route.breadcrumbName}
) : (
{route.breadcrumbName}
);
}}
rightContentRender={() => }
postMenuData={(menuData) => {
menuDataRef.current = menuData || [];
return menuData || [];
}}
{...props}
{...settings}
>
{children}
) : null
}
export default connect(({ global, settings }: ConnectState) => ({
collapsed: global.collapsed,
settings,
}))(BasicLayout);