BasicLayout.js 5.71 KB
Newer Older
1 2
import React from 'react';
import PropTypes from 'prop-types';
3
import { Layout, Icon, message } from 'antd';
4 5
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
偏右's avatar
偏右 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';
jiang's avatar
jiang committed
9
import { enquireScreen } from 'enquire-js';
偏右's avatar
偏右 committed
10
import GlobalHeader from '../components/GlobalHeader';
11
import GlobalFooter from '../components/GlobalFooter';
偏右's avatar
偏右 committed
12
import SiderMenu from '../components/SiderMenu';
afc163's avatar
afc163 committed
13
import NotFound from '../routes/Exception/404';
ddcat1115's avatar
ddcat1115 committed
14
import { getRoutes } from '../utils/utils';
陈帅's avatar
陈帅 committed
15
import { getMenuData } from '../common/menu';
16
import logo from '../assets/logo.svg';
17

陈帅's avatar
陈帅 committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * 根据菜单取得重定向地址.
 */
const redirectData = [];
const getRedirect = (item) => {
  if (item && item.children) {
    if (item.children[0] && item.children[0].path) {
      redirectData.push({
        from: `/${item.path}`,
        to: `/${item.children[0].path}`,
      });
      item.children.forEach((children) => {
        getRedirect(children);
      });
    }
  }
};
getMenuData().forEach(getRedirect);

const { Content } = Layout;
afc163's avatar
afc163 committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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,
  },
};

jiang's avatar
jiang committed
59 60 61 62 63
let isMobile;
enquireScreen((b) => {
  isMobile = b;
});

64 65
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
66 67
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
68
  }
jiang's avatar
jiang committed
69 70 71
  state = {
    isMobile,
  };
72
  getChildContext() {
ddcat1115's avatar
ddcat1115 committed
73 74 75 76 77
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
78
  }
jiang's avatar
jiang committed
79
  componentDidMount() {
afc163's avatar
afc163 committed
80
    enquireScreen((mobile) => {
jiang's avatar
jiang committed
81
      this.setState({
afc163's avatar
afc163 committed
82
        isMobile: mobile,
jiang's avatar
jiang committed
83 84
      });
    });
85 86 87
    this.props.dispatch({
      type: 'user/fetchCurrent',
    });
jiang's avatar
jiang committed
88
  }
89
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
90
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
91 92
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
93 94 95
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - Ant Design Pro`;
    }
ddcat1115's avatar
ddcat1115 committed
96
    return title;
97
  }
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  handleMenuCollapse = (collapsed) => {
    this.props.dispatch({
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
  }
  handleNoticeClear = (type) => {
    message.success(`清空了${type}`);
    this.props.dispatch({
      type: 'global/clearNotices',
      payload: type,
    });
  }
  handleMenuClick = ({ key }) => {
    if (key === 'logout') {
      this.props.dispatch({
        type: 'login/logout',
      });
    }
  }
  handleNoticeVisibleChange = (visible) => {
    if (visible) {
      this.props.dispatch({
        type: 'global/fetchNotices',
      });
    }
  }
125
  render() {
偏右's avatar
偏右 committed
126
    const {
127
      currentUser, collapsed, fetchingNotices, notices, routerData, match, location,
偏右's avatar
偏右 committed
128
    } = this.props;
afc163's avatar
afc163 committed
129 130
    const layout = (
      <Layout>
偏右's avatar
偏右 committed
131
        <SiderMenu
ddcat1115's avatar
ddcat1115 committed
132 133
          logo={logo}
          menuData={getMenuData()}
afc163's avatar
afc163 committed
134
          collapsed={collapsed}
偏右's avatar
偏右 committed
135
          location={location}
jiang's avatar
jiang committed
136
          isMobile={this.state.isMobile}
137
          onCollapse={this.handleMenuCollapse}
偏右's avatar
偏右 committed
138
        />
afc163's avatar
afc163 committed
139
        <Layout>
偏右's avatar
偏右 committed
140
          <GlobalHeader
141
            logo={logo}
偏右's avatar
偏右 committed
142 143 144 145
            currentUser={currentUser}
            fetchingNotices={fetchingNotices}
            notices={notices}
            collapsed={collapsed}
jiang's avatar
jiang committed
146
            isMobile={this.state.isMobile}
147 148 149 150
            onNoticeClear={this.handleNoticeClear}
            onCollapse={this.handleMenuCollapse}
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
偏右's avatar
偏右 committed
151
          />
afc163's avatar
afc163 committed
152
          <Content style={{ margin: '24px 24px 0', height: '100%' }}>
afc163's avatar
afc163 committed
153 154 155
            <div style={{ minHeight: 'calc(100vh - 260px)' }}>
              <Switch>
                {
陈帅's avatar
陈帅 committed
156 157
                  redirectData.map(item =>
                    <Redirect key={item.from} exact from={item.from} to={item.to} />
ddcat1115's avatar
ddcat1115 committed
158
                  )
afc163's avatar
afc163 committed
159
                }
陈帅's avatar
陈帅 committed
160 161 162 163 164 165 166 167 168 169
                {
                  getRoutes(match.path, routerData).map(item => (
                    <Route
                      key={item.key}
                      path={item.path}
                      component={item.component}
                      exact={item.exact}
                    />
                  ))
                }
afc163's avatar
afc163 committed
170
                <Redirect exact from="/" to="/dashboard/analysis" />
ddcat1115's avatar
ddcat1115 committed
171
                <Route render={NotFound} />
afc163's avatar
afc163 committed
172 173
              </Switch>
            </div>
afc163's avatar
afc163 committed
174 175
            <GlobalFooter
              links={[{
afc163's avatar
afc163 committed
176
                title: 'Pro 首页',
afc163's avatar
afc163 committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
                href: 'http://pro.ant.design',
                blankTarget: true,
              }, {
                title: 'GitHub',
                href: 'https://github.com/ant-design/ant-design-pro',
                blankTarget: true,
              }, {
                title: 'Ant Design',
                href: 'http://ant.design',
                blankTarget: true,
              }]}
              copyright={
                <div>
                  Copyright <Icon type="copyright" /> 2017 蚂蚁金服体验技术部出品
                </div>
              }
            />
          </Content>
195
        </Layout>
afc163's avatar
afc163 committed
196 197 198 199 200 201 202 203
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
          {params => <div className={classNames(params)}>{layout}</div>}
        </ContainerQuery>
204 205 206 207 208 209 210 211 212 213 214
      </DocumentTitle>
    );
  }
}

export default connect(state => ({
  currentUser: state.user.currentUser,
  collapsed: state.global.collapsed,
  fetchingNotices: state.global.fetchingNotices,
  notices: state.global.notices,
}))(BasicLayout);