BasicLayout.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import React from 'react';
  2. import { Layout } from 'antd';
  3. import DocumentTitle from 'react-document-title';
  4. import { connect } from 'dva';
  5. import { Route, Redirect, Switch } from 'dva/router';
  6. import { ContainerQuery } from 'react-container-query';
  7. import classNames from 'classnames';
  8. import pathToRegexp from 'path-to-regexp';
  9. import SiderMenu from '../components/SiderMenu';
  10. import NotFound from '../routes/Exception/404';
  11. import { getRoutes } from '../utils/utils';
  12. import Authorized from '../utils/Authorized';
  13. import SettingDarwer from '../components/SettingDarwer';
  14. import logo from '../assets/logo.svg';
  15. import Footer from './Footer';
  16. import Header from './Header';
  17. import Context from './MenuContext';
  18. const { Content } = Layout;
  19. const { AuthorizedRoute, check } = Authorized;
  20. /**
  21. * 获取面包屑映射
  22. * @param {Object} menuData 菜单配置
  23. * @param {Object} routerData 路由配置
  24. */
  25. const getBreadcrumbNameMap = (menuData, routerData) => {
  26. const result = {};
  27. const childResult = {};
  28. for (const i of menuData) {
  29. if (!routerData[i.path]) {
  30. result[i.path] = i;
  31. }
  32. if (i.children) {
  33. Object.assign(childResult, getBreadcrumbNameMap(i.children, routerData));
  34. }
  35. }
  36. return Object.assign({}, routerData, result, childResult);
  37. };
  38. const query = {
  39. 'screen-xs': {
  40. maxWidth: 575,
  41. },
  42. 'screen-sm': {
  43. minWidth: 576,
  44. maxWidth: 767,
  45. },
  46. 'screen-md': {
  47. minWidth: 768,
  48. maxWidth: 991,
  49. },
  50. 'screen-lg': {
  51. minWidth: 992,
  52. maxWidth: 1199,
  53. },
  54. 'screen-xl': {
  55. minWidth: 1200,
  56. },
  57. };
  58. class BasicLayout extends React.PureComponent {
  59. getContext() {
  60. const { location, routerData, menuData } = this.props;
  61. return {
  62. location,
  63. breadcrumbNameMap: getBreadcrumbNameMap(menuData, routerData),
  64. };
  65. }
  66. getPageTitle() {
  67. const { routerData, location } = this.props;
  68. const { pathname } = location;
  69. let title = 'Ant Design Pro';
  70. let currRouterData = null;
  71. // match params path
  72. Object.keys(routerData).forEach(key => {
  73. if (pathToRegexp(key).test(pathname)) {
  74. currRouterData = routerData[key];
  75. }
  76. });
  77. if (currRouterData && currRouterData.name) {
  78. title = `${currRouterData.name} - Ant Design Pro`;
  79. }
  80. return title;
  81. }
  82. getLayoutStyle = () => {
  83. const { fixSiderbar } = this.props;
  84. if (fixSiderbar) {
  85. return {
  86. height: '100vh',
  87. overflow: 'auto',
  88. };
  89. }
  90. return null;
  91. };
  92. getContentStyle = () => {
  93. const { fixedHeader } = this.props;
  94. return {
  95. margin: '24px 24px 0',
  96. paddingTop: fixedHeader ? 64 : 0,
  97. };
  98. };
  99. getBashRedirect = () => {
  100. // According to the url parameter to redirect
  101. // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
  102. const urlParams = new URL(window.location.href);
  103. const redirect = urlParams.searchParams.get('redirect');
  104. // Remove the parameters in the url
  105. if (redirect) {
  106. urlParams.searchParams.delete('redirect');
  107. window.history.replaceState(null, 'redirect', urlParams.href);
  108. } else {
  109. const { routerData } = this.props;
  110. // get the first authorized route path in routerData
  111. const authorizedPath = Object.keys(routerData).find(
  112. item => check(routerData[item].authority, item) && item !== '/'
  113. );
  114. return authorizedPath;
  115. }
  116. return redirect;
  117. };
  118. handleMenuCollapse = collapsed => {
  119. this.props.dispatch({
  120. type: 'global/changeLayoutCollapsed',
  121. payload: collapsed,
  122. });
  123. };
  124. render() {
  125. const { isMobile, redirectData, routerData, match } = this.props;
  126. const isTop = this.props.layout === 'topmenu';
  127. const bashRedirect = this.getBashRedirect();
  128. const myRedirectData = redirectData || [];
  129. const layout = (
  130. <Layout>
  131. {isTop && !isMobile ? null : (
  132. <SiderMenu
  133. logo={logo}
  134. Authorized={Authorized}
  135. theme={this.props.silderTheme}
  136. onCollapse={this.handleMenuCollapse}
  137. {...this.props}
  138. />
  139. )}
  140. <Layout style={this.getLayoutStyle()}>
  141. <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
  142. <Content style={this.getContentStyle()}>
  143. <Switch>
  144. {myRedirectData.map(item => (
  145. <Redirect key={item.from} exact from={item.from} to={item.to} />
  146. ))}
  147. {getRoutes(match.path, routerData).map(item => (
  148. <AuthorizedRoute
  149. key={item.key}
  150. path={item.path}
  151. component={item.component}
  152. exact={item.exact}
  153. authority={item.authority}
  154. redirectPath="/exception/403"
  155. />
  156. ))}
  157. <Redirect exact from="/" to={bashRedirect} />
  158. <Route render={NotFound} />
  159. </Switch>
  160. </Content>
  161. <Footer />
  162. </Layout>
  163. </Layout>
  164. );
  165. return (
  166. <DocumentTitle title={this.getPageTitle()}>
  167. <ContainerQuery query={query}>
  168. {params => (
  169. <Context.Provider value={this.getContext()}>
  170. <div className={classNames(params)}>
  171. {layout}
  172. <SettingDarwer />
  173. </div>
  174. </Context.Provider>
  175. )}
  176. </ContainerQuery>
  177. </DocumentTitle>
  178. );
  179. }
  180. }
  181. export default connect(({ global, setting }) => ({
  182. collapsed: global.collapsed,
  183. layout: setting.layout,
  184. ...setting,
  185. }))(BasicLayout);