BasicLayout.js 5.21 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
const { AuthorizedRoute, check } = Authorized;
ddcat1115's avatar
ddcat1115 committed
20

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

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * 获取面包屑映射
 * @param {Object} menuData 菜单配置
 * @param {Object} routerData 路由配置
 */
const getBreadcrumbNameMap = (menuData, routerData) => {
  const result = {};
  const childResult = {};
  for (const i of menuData) {
    if (!routerData[i.path]) {
      result[i.path] = i;
    }
    if (i.children) {
      Object.assign(childResult, getBreadcrumbNameMap(i.children, routerData));
    }
  }
  return Object.assign({}, routerData, result, childResult);
};

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,
  },
};

63 64
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
65 66
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
jiang's avatar
jiang committed
67
  };
68
  getChildContext() {
ddcat1115's avatar
ddcat1115 committed
69
    const { location, routerData, menuData } = this.props;
ddcat1115's avatar
ddcat1115 committed
70 71
    return {
      location,
ddcat1115's avatar
ddcat1115 committed
72
      breadcrumbNameMap: getBreadcrumbNameMap(menuData, routerData),
ddcat1115's avatar
ddcat1115 committed
73
    };
74 75
  }
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
76
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
77 78
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
79 80 81
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - Ant Design Pro`;
    }
ddcat1115's avatar
ddcat1115 committed
82
    return title;
83
  }
84 85 86 87
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
88 89

    const redirect = urlParams.searchParams.get('redirect');
90
    // Remove the parameters in the url
jim's avatar
jim committed
91 92 93 94
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
ddcat1115's avatar
ddcat1115 committed
95 96
      const { routerData } = this.props;
      // get the first authorized route path in routerData
jim's avatar
jim committed
97 98 99
      const authorizedPath = Object.keys(routerData).find(
        item => check(routerData[item].authority, item) && item !== '/'
      );
ddcat1115's avatar
ddcat1115 committed
100
      return authorizedPath;
jim's avatar
jim committed
101
    }
102
    return redirect;
jim's avatar
jim committed
103
  };
jim's avatar
jim committed
104
  handleMenuCollapse = collapsed => {
105 106 107 108
    this.props.dispatch({
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
109
  };
jim's avatar
jim committed
110
  changeSetting = setting => {
111
    this.props.dispatch({
jim's avatar
jim committed
112 113
      type: 'setting/changeSetting',
      payload: setting,
114
    });
jim's avatar
jim committed
115
  };
116
  render() {
jim's avatar
jim committed
117
    const { isMobile, redirectData, routerData, fixedHeader, match } = this.props;
jim's avatar
jim committed
118
    const isTop = this.props.layout === 'topmenu';
119
    const bashRedirect = this.getBashRedirect();
jim's avatar
jim committed
120
    const myRedirectData = redirectData || [];
afc163's avatar
afc163 committed
121 122
    const layout = (
      <Layout>
jim's avatar
jim committed
123
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
124 125 126
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
jim's avatar
jim committed
127
            theme={this.props.silderTheme}
jim's avatar
jim committed
128
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
129
            {...this.props}
jim's avatar
jim committed
130 131
          />
        )}
afc163's avatar
afc163 committed
132
        <Layout>
jim's avatar
jim committed
133
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
jim's avatar
jim committed
134 135 136 137 138 139 140
          <Content
            style={{
              margin: '24px 24px 0',
              height: '100%',
              paddingTop: fixedHeader ? 64 : 0,
            }}
          >
141
            <Switch>
jim's avatar
jim committed
142 143 144 145 146 147 148 149 150 151 152 153 154
              {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"
                />
              ))}
155 156 157
              <Redirect exact from="/" to={bashRedirect} />
              <Route render={NotFound} />
            </Switch>
afc163's avatar
afc163 committed
158
          </Content>
jim's avatar
jim committed
159
          <Footer />
160
        </Layout>
afc163's avatar
afc163 committed
161 162 163 164 165 166
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
jim's avatar
jim committed
167 168 169 170 171 172
          {params => (
            <div className={classNames(params)}>
              {layout}
              <RightSidebar onChange={this.changeSetting} />
            </div>
          )}
afc163's avatar
afc163 committed
173
        </ContainerQuery>
174 175 176 177 178
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
179
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
180
  collapsed: global.collapsed,
jim's avatar
jim committed
181 182
  layout: setting.layout,
  ...setting,
183
}))(BasicLayout);