/** * 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 { ConnectProps, ConnectState, Dispatch } from '@/models/connect'; import ProLayout, { MenuDataItem, BasicLayoutProps as ProLayoutProps, Settings, SettingDrawer, } from '@ant-design/pro-layout'; import React, { useState } from 'react'; import Authorized from '@/utils/Authorized'; import Link from 'umi/link'; import router from 'umi/router'; import RightContent from '@/components/GlobalHeader/RightContent'; import { connect } from 'dva'; import { formatMessage } from 'umi-plugin-react/locale'; import { isAntDesignPro } from '@/utils/utils'; import { message } from 'antd'; import store from '@/utils/store'; import user from 'mock/user'; export interface BasicLayoutProps extends ProLayoutProps, Omit { breadcrumbNameMap: { [path: string]: MenuDataItem; }; settings: Settings; } export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & { breadcrumbNameMap: { [path: string]: MenuDataItem; }; }; /** * use Authorized check all menu item */ const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] => menuList.map(item => { // 遍历给所有的children加上了[] const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] }; // 检查菜单项是否有权限 return Authorized.check(item.authority, localItem, null) as MenuDataItem; }); // footer渲染 const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => { if (!isAntDesignPro()) { return defaultDom; } return ( <> {defaultDom}
netlify logo
); }; const loadInitData = (dispatch: Dispatch) => { Promise.all([dispatch({ type: 'public/getUserInfo' }), dispatch({ type: 'public/getUserInfo' })]); }; const BasicLayout: React.FC = props => { const { dispatch, children, settings } = props; /** * constructor */ const [loaded, setLoaded] = useState(false); useState(() => { if (dispatch) { // 查看当前用户是否在登录状态 dispatch({ type: 'user/checkUserLoginStatus', }).then(({ code, data = false }: { code: string; data: boolean }) => { setLoaded(true); if (code === 'sys.success') { // 登录成功 if (data) { loadInitData(dispatch); } else if (store.get('userId')) { store.set('token', ''); store.set('userId', ''); message.error('登录已过期,请重新登录!'); router.push('/user/login'); } else { // 此处应该跳转到404或者403页面为佳 // message.error("用户未登录,请先登录系统!"); router.push('/404'); } } }); // dispatch({ // type: 'settings/getSetting', // }); } }); /** * init variables */ const handleMenuCollapse = (payload: boolean): void => dispatch && dispatch({ type: 'global/changeLayoutCollapsed', payload, }); return ( loaded && (
( {defaultDom} )} breadcrumbRender={(routers = []) => [ { path: '/', breadcrumbName: formatMessage({ id: 'menu.home', defaultMessage: 'Home', }), }, ...routers, ]} footerRender={false} // 不显示footer menuDataRender={menuDataRender} // 渲染导航菜单列表 formatMessage={formatMessage} rightContentRender={rightProps => } {...props} {...settings} > {children} {/* 主题设置抽屉 */} dispatch({ type: 'settings/changeSetting', payload: config, }) } />
) ); }; export default connect(({ global, settings }: ConnectState) => ({ collapsed: global.collapsed, settings, }))(BasicLayout);