BasicLayout.js 4.54 KB
Newer Older
jim's avatar
jim committed
1
import React from 'react';
2
import PropTypes from 'prop-types';
jim's avatar
jim committed
3
import { Layout } from 'antd';
4 5
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
jim's avatar
jim committed
6
import { Route, Redirect, Switch } from 'dva/router';
afc163's avatar
afc163 committed
7 8
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
偏右's avatar
偏右 committed
9
import SiderMenu from '../components/SiderMenu';
afc163's avatar
afc163 committed
10
import NotFound from '../routes/Exception/404';
ddcat1115's avatar
ddcat1115 committed
11
import { getRoutes } from '../utils/utils';
ddcat1115's avatar
ddcat1115 committed
12
import Authorized from '../utils/Authorized';
jim's avatar
jim committed
13
import Sidebar from '../components/Sidebar';
14
import logo from '../assets/logo.svg';
jim's avatar
jim committed
15 16
import Footer from './Footer';
import Header from './Header';
17

jim's avatar
jim committed
18
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
19 20
const { AuthorizedRoute } = Authorized;

jim's avatar
jim committed
21
const RightSidebar = connect(({ setting }) => ({ ...setting }))(Sidebar);
陈帅's avatar
陈帅 committed
22

afc163's avatar
afc163 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
const query = {
  'screen-xs': {
    maxWidth: 575,
  },
  'screen-sm': {
    minWidth: 576,
    maxWidth: 767,
  },
  'screen-md': {
    minWidth: 768,
    maxWidth: 991,
  },
  'screen-lg': {
    minWidth: 992,
    maxWidth: 1199,
  },
  'screen-xl': {
    minWidth: 1200,
  },
};
43 44
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
45 46
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
jiang's avatar
jiang committed
47
  };
48
  getChildContext() {
ddcat1115's avatar
ddcat1115 committed
49 50 51 52 53
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
54 55
  }
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
56
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
57 58
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
59 60 61
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - Ant Design Pro`;
    }
ddcat1115's avatar
ddcat1115 committed
62
    return title;
63
  }
64 65 66 67
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
68 69

    const redirect = urlParams.searchParams.get('redirect');
70
    // Remove the parameters in the url
jim's avatar
jim committed
71 72 73 74 75 76
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
      return '/dashboard/analysis';
    }
77
    return redirect;
jim's avatar
jim committed
78
  };
79 80 81 82 83
  handleMenuCollapse = (collapsed) => {
    this.props.dispatch({
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
84 85
  };
  changeSetting = (setting) => {
86
    this.props.dispatch({
jim's avatar
jim committed
87 88
      type: 'setting/changeSetting',
      payload: setting,
89 90
    });
  }
91
  render() {
偏右's avatar
偏右 committed
92
    const {
jim's avatar
jim committed
93 94 95 96 97
      isMobile,
      redirectData,
      routerData,
      fixedHeader,
      match,
偏右's avatar
偏右 committed
98
    } = this.props;
jim's avatar
jim committed
99
    const isTop = this.props.layout === 'topmenu';
100
    const bashRedirect = this.getBashRedirect();
jim's avatar
jim committed
101
    const myRedirectData = redirectData || [];
afc163's avatar
afc163 committed
102 103
    const layout = (
      <Layout>
jim's avatar
jim committed
104
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
105 106 107
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
jim's avatar
jim committed
108
            theme={this.props.silderTheme}
jim's avatar
jim committed
109
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
110
            {...this.props}
jim's avatar
jim committed
111 112
          />
        )}
afc163's avatar
afc163 committed
113
        <Layout>
jim's avatar
jim committed
114 115 116 117 118 119 120 121 122 123 124 125
          <Header
            handleMenuCollapse={this.handleMenuCollapse}
            logo={logo}
            {...this.props}
          />
          <Content
            style={{
              margin: '24px 24px 0',
              height: '100%',
              paddingTop: fixedHeader ? 64 : 0,
            }}
          >
126
            <Switch>
jim's avatar
jim committed
127 128 129 130 131 132 133 134 135 136 137 138 139
              {myRedirectData.map(item => (
                <Redirect key={item.from} exact from={item.from} to={item.to} />
              ))}
              {getRoutes(match.path, routerData).map(item => (
                <AuthorizedRoute
                  key={item.key}
                  path={item.path}
                  component={item.component}
                  exact={item.exact}
                  authority={item.authority}
                  redirectPath="/exception/403"
                />
              ))}
140 141 142
              <Redirect exact from="/" to={bashRedirect} />
              <Route render={NotFound} />
            </Switch>
afc163's avatar
afc163 committed
143
          </Content>
jim's avatar
jim committed
144
          <Footer />
145
        </Layout>
afc163's avatar
afc163 committed
146 147 148 149 150 151
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
jim's avatar
jim committed
152 153 154 155 156 157
          {params => (
            <div className={classNames(params)}>
              {layout}
              <RightSidebar onChange={this.changeSetting} />
            </div>
          )}
afc163's avatar
afc163 committed
158
        </ContainerQuery>
159 160 161 162 163
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
164
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
165
  collapsed: global.collapsed,
jim's avatar
jim committed
166 167
  layout: setting.layout,
  ...setting,
168
}))(BasicLayout);