BasicLayout.jsx 4.4 KB

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