BasicLayout.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import React, { Fragment } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Layout, Icon, message } from 'antd';
  4. import DocumentTitle from 'react-document-title';
  5. import { connect } from 'dva';
  6. import { Route, Redirect, Switch, routerRedux } from 'dva/router';
  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 GlobalHeader from '../components/GlobalHeader';
  12. import GlobalFooter from '../components/GlobalFooter';
  13. import SiderMenu from '../components/SiderMenu';
  14. import NotFound from '../routes/Exception/404';
  15. import { getRoutes } from '../utils/utils';
  16. import Authorized from '../utils/Authorized';
  17. import { getMenuData } from '../common/menu';
  18. import logo from '../assets/logo.svg';
  19. const { Content, Header, Footer } = Layout;
  20. const { AuthorizedRoute, check } = Authorized;
  21. /**
  22. * 根据菜单取得重定向地址.
  23. */
  24. const redirectData = [];
  25. const getRedirect = item => {
  26. if (item && item.children) {
  27. if (item.children[0] && item.children[0].path) {
  28. redirectData.push({
  29. from: `${item.path}`,
  30. to: `${item.children[0].path}`,
  31. });
  32. item.children.forEach(children => {
  33. getRedirect(children);
  34. });
  35. }
  36. }
  37. };
  38. getMenuData().forEach(getRedirect);
  39. /**
  40. * 获取面包屑映射
  41. * @param {Object} menuData 菜单配置
  42. * @param {Object} routerData 路由配置
  43. */
  44. const getBreadcrumbNameMap = (menuData, routerData) => {
  45. const result = {};
  46. const childResult = {};
  47. for (const i of menuData) {
  48. if (!routerData[i.path]) {
  49. result[i.path] = i;
  50. }
  51. if (i.children) {
  52. Object.assign(childResult, getBreadcrumbNameMap(i.children, routerData));
  53. }
  54. }
  55. return Object.assign({}, routerData, result, childResult);
  56. };
  57. const query = {
  58. 'screen-xs': {
  59. maxWidth: 575,
  60. },
  61. 'screen-sm': {
  62. minWidth: 576,
  63. maxWidth: 767,
  64. },
  65. 'screen-md': {
  66. minWidth: 768,
  67. maxWidth: 991,
  68. },
  69. 'screen-lg': {
  70. minWidth: 992,
  71. maxWidth: 1199,
  72. },
  73. 'screen-xl': {
  74. minWidth: 1200,
  75. maxWidth: 1599,
  76. },
  77. 'screen-xxl': {
  78. minWidth: 1600,
  79. },
  80. };
  81. let isMobile;
  82. enquireScreen(b => {
  83. isMobile = b;
  84. });
  85. class BasicLayout extends React.PureComponent {
  86. static childContextTypes = {
  87. location: PropTypes.object,
  88. breadcrumbNameMap: PropTypes.object,
  89. };
  90. state = {
  91. isMobile,
  92. };
  93. getChildContext() {
  94. const { location, routerData } = this.props;
  95. return {
  96. location,
  97. breadcrumbNameMap: getBreadcrumbNameMap(getMenuData(), routerData),
  98. };
  99. }
  100. componentDidMount() {
  101. this.enquireHandler = enquireScreen(mobile => {
  102. this.setState({
  103. isMobile: mobile,
  104. });
  105. });
  106. const { dispatch } = this.props;
  107. dispatch({
  108. type: 'user/fetchCurrent',
  109. });
  110. }
  111. componentWillUnmount() {
  112. unenquireScreen(this.enquireHandler);
  113. }
  114. getPageTitle() {
  115. const { routerData, location } = this.props;
  116. const { pathname } = location;
  117. let title = 'Ant Design Pro';
  118. let currRouterData = null;
  119. // match params path
  120. Object.keys(routerData).forEach(key => {
  121. if (pathToRegexp(key).test(pathname)) {
  122. currRouterData = routerData[key];
  123. }
  124. });
  125. if (currRouterData && currRouterData.name) {
  126. title = `${currRouterData.name} - Ant Design Pro`;
  127. }
  128. return title;
  129. }
  130. getBaseRedirect = () => {
  131. // According to the url parameter to redirect
  132. // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
  133. const urlParams = new URL(window.location.href);
  134. const redirect = urlParams.searchParams.get('redirect');
  135. // Remove the parameters in the url
  136. if (redirect) {
  137. urlParams.searchParams.delete('redirect');
  138. window.history.replaceState(null, 'redirect', urlParams.href);
  139. } else {
  140. const { routerData } = this.props;
  141. // get the first authorized route path in routerData
  142. const authorizedPath = Object.keys(routerData).find(
  143. item => check(routerData[item].authority, item) && item !== '/'
  144. );
  145. return authorizedPath;
  146. }
  147. return redirect;
  148. };
  149. handleMenuCollapse = collapsed => {
  150. const { dispatch } = this.props;
  151. dispatch({
  152. type: 'global/changeLayoutCollapsed',
  153. payload: collapsed,
  154. });
  155. };
  156. handleNoticeClear = type => {
  157. message.success(`清空了${type}`);
  158. const { dispatch } = this.props;
  159. dispatch({
  160. type: 'global/clearNotices',
  161. payload: type,
  162. });
  163. };
  164. handleMenuClick = ({ key }) => {
  165. const { dispatch } = this.props;
  166. if (key === 'triggerError') {
  167. dispatch(routerRedux.push('/exception/trigger'));
  168. return;
  169. }
  170. if (key === 'logout') {
  171. dispatch({
  172. type: 'login/logout',
  173. });
  174. }
  175. };
  176. handleNoticeVisibleChange = visible => {
  177. const { dispatch } = this.props;
  178. if (visible) {
  179. dispatch({
  180. type: 'global/fetchNotices',
  181. });
  182. }
  183. };
  184. render() {
  185. const {
  186. currentUser,
  187. collapsed,
  188. fetchingNotices,
  189. notices,
  190. routerData,
  191. match,
  192. location,
  193. } = this.props;
  194. const { isMobile: mb } = this.state;
  195. const bashRedirect = this.getBaseRedirect();
  196. const layout = (
  197. <Layout>
  198. <SiderMenu
  199. logo={logo}
  200. // 不带Authorized参数的情况下如果没有权限,会强制跳到403界面
  201. // If you do not have the Authorized parameter
  202. // you will be forced to jump to the 403 interface without permission
  203. Authorized={Authorized}
  204. menuData={getMenuData()}
  205. collapsed={collapsed}
  206. location={location}
  207. isMobile={mb}
  208. onCollapse={this.handleMenuCollapse}
  209. />
  210. <Layout>
  211. <Header style={{ padding: 0 }}>
  212. <GlobalHeader
  213. logo={logo}
  214. currentUser={currentUser}
  215. fetchingNotices={fetchingNotices}
  216. notices={notices}
  217. collapsed={collapsed}
  218. isMobile={mb}
  219. onNoticeClear={this.handleNoticeClear}
  220. onCollapse={this.handleMenuCollapse}
  221. onMenuClick={this.handleMenuClick}
  222. onNoticeVisibleChange={this.handleNoticeVisibleChange}
  223. />
  224. </Header>
  225. <Content style={{ margin: '24px 24px 0', height: '100%' }}>
  226. <Switch>
  227. {redirectData.map(item => (
  228. <Redirect key={item.from} exact from={item.from} to={item.to} />
  229. ))}
  230. {getRoutes(match.path, routerData).map(item => (
  231. <AuthorizedRoute
  232. key={item.key}
  233. path={item.path}
  234. component={item.component}
  235. exact={item.exact}
  236. authority={item.authority}
  237. redirectPath="/exception/403"
  238. />
  239. ))}
  240. <Redirect exact from="/" to={bashRedirect} />
  241. <Route render={NotFound} />
  242. </Switch>
  243. </Content>
  244. <Footer style={{ padding: 0 }}>
  245. <GlobalFooter
  246. links={[
  247. {
  248. key: 'Pro 首页',
  249. title: 'Pro 首页',
  250. href: 'http://pro.ant.design',
  251. blankTarget: true,
  252. },
  253. {
  254. key: 'github',
  255. title: <Icon type="github" />,
  256. href: 'https://github.com/ant-design/ant-design-pro',
  257. blankTarget: true,
  258. },
  259. {
  260. key: 'Ant Design',
  261. title: 'Ant Design',
  262. href: 'http://ant.design',
  263. blankTarget: true,
  264. },
  265. ]}
  266. copyright={
  267. <Fragment>
  268. Copyright <Icon type="copyright" /> 2018 蚂蚁金服体验技术部出品
  269. </Fragment>
  270. }
  271. />
  272. </Footer>
  273. </Layout>
  274. </Layout>
  275. );
  276. return (
  277. <DocumentTitle title={this.getPageTitle()}>
  278. <ContainerQuery query={query}>
  279. {params => <div className={classNames(params)}>{layout}</div>}
  280. </ContainerQuery>
  281. </DocumentTitle>
  282. );
  283. }
  284. }
  285. export default connect(({ user, global = {}, loading }) => ({
  286. currentUser: user.currentUser,
  287. collapsed: global.collapsed,
  288. fetchingNotices: loading.effects['global/fetchNotices'],
  289. notices: global.notices,
  290. }))(BasicLayout);