BasicLayout.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
  3. * You can view component api by:
  4. * https://github.com/ant-design/ant-design-pro-layout
  5. */
  6. import ProLayout, {
  7. MenuDataItem,
  8. BasicLayoutProps as ProLayoutProps,
  9. Settings,
  10. DefaultFooter,
  11. } from '@ant-design/pro-layout';
  12. import React, { useEffect } from 'react';
  13. import Link from 'umi/link';
  14. import { Dispatch } from 'redux';
  15. import { connect } from 'dva';
  16. import { Icon, Result, Button } from 'antd';
  17. import { formatMessage } from 'umi-plugin-react/locale';
  18. import Authorized from '@/utils/Authorized';
  19. import RightContent from '@/components/GlobalHeader/RightContent';
  20. import { ConnectState } from '@/models/connect';
  21. import { isAntDesignPro, getAuthorityFromRouter } from '@/utils/utils';
  22. import logo from '../assets/logo.svg';
  23. const noMatch = (
  24. <Result
  25. status="403"
  26. title="403"
  27. subTitle="Sorry, you are not authorized to access this page."
  28. extra={
  29. <Button type="primary">
  30. <Link to="/user/login">Go Login</Link>
  31. </Button>
  32. }
  33. />
  34. );
  35. export interface BasicLayoutProps extends ProLayoutProps {
  36. breadcrumbNameMap: {
  37. [path: string]: MenuDataItem;
  38. };
  39. route: ProLayoutProps['route'] & {
  40. authority: string[];
  41. };
  42. settings: Settings;
  43. dispatch: Dispatch;
  44. }
  45. export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
  46. breadcrumbNameMap: {
  47. [path: string]: MenuDataItem;
  48. };
  49. };
  50. /**
  51. * use Authorized check all menu item
  52. */
  53. const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  54. menuList.map(item => {
  55. const localItem = {
  56. ...item,
  57. children: item.children ? menuDataRender(item.children) : [],
  58. };
  59. return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  60. });
  61. const defaultFooterDom = (
  62. <DefaultFooter
  63. copyright="2019 蚂蚁金服体验技术部出品"
  64. links={[
  65. {
  66. key: 'Ant Design Pro',
  67. title: 'Ant Design Pro',
  68. href: 'https://pro.ant.design',
  69. blankTarget: true,
  70. },
  71. {
  72. key: 'github',
  73. title: <Icon type="github" />,
  74. href: 'https://github.com/ant-design/ant-design-pro',
  75. blankTarget: true,
  76. },
  77. {
  78. key: 'Ant Design',
  79. title: 'Ant Design',
  80. href: 'https://ant.design',
  81. blankTarget: true,
  82. },
  83. ]}
  84. />
  85. );
  86. const footerRender: BasicLayoutProps['footerRender'] = () => {
  87. if (!isAntDesignPro()) {
  88. return defaultFooterDom;
  89. }
  90. return (
  91. <>
  92. {defaultFooterDom}
  93. <div
  94. style={{
  95. padding: '0px 24px 24px',
  96. textAlign: 'center',
  97. }}
  98. >
  99. <a href="https://www.netlify.com" target="_blank" rel="noopener noreferrer">
  100. <img
  101. src="https://www.netlify.com/img/global/badges/netlify-color-bg.svg"
  102. width="82px"
  103. alt="netlify logo"
  104. />
  105. </a>
  106. </div>
  107. </>
  108. );
  109. };
  110. const BasicLayout: React.FC<BasicLayoutProps> = props => {
  111. const { dispatch, children, settings, location = { pathname: '/' } } = props;
  112. /**
  113. * constructor
  114. */
  115. useEffect(() => {
  116. if (dispatch) {
  117. dispatch({
  118. type: 'user/fetchCurrent',
  119. });
  120. }
  121. }, []);
  122. /**
  123. * init variables
  124. */
  125. const handleMenuCollapse = (payload: boolean): void => {
  126. if (dispatch) {
  127. dispatch({
  128. type: 'global/changeLayoutCollapsed',
  129. payload,
  130. });
  131. }
  132. };
  133. // get children authority
  134. const authorized = getAuthorityFromRouter(props.route.routes, location.pathname || '/') || {
  135. authority: undefined,
  136. };
  137. return (
  138. <ProLayout
  139. logo={logo}
  140. menuHeaderRender={(logoDom, titleDom) => (
  141. <Link to="/">
  142. {logoDom}
  143. {titleDom}
  144. </Link>
  145. )}
  146. onCollapse={handleMenuCollapse}
  147. menuItemRender={(menuItemProps, defaultDom) => {
  148. if (menuItemProps.isUrl || menuItemProps.children) {
  149. return defaultDom;
  150. }
  151. return <Link to={menuItemProps.path}>{defaultDom}</Link>;
  152. }}
  153. breadcrumbRender={(routers = []) => [
  154. {
  155. path: '/',
  156. breadcrumbName: formatMessage({
  157. id: 'menu.home',
  158. defaultMessage: 'Home',
  159. }),
  160. },
  161. ...routers,
  162. ]}
  163. itemRender={(route, params, routes, paths) => {
  164. const first = routes.indexOf(route) === 0;
  165. return first ? (
  166. <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
  167. ) : (
  168. <span>{route.breadcrumbName}</span>
  169. );
  170. }}
  171. footerRender={footerRender}
  172. menuDataRender={menuDataRender}
  173. formatMessage={formatMessage}
  174. rightContentRender={() => <RightContent />}
  175. {...props}
  176. {...settings}
  177. >
  178. <Authorized authority={authorized!.authority} noMatch={noMatch}>
  179. {children}
  180. </Authorized>
  181. </ProLayout>
  182. );
  183. };
  184. export default connect(({ global, settings }: ConnectState) => ({
  185. collapsed: global.collapsed,
  186. settings,
  187. }))(BasicLayout);