BasicLayout.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import React 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 { enquireScreen, unenquireScreen } from 'enquire-js';
  11. import { formatMessage } from 'umi/locale';
  12. import SiderMenu from '@/components/SiderMenu';
  13. import Authorized from '@/utils/Authorized';
  14. import SettingDrawer from '@/components/SettingDrawer';
  15. import logo from '../assets/logo.svg';
  16. import Footer from './Footer';
  17. import Header from './Header';
  18. import Context from './MenuContext';
  19. import Exception403 from '../pages/Exception/403';
  20. const { Content } = Layout;
  21. // Conversion router to menu.
  22. function formatter(data, parentAuthority, parentName) {
  23. return data
  24. .map(item => {
  25. if (!item.name || !item.path) {
  26. return null;
  27. }
  28. let locale = 'menu';
  29. if (parentName) {
  30. locale = `${parentName}.${item.name}`;
  31. } else {
  32. locale = `menu.${item.name}`;
  33. }
  34. const result = {
  35. ...item,
  36. name: formatMessage({ id: locale, defaultMessage: item.name }),
  37. locale,
  38. authority: item.authority || parentAuthority,
  39. };
  40. if (item.routes) {
  41. const children = formatter(item.routes, item.authority, locale);
  42. // Reduce memory usage
  43. result.children = children;
  44. }
  45. delete result.routes;
  46. return result;
  47. })
  48. .filter(item => item);
  49. }
  50. const memoizeOneFormatter = memoizeOne(formatter, isEqual);
  51. const query = {
  52. 'screen-xs': {
  53. maxWidth: 575,
  54. },
  55. 'screen-sm': {
  56. minWidth: 576,
  57. maxWidth: 767,
  58. },
  59. 'screen-md': {
  60. minWidth: 768,
  61. maxWidth: 991,
  62. },
  63. 'screen-lg': {
  64. minWidth: 992,
  65. maxWidth: 1199,
  66. },
  67. 'screen-xl': {
  68. minWidth: 1200,
  69. maxWidth: 1599,
  70. },
  71. 'screen-xxl': {
  72. minWidth: 1600,
  73. },
  74. };
  75. class BasicLayout extends React.PureComponent {
  76. constructor(props) {
  77. super(props);
  78. this.getPageTitle = memoizeOne(this.getPageTitle);
  79. this.getBreadcrumbNameMap = memoizeOne(this.getBreadcrumbNameMap, isEqual);
  80. this.breadcrumbNameMap = this.getBreadcrumbNameMap();
  81. this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
  82. }
  83. state = {
  84. rendering: true,
  85. isMobile: false,
  86. menuData: this.getMenuData(),
  87. };
  88. componentDidMount() {
  89. const { dispatch } = this.props;
  90. dispatch({
  91. type: 'user/fetchCurrent',
  92. });
  93. dispatch({
  94. type: 'setting/getSetting',
  95. });
  96. this.renderRef = requestAnimationFrame(() => {
  97. this.setState({
  98. rendering: false,
  99. });
  100. });
  101. this.enquireHandler = enquireScreen(mobile => {
  102. const { isMobile } = this.state;
  103. if (isMobile !== mobile) {
  104. this.setState({
  105. isMobile: mobile,
  106. });
  107. }
  108. });
  109. }
  110. componentDidUpdate(preProps) {
  111. // After changing to phone mode,
  112. // if collapsed is true, you need to click twice to display
  113. this.breadcrumbNameMap = this.getBreadcrumbNameMap();
  114. const { isMobile } = this.state;
  115. const { collapsed } = this.props;
  116. if (isMobile && !preProps.isMobile && !collapsed) {
  117. this.handleMenuCollapse(false);
  118. }
  119. }
  120. componentWillUnmount() {
  121. cancelAnimationFrame(this.renderRef);
  122. unenquireScreen(this.enquireHandler);
  123. }
  124. getContext() {
  125. const { location } = this.props;
  126. return {
  127. location,
  128. breadcrumbNameMap: this.breadcrumbNameMap,
  129. };
  130. }
  131. getMenuData() {
  132. const {
  133. route: { routes, authority },
  134. } = this.props;
  135. return memoizeOneFormatter(routes, authority);
  136. }
  137. /**
  138. * 获取面包屑映射
  139. * @param {Object} menuData 菜单配置
  140. */
  141. getBreadcrumbNameMap() {
  142. const routerMap = {};
  143. const mergeMenuAndRouter = data => {
  144. data.forEach(menuItem => {
  145. if (menuItem.children) {
  146. mergeMenuAndRouter(menuItem.children);
  147. }
  148. // Reduce memory usage
  149. routerMap[menuItem.path] = menuItem;
  150. });
  151. };
  152. mergeMenuAndRouter(this.getMenuData());
  153. return routerMap;
  154. }
  155. matchParamsPath = pathname => {
  156. const pathKey = Object.keys(this.breadcrumbNameMap).find(key =>
  157. pathToRegexp(key).test(pathname)
  158. );
  159. return this.breadcrumbNameMap[pathKey];
  160. };
  161. getPageTitle = pathname => {
  162. const currRouterData = this.matchParamsPath(pathname);
  163. if (!currRouterData) {
  164. return 'Ant Design Pro';
  165. }
  166. const message = formatMessage({
  167. id: currRouterData.locale || currRouterData.name,
  168. defaultMessage: currRouterData.name,
  169. });
  170. return `${message} - Ant Design Pro`;
  171. };
  172. getLayoutStyle = () => {
  173. const { isMobile } = this.state;
  174. const { fixSiderbar, collapsed, layout } = this.props;
  175. if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
  176. return {
  177. paddingLeft: collapsed ? '80px' : '256px',
  178. };
  179. }
  180. return null;
  181. };
  182. getContentStyle = () => {
  183. const { fixedHeader } = this.props;
  184. return {
  185. margin: '24px 24px 0',
  186. paddingTop: fixedHeader ? 64 : 0,
  187. };
  188. };
  189. handleMenuCollapse = collapsed => {
  190. const { dispatch } = this.props;
  191. dispatch({
  192. type: 'global/changeLayoutCollapsed',
  193. payload: collapsed,
  194. });
  195. };
  196. renderSettingDrawer() {
  197. // Do not render SettingDrawer in production
  198. // unless it is deployed in preview.pro.ant.design as demo
  199. const { rendering } = this.state;
  200. if ((rendering || process.env.NODE_ENV === 'production') && APP_TYPE !== 'site') {
  201. return null;
  202. }
  203. return <SettingDrawer />;
  204. }
  205. render() {
  206. const {
  207. navTheme,
  208. layout: PropsLayout,
  209. children,
  210. location: { pathname },
  211. } = this.props;
  212. const { isMobile, menuData } = this.state;
  213. const isTop = PropsLayout === 'topmenu';
  214. const routerConfig = this.matchParamsPath(pathname);
  215. const layout = (
  216. <Layout>
  217. {isTop && !isMobile ? null : (
  218. <SiderMenu
  219. logo={logo}
  220. Authorized={Authorized}
  221. theme={navTheme}
  222. onCollapse={this.handleMenuCollapse}
  223. menuData={menuData}
  224. isMobile={isMobile}
  225. {...this.props}
  226. />
  227. )}
  228. <Layout
  229. style={{
  230. ...this.getLayoutStyle(),
  231. minHeight: '100vh',
  232. }}
  233. >
  234. <Header
  235. menuData={menuData}
  236. handleMenuCollapse={this.handleMenuCollapse}
  237. logo={logo}
  238. isMobile={isMobile}
  239. {...this.props}
  240. />
  241. <Content style={this.getContentStyle()}>
  242. <Authorized
  243. authority={routerConfig && routerConfig.authority}
  244. noMatch={<Exception403 />}
  245. >
  246. {children}
  247. </Authorized>
  248. </Content>
  249. <Footer />
  250. </Layout>
  251. </Layout>
  252. );
  253. return (
  254. <React.Fragment>
  255. <DocumentTitle title={this.getPageTitle(pathname)}>
  256. <ContainerQuery query={query}>
  257. {params => (
  258. <Context.Provider value={this.getContext()}>
  259. <div className={classNames(params)}>{layout}</div>
  260. </Context.Provider>
  261. )}
  262. </ContainerQuery>
  263. </DocumentTitle>
  264. {this.renderSettingDrawer()}
  265. </React.Fragment>
  266. );
  267. }
  268. }
  269. export default connect(({ global, setting }) => ({
  270. collapsed: global.collapsed,
  271. layout: setting.layout,
  272. ...setting,
  273. }))(BasicLayout);