BasicLayout.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 { connect } from 'dva';
  7. import { ContainerQuery } from 'react-container-query';
  8. import classNames from 'classnames';
  9. import pathToRegexp from 'path-to-regexp';
  10. import Media from 'react-media';
  11. import { formatMessage } from 'umi/locale';
  12. import Authorized from '@/utils/Authorized';
  13. import logo from '../assets/logo.svg';
  14. import Footer from './Footer';
  15. import Header from './Header';
  16. import Context from './MenuContext';
  17. import Exception403 from '../pages/Exception/403';
  18. import PageLoading from '@/components/PageLoading';
  19. import SiderMenu from '@/components/SiderMenu';
  20. import { menu, title } from '../defaultSettings';
  21. import styles from './BasicLayout.less';
  22. // lazy load SettingDrawer
  23. const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer'));
  24. const { Content } = Layout;
  25. const query = {
  26. 'screen-xs': {
  27. maxWidth: 575,
  28. },
  29. 'screen-sm': {
  30. minWidth: 576,
  31. maxWidth: 767,
  32. },
  33. 'screen-md': {
  34. minWidth: 768,
  35. maxWidth: 991,
  36. },
  37. 'screen-lg': {
  38. minWidth: 992,
  39. maxWidth: 1199,
  40. },
  41. 'screen-xl': {
  42. minWidth: 1200,
  43. maxWidth: 1599,
  44. },
  45. 'screen-xxl': {
  46. minWidth: 1600,
  47. },
  48. };
  49. class BasicLayout extends React.Component {
  50. constructor(props) {
  51. super(props);
  52. this.getPageTitle = memoizeOne(this.getPageTitle);
  53. this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
  54. }
  55. componentDidMount() {
  56. const {
  57. dispatch,
  58. route: { routes, authority },
  59. } = this.props;
  60. dispatch({
  61. type: 'user/fetchCurrent',
  62. });
  63. dispatch({
  64. type: 'setting/getSetting',
  65. });
  66. dispatch({
  67. type: 'menu/getMenuData',
  68. payload: { routes, authority },
  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. getRouteAuthority = (pathname, routeData) => {
  83. const routes = routeData.slice(); // clone
  84. const getAuthority = (routeDatas, path) => {
  85. let authorities;
  86. routeDatas.forEach(route => {
  87. // check partial route
  88. if (pathToRegexp(`${route.path}(.*)`).test(path)) {
  89. if (route.authority) {
  90. authorities = route.authority;
  91. }
  92. // is exact route?
  93. if (!pathToRegexp(route.path).test(path) && route.routes) {
  94. authorities = getAuthority(route.routes, path);
  95. }
  96. }
  97. });
  98. return authorities;
  99. };
  100. return getAuthority(routes, pathname);
  101. };
  102. getPageTitle = (pathname, breadcrumbNameMap) => {
  103. const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
  104. if (!currRouterData) {
  105. return title;
  106. }
  107. const pageName = menu.disableLocal
  108. ? currRouterData.name
  109. : formatMessage({
  110. id: currRouterData.locale || currRouterData.name,
  111. defaultMessage: currRouterData.name,
  112. });
  113. return `${pageName} - ${title}`;
  114. };
  115. getLayoutStyle = () => {
  116. const { fixSiderbar, isMobile, collapsed, layout } = this.props;
  117. if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
  118. return {
  119. paddingLeft: collapsed ? '80px' : '256px',
  120. };
  121. }
  122. return null;
  123. };
  124. handleMenuCollapse = collapsed => {
  125. const { dispatch } = this.props;
  126. dispatch({
  127. type: 'global/changeLayoutCollapsed',
  128. payload: collapsed,
  129. });
  130. };
  131. renderSettingDrawer = () => {
  132. // Do not render SettingDrawer in production
  133. // unless it is deployed in preview.pro.ant.design as demo
  134. if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
  135. return null;
  136. }
  137. return <SettingDrawer />;
  138. };
  139. render() {
  140. const {
  141. navTheme,
  142. layout: PropsLayout,
  143. children,
  144. location: { pathname },
  145. isMobile,
  146. menuData,
  147. breadcrumbNameMap,
  148. route: { routes },
  149. fixedHeader,
  150. } = this.props;
  151. const isTop = PropsLayout === 'topmenu';
  152. const routerConfig = this.getRouteAuthority(pathname, routes);
  153. const contentStyle = !fixedHeader ? { paddingTop: 0 } : {};
  154. const layout = (
  155. <Layout>
  156. {isTop && !isMobile ? null : (
  157. <SiderMenu
  158. logo={logo}
  159. theme={navTheme}
  160. onCollapse={this.handleMenuCollapse}
  161. menuData={menuData}
  162. isMobile={isMobile}
  163. {...this.props}
  164. />
  165. )}
  166. <Layout
  167. style={{
  168. ...this.getLayoutStyle(),
  169. minHeight: '100vh',
  170. }}
  171. >
  172. <Header
  173. menuData={menuData}
  174. handleMenuCollapse={this.handleMenuCollapse}
  175. logo={logo}
  176. isMobile={isMobile}
  177. {...this.props}
  178. />
  179. <Content className={styles.content} style={contentStyle}>
  180. <Authorized authority={routerConfig} noMatch={<Exception403 />}>
  181. {children}
  182. </Authorized>
  183. </Content>
  184. <Footer />
  185. </Layout>
  186. </Layout>
  187. );
  188. return (
  189. <React.Fragment>
  190. <DocumentTitle title={this.getPageTitle(pathname, breadcrumbNameMap)}>
  191. <ContainerQuery query={query}>
  192. {params => (
  193. <Context.Provider value={this.getContext()}>
  194. <div className={classNames(params)}>{layout}</div>
  195. </Context.Provider>
  196. )}
  197. </ContainerQuery>
  198. </DocumentTitle>
  199. <Suspense fallback={<PageLoading />}>{this.renderSettingDrawer()}</Suspense>
  200. </React.Fragment>
  201. );
  202. }
  203. }
  204. export default connect(({ global, setting, menu: menuModel }) => ({
  205. collapsed: global.collapsed,
  206. layout: setting.layout,
  207. menuData: menuModel.menuData,
  208. breadcrumbNameMap: menuModel.breadcrumbNameMap,
  209. ...setting,
  210. }))(props => (
  211. <Media query="(max-width: 599px)">
  212. {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  213. </Media>
  214. ));