BasicLayout.js 4.96 KB
Newer Older
jim's avatar
jim committed
1 2
import React from 'react';
import { Layout } from 'antd';
3 4
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
jim's avatar
jim committed
5
import { Route, Redirect, Switch } from 'dva/router';
afc163's avatar
afc163 committed
6 7
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
偏右's avatar
偏右 committed
8
import SiderMenu from '../components/SiderMenu';
afc163's avatar
afc163 committed
9
import NotFound from '../routes/Exception/404';
ddcat1115's avatar
ddcat1115 committed
10
import { getRoutes } from '../utils/utils';
ddcat1115's avatar
ddcat1115 committed
11
import Authorized from '../utils/Authorized';
jim's avatar
jim committed
12
import Sidebar from '../components/Sidebar';
13
import logo from '../assets/logo.svg';
jim's avatar
jim committed
14 15
import Footer from './Footer';
import Header from './Header';
16
import Context from './MenuContext';
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

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * 获取面包屑映射
 * @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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
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,
  },
};

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

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

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
jim's avatar
jim committed
155
          {params => (
jim's avatar
jim committed
156 157 158
            <Context.Provider value={this.getContext()}>
              <div className={classNames(params)}>
                {layout}
jim's avatar
jim committed
159
                <Sidebar />
jim's avatar
jim committed
160 161
              </div>
            </Context.Provider>
jim's avatar
jim committed
162
          )}
afc163's avatar
afc163 committed
163
        </ContainerQuery>
164 165 166 167 168
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
169
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
170
  collapsed: global.collapsed,
jim's avatar
jim committed
171 172
  layout: setting.layout,
  ...setting,
173
}))(BasicLayout);