BasicLayout.js 5.66 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
afc163's avatar
afc163 committed
132
          collapsed={collapsed}
偏右's avatar
偏右 committed
133
          location={location}
jiang's avatar
jiang committed
134
          isMobile={this.state.isMobile}
135
          onCollapse={this.handleMenuCollapse}
偏右's avatar
偏右 committed
136
        />
afc163's avatar
afc163 committed
137
        <Layout>
偏右's avatar
偏右 committed
138
          <GlobalHeader
139
            logo={logo}
偏右's avatar
偏右 committed
140 141 142 143
            currentUser={currentUser}
            fetchingNotices={fetchingNotices}
            notices={notices}
            collapsed={collapsed}
jiang's avatar
jiang committed
144
            isMobile={this.state.isMobile}
145 146 147 148
            onNoticeClear={this.handleNoticeClear}
            onCollapse={this.handleMenuCollapse}
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
偏右's avatar
偏右 committed
149
          />
afc163's avatar
afc163 committed
150
          <Content style={{ margin: '24px 24px 0', height: '100%' }}>
afc163's avatar
afc163 committed
151 152 153
            <div style={{ minHeight: 'calc(100vh - 260px)' }}>
              <Switch>
                {
陈帅's avatar
陈帅 committed
154 155
                  redirectData.map(item =>
                    <Redirect key={item.from} exact from={item.from} to={item.to} />
ddcat1115's avatar
ddcat1115 committed
156
                  )
afc163's avatar
afc163 committed
157
                }
陈帅's avatar
陈帅 committed
158 159 160 161 162 163 164 165 166 167
                {
                  getRoutes(match.path, routerData).map(item => (
                    <Route
                      key={item.key}
                      path={item.path}
                      component={item.component}
                      exact={item.exact}
                    />
                  ))
                }
afc163's avatar
afc163 committed
168
                <Redirect exact from="/" to="/dashboard/analysis" />
ddcat1115's avatar
ddcat1115 committed
169
                <Route render={NotFound} />
afc163's avatar
afc163 committed
170 171
              </Switch>
            </div>
afc163's avatar
afc163 committed
172 173
            <GlobalFooter
              links={[{
afc163's avatar
afc163 committed
174
                title: 'Pro 首页',
afc163's avatar
afc163 committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
                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>
193
        </Layout>
afc163's avatar
afc163 committed
194 195 196 197 198 199 200 201
      </Layout>
    );

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

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