BasicLayout.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 from '@ant-design/pro-layout'
  7. import React, { useEffect, useMemo, useRef } from 'react'
  8. import { Link, useIntl, connect, history } from 'umi'
  9. import { Result, Button } from 'antd'
  10. import Authorized from '@/utils/Authorized'
  11. import RightContent from '@/components/GlobalHeader/RightContent'
  12. import { getMatchMenu } from '@umijs/route-utils'
  13. import logo from '../assets/logo.svg'
  14. import styles from './BasicLayout.less'
  15. const noMatch = (
  16. <Result
  17. status={403}
  18. title="403"
  19. subTitle="Sorry, you are not authorized to access this page."
  20. extra={
  21. <Button type="primary">
  22. <Link to="/user/login">Go Login</Link>
  23. </Button>
  24. }
  25. />
  26. )
  27. /**
  28. * use Authorized check all menu item
  29. */
  30. const menuDataRender = menuList =>
  31. menuList.map(item => {
  32. const localItem = {
  33. ...item,
  34. children: item.children ? menuDataRender(item.children) : undefined
  35. }
  36. return Authorized.check(item.authority, localItem, null)
  37. })
  38. // const defaultFooterDom = (
  39. // <DefaultFooter
  40. // copyright={`${new Date().getFullYear()} 蚂蚁集团体验技术部出品`}
  41. // links={[
  42. // {
  43. // key: 'Ant Design Pro',
  44. // title: 'Ant Design Pro',
  45. // href: 'https://pro.ant.design',
  46. // blankTarget: true,
  47. // },
  48. // {
  49. // key: 'github',
  50. // title: <GithubOutlined />,
  51. // href: 'https://github.com/ant-design/ant-design-pro',
  52. // blankTarget: true,
  53. // },
  54. // {
  55. // key: 'Ant Design',
  56. // title: 'Ant Design',
  57. // href: 'https://ant.design',
  58. // blankTarget: true,
  59. // },
  60. // ]}
  61. // />
  62. // );
  63. const BasicLayout = props => {
  64. const {
  65. dispatch,
  66. children,
  67. settings,
  68. location = {
  69. pathname: '/'
  70. }
  71. } = props
  72. const menuDataRef = useRef([])
  73. useEffect(() => {
  74. if (dispatch) {
  75. dispatch({
  76. type: 'user/fetchCurrent'
  77. })
  78. }
  79. }, [ dispatch ])
  80. /**
  81. * init variables
  82. */
  83. const handleMenuCollapse = payload => {
  84. if (dispatch) {
  85. dispatch({
  86. type: 'global/changeLayoutCollapsed',
  87. payload
  88. })
  89. }
  90. } // get children authority
  91. const authorized = useMemo(
  92. () =>
  93. getMatchMenu(location.pathname || '/', menuDataRef.current).pop() || {
  94. authority: undefined
  95. },
  96. [ location.pathname ]
  97. )
  98. const { formatMessage } = useIntl()
  99. return (
  100. <ProLayout
  101. className={styles.layoutBg}
  102. logo={logo}
  103. formatMessage={formatMessage}
  104. {...props}
  105. {...settings}
  106. onCollapse={handleMenuCollapse}
  107. onMenuHeaderClick={() => history.push('/')}
  108. menuItemRender={(menuItemProps, defaultDom) => {
  109. if (menuItemProps.isUrl || !menuItemProps.path) {
  110. return defaultDom
  111. }
  112. return <Link to={menuItemProps.path}>{defaultDom}</Link>
  113. }}
  114. breadcrumbRender={(routers = []) => [
  115. {
  116. path: '/',
  117. breadcrumbName: formatMessage({
  118. id: 'menu.home'
  119. })
  120. },
  121. ...routers
  122. ]}
  123. itemRender={(route, params, routes, paths) => {
  124. const first = routes.indexOf(route) === 0
  125. return first ? (
  126. <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
  127. ) : (
  128. <span>{route.breadcrumbName}</span>
  129. )
  130. }}
  131. // footerRender={() => defaultFooterDom}
  132. menuDataRender={menuDataRender}
  133. rightContentRender={() => <RightContent />}
  134. postMenuData={menuData => {
  135. menuDataRef.current = menuData || []
  136. return menuData || []
  137. }}>
  138. <Authorized authority={authorized.authority} noMatch={noMatch}>
  139. {children}
  140. </Authorized>
  141. </ProLayout>
  142. )
  143. }
  144. export default connect(({ global, settings }) => ({
  145. collapsed: global.collapsed,
  146. settings
  147. }))(BasicLayout)