BasicLayout.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import React, { Suspense } from 'react';
  2. import { Layout } from 'antd';
  3. import DocumentTitle from 'react-document-title';
  4. import isEqual from 'lodash/isEqual';
  5. import memoizeOne from 'memoize-one';
  6. import { ContainerQuery } from 'react-container-query';
  7. import classNames from 'classnames';
  8. import pathToRegexp from 'path-to-regexp';
  9. import { formatMessage } from 'umi/locale';
  10. import Authorized from '@/utils/Authorized';
  11. import Footer from './Footer';
  12. import Header from './Header';
  13. import Context from './MenuContext';
  14. import Exception403 from '../pages/Exception/403';
  15. import PageLoading from '@/components/PageLoading';
  16. import { AliveScope } from 'react-activation'
  17. import SiderMenu from '@/components/SiderMenu';
  18. import styles from './BasicLayout.less';
  19. import Custom from '@/pages/Custom';
  20. // lazy load SettingDrawer
  21. const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer'));
  22. const { Content } = Layout;
  23. const query = {
  24. 'screen-xs': {
  25. maxWidth: 575,
  26. },
  27. 'screen-sm': {
  28. minWidth: 576,
  29. maxWidth: 767,
  30. },
  31. 'screen-md': {
  32. minWidth: 768,
  33. maxWidth: 991,
  34. },
  35. 'screen-lg': {
  36. minWidth: 992,
  37. maxWidth: 1199,
  38. },
  39. 'screen-xl': {
  40. minWidth: 1200,
  41. maxWidth: 1599,
  42. },
  43. 'screen-xxl': {
  44. minWidth: 1600,
  45. },
  46. };
  47. class BasicLayout extends React.PureComponent {
  48. constructor(props) {
  49. super(props);
  50. this.getPageTitle = memoizeOne(this.getPageTitle);
  51. this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
  52. }
  53. componentDidMount() {
  54. const {
  55. dispatch,
  56. route: { routes, authority },
  57. } = this.props;
  58. dispatch({
  59. type: 'menu/getMenuData',
  60. payload: { routes, authority },
  61. });
  62. }
  63. componentDidUpdate(preProps) {
  64. // After changing to phone mode,
  65. // if collapsed is true, you need to click twice to display
  66. const { collapsed, isMobile } = this.props;
  67. if (isMobile && !preProps.isMobile && !collapsed) {
  68. this.handleMenuCollapse(false);
  69. }
  70. }
  71. getContext() {
  72. const { location, breadcrumbNameMap } = this.props;
  73. return {
  74. location,
  75. breadcrumbNameMap,
  76. };
  77. }
  78. matchParamsPath = (pathname, breadcrumbNameMap) => {
  79. const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
  80. return breadcrumbNameMap[pathKey];
  81. };
  82. getRouterAuthority = (pathname, routeData) => {
  83. let routeAuthority = ['noAuthority'];
  84. const getAuthority = (key, routes) => {
  85. routes.map(route => {
  86. if (route.path && pathToRegexp(route.path).test(key)) {
  87. routeAuthority = route.authority;
  88. } else if (route.routes) {
  89. routeAuthority = getAuthority(key, route.routes);
  90. }
  91. return route;
  92. });
  93. return routeAuthority;
  94. };
  95. return getAuthority(pathname, routeData);
  96. };
  97. getPageTitle = (pathname, breadcrumbNameMap) => {
  98. const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
  99. if (!currRouterData) {
  100. return '平台后台';
  101. }
  102. const pageName = formatMessage({
  103. id: currRouterData.locale || currRouterData.name,
  104. defaultMessage: currRouterData.name,
  105. });
  106. return `${pageName} - 平台后台`;
  107. };
  108. getLayoutStyle = () => {
  109. const { fixSiderbar, isMobile, collapsed, layout } = this.props;
  110. if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
  111. return {
  112. paddingLeft: collapsed ? '70px' : '150px',
  113. };
  114. }
  115. return null;
  116. };
  117. handleMenuCollapse = collapsed => {
  118. const { dispatch } = this.props;
  119. dispatch({
  120. type: 'global/changeLayoutCollapsed',
  121. payload: collapsed,
  122. });
  123. };
  124. renderSettingDrawer = () => {
  125. // Do not render SettingDrawer in production
  126. // unless it is deployed in preview.pro.ant.design as demo
  127. if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
  128. return null;
  129. }
  130. return <SettingDrawer />;
  131. };
  132. render() {
  133. const {
  134. navTheme,
  135. layout: PropsLayout,
  136. children,
  137. location: { pathname },
  138. isMobile,
  139. menuData,
  140. breadcrumbNameMap,
  141. route: { routes },
  142. fixedHeader,
  143. collapsed,
  144. } = this.props;
  145. const isTop = PropsLayout === 'topmenu';
  146. const routerConfig = this.getRouterAuthority(pathname, routes);
  147. const contentStyle = !fixedHeader ? { paddingTop: 0,marginLeft:collapsed?20:0 } : {marginLeft:collapsed?20:10};
  148. const layout = (
  149. <Layout>
  150. <SiderMenu
  151. theme={navTheme}
  152. onCollapse={this.handleMenuCollapse}
  153. menuData={menuData}
  154. isMobile={isMobile}
  155. {...this.props}
  156. />
  157. <Layout
  158. style={{
  159. ...this.getLayoutStyle(),
  160. minHeight: '100vh',
  161. }}
  162. >
  163. <Header
  164. menuData={menuData}
  165. handleMenuCollapse={this.handleMenuCollapse}
  166. isMobile={isMobile}
  167. {...this.props}
  168. />
  169. <Content className={styles.show_long_desc} style={contentStyle}>
  170. <AliveScope>
  171. <Authorized authority={routerConfig} noMatch={<Exception403 />}>
  172. {children}
  173. </Authorized>
  174. </AliveScope>
  175. </Content>
  176. <Footer />
  177. </Layout>
  178. </Layout>
  179. );
  180. return (
  181. <React.Fragment>
  182. <DocumentTitle title={this.getPageTitle(pathname, breadcrumbNameMap)}>
  183. <ContainerQuery query={query}>
  184. {params => (
  185. <Context.Provider value={this.getContext()}>
  186. <div className={classNames(params)}>{layout}</div>
  187. </Context.Provider>
  188. )}
  189. </ContainerQuery>
  190. </DocumentTitle>
  191. <Custom />
  192. </React.Fragment>
  193. );
  194. }
  195. }
  196. export default BasicLayout;