BasicLayout.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 SiderMenu from '../components/SiderMenu';
  9. import NotFound from '../routes/Exception/404';
  10. import { getRoutes } from '../utils/utils';
  11. import Authorized from '../utils/Authorized';
  12. import Sidebar from '../components/Sidebar';
  13. import logo from '../assets/logo.svg';
  14. import Footer from './Footer';
  15. import Header from './Header';
  16. import Context from './MenuContext';
  17. const { Content } = Layout;
  18. const { AuthorizedRoute, check } = Authorized;
  19. const RightSidebar = connect(({ setting }) => ({ ...setting }))(Sidebar);
  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. if (routerData[pathname] && routerData[pathname].name) {
  71. title = `${routerData[pathname].name} - Ant Design Pro`;
  72. }
  73. return title;
  74. }
  75. getBashRedirect = () => {
  76. // According to the url parameter to redirect
  77. // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
  78. const urlParams = new URL(window.location.href);
  79. const redirect = urlParams.searchParams.get('redirect');
  80. // Remove the parameters in the url
  81. if (redirect) {
  82. urlParams.searchParams.delete('redirect');
  83. window.history.replaceState(null, 'redirect', urlParams.href);
  84. } else {
  85. const { routerData } = this.props;
  86. // get the first authorized route path in routerData
  87. const authorizedPath = Object.keys(routerData).find(
  88. item => check(routerData[item].authority, item) && item !== '/'
  89. );
  90. return authorizedPath;
  91. }
  92. return redirect;
  93. };
  94. handleMenuCollapse = collapsed => {
  95. this.props.dispatch({
  96. type: 'global/changeLayoutCollapsed',
  97. payload: collapsed,
  98. });
  99. };
  100. changeSetting = setting => {
  101. this.props.dispatch({
  102. type: 'setting/changeSetting',
  103. payload: setting,
  104. });
  105. };
  106. render() {
  107. const { isMobile, redirectData, routerData, fixedHeader, match } = this.props;
  108. const isTop = this.props.layout === 'topmenu';
  109. const bashRedirect = this.getBashRedirect();
  110. const myRedirectData = redirectData || [];
  111. const layout = (
  112. <Layout>
  113. {isTop && !isMobile ? null : (
  114. <SiderMenu
  115. logo={logo}
  116. Authorized={Authorized}
  117. theme={this.props.silderTheme}
  118. onCollapse={this.handleMenuCollapse}
  119. {...this.props}
  120. />
  121. )}
  122. <Layout>
  123. <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
  124. <Content
  125. style={{
  126. margin: '24px 24px 0',
  127. height: '100%',
  128. paddingTop: fixedHeader ? 64 : 0,
  129. }}
  130. >
  131. <Switch>
  132. {myRedirectData.map(item => (
  133. <Redirect key={item.from} exact from={item.from} to={item.to} />
  134. ))}
  135. {getRoutes(match.path, routerData).map(item => (
  136. <AuthorizedRoute
  137. key={item.key}
  138. path={item.path}
  139. component={item.component}
  140. exact={item.exact}
  141. authority={item.authority}
  142. redirectPath="/exception/403"
  143. />
  144. ))}
  145. <Redirect exact from="/" to={bashRedirect} />
  146. <Route render={NotFound} />
  147. </Switch>
  148. </Content>
  149. <Footer />
  150. </Layout>
  151. </Layout>
  152. );
  153. return (
  154. <DocumentTitle title={this.getPageTitle()}>
  155. <ContainerQuery query={query}>
  156. {params => (
  157. <Context.Provider value={this.getContext()}>
  158. <div className={classNames(params)}>
  159. {layout}
  160. <RightSidebar onChange={this.changeSetting} />
  161. </div>
  162. </Context.Provider>
  163. )}
  164. </ContainerQuery>
  165. </DocumentTitle>
  166. );
  167. }
  168. }
  169. export default connect(({ global, setting }) => ({
  170. collapsed: global.collapsed,
  171. layout: setting.layout,
  172. ...setting,
  173. }))(BasicLayout);