BasicLayout.js 6.97 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';
6
import { Route, Redirect, Switch, routerRedux } 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';
ddcat1115's avatar
ddcat1115 committed
15
import Authorized from '../utils/Authorized';
陈帅's avatar
陈帅 committed
16
import { getMenuData } from '../common/menu';
17
import logo from '../assets/logo.svg';
18

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

陈帅's avatar
陈帅 committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * 根据菜单取得重定向地址.
 */
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);

afc163's avatar
afc163 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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
62 63 64 65 66
let isMobile;
enquireScreen((b) => {
  isMobile = b;
});

67 68
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
69 70
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
71
  }
jiang's avatar
jiang committed
72 73 74
  state = {
    isMobile,
  };
75
  getChildContext() {
ddcat1115's avatar
ddcat1115 committed
76 77 78 79 80
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
81
  }
jiang's avatar
jiang committed
82
  componentDidMount() {
afc163's avatar
afc163 committed
83
    enquireScreen((mobile) => {
jiang's avatar
jiang committed
84
      this.setState({
afc163's avatar
afc163 committed
85
        isMobile: mobile,
jiang's avatar
jiang committed
86 87
      });
    });
88 89 90
    this.props.dispatch({
      type: 'user/fetchCurrent',
    });
jiang's avatar
jiang committed
91
  }
92
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
93
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
94 95
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
96 97 98
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - Ant Design Pro`;
    }
ddcat1115's avatar
ddcat1115 committed
99
    return title;
100
  }
101 102 103 104 105 106 107 108 109 110
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
    const redirect = urlParams.searchParams.get('redirect') || '/dashboard/analysis';
    // Remove the parameters in the url
    urlParams.searchParams.delete('redirect');
    window.history.pushState(null, 'redirect', urlParams.href);
    return redirect;
  }
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 }) => {
125 126 127 128
    if (key === 'triggerError') {
      this.props.dispatch(routerRedux.push('/exception/trigger'));
      return;
    }
129 130 131 132 133 134 135 136 137 138 139 140 141
    if (key === 'logout') {
      this.props.dispatch({
        type: 'login/logout',
      });
    }
  }
  handleNoticeVisibleChange = (visible) => {
    if (visible) {
      this.props.dispatch({
        type: 'global/fetchNotices',
      });
    }
  }
142
  render() {
偏右's avatar
偏右 committed
143
    const {
144
      currentUser, collapsed, fetchingNotices, notices, routerData, match, location,
偏右's avatar
偏右 committed
145
    } = this.props;
146
    const bashRedirect = this.getBashRedirect();
afc163's avatar
afc163 committed
147 148
    const layout = (
      <Layout>
偏右's avatar
偏右 committed
149
        <SiderMenu
ddcat1115's avatar
ddcat1115 committed
150
          logo={logo}
ddcat1115's avatar
ddcat1115 committed
151 152 153 154
          // 不带Authorized参数的情况下如果没有权限,会强制跳到403界面
          // If you do not have the Authorized parameter
          // you will be forced to jump to the 403 interface without permission
          Authorized={Authorized}
ddcat1115's avatar
ddcat1115 committed
155
          menuData={getMenuData()}
afc163's avatar
afc163 committed
156
          collapsed={collapsed}
偏右's avatar
偏右 committed
157
          location={location}
jiang's avatar
jiang committed
158
          isMobile={this.state.isMobile}
159
          onCollapse={this.handleMenuCollapse}
偏右's avatar
偏右 committed
160
        />
afc163's avatar
afc163 committed
161
        <Layout>
偏右's avatar
偏右 committed
162
          <GlobalHeader
163
            logo={logo}
偏右's avatar
偏右 committed
164 165 166 167
            currentUser={currentUser}
            fetchingNotices={fetchingNotices}
            notices={notices}
            collapsed={collapsed}
jiang's avatar
jiang committed
168
            isMobile={this.state.isMobile}
169 170 171 172
            onNoticeClear={this.handleNoticeClear}
            onCollapse={this.handleMenuCollapse}
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
偏右's avatar
偏右 committed
173
          />
afc163's avatar
afc163 committed
174
          <Content style={{ margin: '24px 24px 0', height: '100%' }}>
afc163's avatar
afc163 committed
175 176
            <div style={{ minHeight: 'calc(100vh - 260px)' }}>
              <Switch>
jim's avatar
jim committed
177 178 179 180 181
                {
                  redirectData.map(item =>
                    <Redirect key={item.from} exact from={item.from} to={item.to} />
                  )
                }
afc163's avatar
afc163 committed
182
                {
ddcat1115's avatar
ddcat1115 committed
183 184 185 186 187 188 189 190 191 192 193
                  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"
                      />
                    )
ddcat1115's avatar
ddcat1115 committed
194
                  )
afc163's avatar
afc163 committed
195
                }
196
                <Redirect exact from="/" to={bashRedirect} />
ddcat1115's avatar
ddcat1115 committed
197
                <Route render={NotFound} />
afc163's avatar
afc163 committed
198 199
              </Switch>
            </div>
afc163's avatar
afc163 committed
200 201
            <GlobalFooter
              links={[{
202
                key: 'Pro 首页',
afc163's avatar
afc163 committed
203
                title: 'Pro 首页',
afc163's avatar
afc163 committed
204 205 206
                href: 'http://pro.ant.design',
                blankTarget: true,
              }, {
207 208
                key: 'github',
                title: <Icon type="github" />,
afc163's avatar
afc163 committed
209 210 211
                href: 'https://github.com/ant-design/ant-design-pro',
                blankTarget: true,
              }, {
212
                key: 'Ant Design',
afc163's avatar
afc163 committed
213 214 215 216 217 218
                title: 'Ant Design',
                href: 'http://ant.design',
                blankTarget: true,
              }]}
              copyright={
                <div>
afc163's avatar
afc163 committed
219
                  Copyright <Icon type="copyright" /> 2018 蚂蚁金服体验技术部出品
afc163's avatar
afc163 committed
220 221 222 223
                </div>
              }
            />
          </Content>
224
        </Layout>
afc163's avatar
afc163 committed
225 226 227 228 229 230 231 232
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
          {params => <div className={classNames(params)}>{layout}</div>}
        </ContainerQuery>
233 234 235 236 237
      </DocumentTitle>
    );
  }
}

Andreas Cederström's avatar
Andreas Cederström committed
238 239 240 241 242
export default connect(({ user, global, loading }) => ({
  currentUser: user.currentUser,
  collapsed: global.collapsed,
  fetchingNotices: loading.effects['global/fetchNotices'],
  notices: global.notices,
243
}))(BasicLayout);