BasicLayout.js 8.18 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Fragment } from 'react';
2
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';
jim's avatar
jim committed
12
import TopNavHeader from '../components/TopNavHeader';
偏右's avatar
偏右 committed
13
import SiderMenu from '../components/SiderMenu';
afc163's avatar
afc163 committed
14
import NotFound from '../routes/Exception/404';
ddcat1115's avatar
ddcat1115 committed
15
import { getRoutes } from '../utils/utils';
ddcat1115's avatar
ddcat1115 committed
16
import Authorized from '../utils/Authorized';
陈帅's avatar
陈帅 committed
17
import { getMenuData } from '../common/menu';
18
import logo from '../assets/logo.svg';
19

20
const { Content, Header, Footer } = Layout;
ddcat1115's avatar
ddcat1115 committed
21 22
const { AuthorizedRoute } = Authorized;

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

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

68 69
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
70 71
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
72
  }
jiang's avatar
jiang committed
73 74 75
  state = {
    isMobile,
  };
76
  getChildContext() {
ddcat1115's avatar
ddcat1115 committed
77 78 79 80 81
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
82
  }
jiang's avatar
jiang committed
83
  componentDidMount() {
afc163's avatar
afc163 committed
84
    enquireScreen((mobile) => {
jiang's avatar
jiang committed
85
      this.setState({
afc163's avatar
afc163 committed
86
        isMobile: mobile,
jiang's avatar
jiang committed
87 88
      });
    });
89 90 91
    this.props.dispatch({
      type: 'user/fetchCurrent',
    });
jiang's avatar
jiang committed
92
  }
93
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
94
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
95 96
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
97 98 99
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - Ant Design Pro`;
    }
ddcat1115's avatar
ddcat1115 committed
100
    return title;
101
  }
102 103 104 105
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
106 107

    const redirect = urlParams.searchParams.get('redirect');
108
    // Remove the parameters in the url
jim's avatar
jim committed
109 110 111 112 113 114
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
      return '/dashboard/analysis';
    }
115 116
    return redirect;
  }
117 118 119 120 121 122 123 124 125 126 127 128 129 130
  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 }) => {
ddcat1115's avatar
ddcat1115 committed
131
    if (key === 'userCenter') {
ddcat1115's avatar
ddcat1115 committed
132
      this.props.dispatch(routerRedux.push('/user-profile/user-center'));
ddcat1115's avatar
ddcat1115 committed
133 134
      return;
    }
135 136 137 138
    if (key === 'triggerError') {
      this.props.dispatch(routerRedux.push('/exception/trigger'));
      return;
    }
陈帅's avatar
陈帅 committed
139
    if (key === 'userinfo') {
ddcat1115's avatar
ddcat1115 committed
140
      this.props.dispatch(routerRedux.push('/user-profile/userinfo/base'));
陈帅's avatar
陈帅 committed
141 142
      return;
    }
143 144 145 146 147 148 149 150 151 152 153 154 155
    if (key === 'logout') {
      this.props.dispatch({
        type: 'login/logout',
      });
    }
  }
  handleNoticeVisibleChange = (visible) => {
    if (visible) {
      this.props.dispatch({
        type: 'global/fetchNotices',
      });
    }
  }
156
  render() {
jim's avatar
jim committed
157
    const isFluid = this.props.layout === 'fluid';
偏右's avatar
偏右 committed
158
    const {
159
      currentUser, collapsed, fetchingNotices, notices, routerData, match, location,
偏右's avatar
偏右 committed
160
    } = this.props;
161
    const bashRedirect = this.getBashRedirect();
afc163's avatar
afc163 committed
162 163
    const layout = (
      <Layout>
jim's avatar
jim committed
164 165 166
        {isFluid && !isMobile ? null : (
          <SiderMenu
            logo={logo}
ddcat1115's avatar
ddcat1115 committed
167 168 169
          // 不带Authorized参数的情况下如果没有权限,会强制跳到403界面
          // If you do not have the Authorized parameter
          // you will be forced to jump to the 403 interface without permission
jim's avatar
jim committed
170 171 172 173 174 175 176 177
            Authorized={Authorized}
            menuData={getMenuData()}
            collapsed={collapsed}
            location={location}
            isMobile={this.state.isMobile}
            onCollapse={this.handleMenuCollapse}
          />
        )}
afc163's avatar
afc163 committed
178
        <Layout>
179
          <Header style={{ padding: 0 }}>
jim's avatar
jim committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
            {isFluid && !isMobile ? (
              <TopNavHeader
                logo={logo}
                mode="horizontal"
                location={location}
                menuData={getMenuData()}
                isMobile={this.state.isMobile}
                onNoticeClear={this.handleNoticeClear}
                onCollapse={this.handleMenuCollapse}
                onMenuClick={this.handleMenuClick}
                onNoticeVisibleChange={this.handleNoticeVisibleChange}
                notices={notices}
                currentUser={currentUser}
                fetchingNotices={fetchingNotices}
              />
          ) : (
196 197 198 199 200 201 202 203 204 205 206 207
            <GlobalHeader
              logo={logo}
              currentUser={currentUser}
              fetchingNotices={fetchingNotices}
              notices={notices}
              collapsed={collapsed}
              isMobile={this.state.isMobile}
              onNoticeClear={this.handleNoticeClear}
              onCollapse={this.handleMenuCollapse}
              onMenuClick={this.handleMenuClick}
              onNoticeVisibleChange={this.handleNoticeVisibleChange}
            />
jim's avatar
jim committed
208
                    )}
209
          </Header>
afc163's avatar
afc163 committed
210
          <Content style={{ margin: '24px 24px 0', height: '100%' }}>
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
            <Switch>
              {
                redirectData.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"
                    />
ddcat1115's avatar
ddcat1115 committed
228
                  )
229
                )
afc163's avatar
afc163 committed
230
              }
231 232 233
              <Redirect exact from="/" to={bashRedirect} />
              <Route render={NotFound} />
            </Switch>
afc163's avatar
afc163 committed
234
          </Content>
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
          <Footer style={{ padding: 0 }}>
            <GlobalFooter
              links={[{
                key: 'Pro 首页',
                title: 'Pro 首页',
                href: 'http://pro.ant.design',
                blankTarget: true,
              }, {
                key: 'github',
                title: <Icon type="github" />,
                href: 'https://github.com/ant-design/ant-design-pro',
                blankTarget: true,
              }, {
                key: 'Ant Design',
                title: 'Ant Design',
                href: 'http://ant.design',
                blankTarget: true,
              }]}
              copyright={
陈帅's avatar
陈帅 committed
254
                <Fragment>
255
                  Copyright <Icon type="copyright" /> 2018 蚂蚁金服体验技术部出品
陈帅's avatar
陈帅 committed
256
                </Fragment>
257 258 259
              }
            />
          </Footer>
260
        </Layout>
afc163's avatar
afc163 committed
261 262 263 264 265 266 267 268
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
          {params => <div className={classNames(params)}>{layout}</div>}
        </ContainerQuery>
269 270 271 272 273
      </DocumentTitle>
    );
  }
}

Andreas Cederström's avatar
Andreas Cederström committed
274 275 276
export default connect(({ user, global, loading }) => ({
  currentUser: user.currentUser,
  collapsed: global.collapsed,
jim's avatar
jim committed
277
  layout: global.layout,
Andreas Cederström's avatar
Andreas Cederström committed
278 279
  fetchingNotices: loading.effects['global/fetchNotices'],
  notices: global.notices,
280
}))(BasicLayout);