BasicLayout.js 5.62 KB
Newer Older
chencheng's avatar
chencheng committed
1
import React from 'react';
jim's avatar
jim committed
2
import { Layout } from 'antd';
3
import DocumentTitle from 'react-document-title';
afc163's avatar
afc163 committed
4
import isEqual from 'lodash/isEqual';
5
import memoizeOne from 'memoize-one';
6
import { connect } from 'dva';
afc163's avatar
afc163 committed
7 8
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
9
import pathToRegexp from 'path-to-regexp';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
10
import Media from 'react-media';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
11
import { formatMessage } from 'umi/locale';
12
import Authorized from '@/utils/Authorized';
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';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
17
import SiderMenu from '@/components/SiderMenu';
18
import { title } from '../defaultSettings';
19 20
import styles from './BasicLayout.less';

jim's avatar
jim committed
21
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
22

ๆ„š้“'s avatar
ๆ„š้“ committed
23 24
const Exception403 = <p>Exception403</p>;

afc163's avatar
afc163 committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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,
yoyo837's avatar
yoyo837 committed
43 44 45 46
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
47 48 49
  },
};

50
class BasicLayout extends React.PureComponent {
51 52
  constructor(props) {
    super(props);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
53
    this.getPageTitle = memoizeOne(this.getPageTitle);
54
    this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
55
  }
56 57

  componentDidMount() {
58 59 60 61
    const {
      dispatch,
      route: { routes, authority },
    } = this.props;
afc163's avatar
afc163 committed
62 63 64 65 66 67
    dispatch({
      type: 'user/fetchCurrent',
    });
    dispatch({
      type: 'setting/getSetting',
    });
68 69 70 71
    dispatch({
      type: 'menu/getMenuData',
      payload: { routes, authority },
    });
72 73
  }

74 75 76
  componentDidUpdate(preProps) {
    // After changing to phone mode,
    // if collapsed is true, you need to click twice to display
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
77
    const { collapsed, isMobile } = this.props;
78 79 80
    if (isMobile && !preProps.isMobile && !collapsed) {
      this.handleMenuCollapse(false);
    }
81 82
  }

jim's avatar
jim committed
83
  getContext() {
84
    const { location, breadcrumbNameMap } = this.props;
ddcat1115's avatar
ddcat1115 committed
85 86
    return {
      location,
87
      breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
88
    };
89
  }
90

91 92 93
  matchParamsPath = (pathname, breadcrumbNameMap) => {
    const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
    return breadcrumbNameMap[pathKey];
94 95
  };

96 97 98
  getRouterAuthority = (pathname, routeData) => {
    let routeAuthority = ['noAuthority'];
    const getAuthority = (key, routes) => {
99
      routes.forEach(route => {
100
        if (route.path && pathToRegexp(route.path).test(key)) {
101 102 103 104 105 106 107 108 109 110 111
          routeAuthority = route.authority;
        } else if (route.routes) {
          routeAuthority = getAuthority(key, route.routes);
        }
        return route;
      });
      return routeAuthority;
    };
    return getAuthority(pathname, routeData);
  };

112 113
  getPageTitle = (pathname, breadcrumbNameMap) => {
    const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
114

115
    if (!currRouterData) {
116
      return title;
ddcat1115's avatar
ddcat1115 committed
117
    }
xiaoiver's avatar
xiaoiver committed
118
    const pageName = formatMessage({
119 120 121
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
122

123
    return `${pageName} - ${title}`;
124
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
125

jim's avatar
jim committed
126
  getLayoutStyle = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
127
    const { fixSiderbar, isMobile, collapsed, layout } = this.props;
128
    if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
jim's avatar
jim committed
129
      return {
130
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
131 132 133 134
      };
    }
    return null;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
135

jim's avatar
jim committed
136
  handleMenuCollapse = collapsed => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
137 138
    const { dispatch } = this.props;
    dispatch({
139 140 141
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
142
  };
143

144
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
145
    const {
afc163's avatar
afc163 committed
146
      navTheme,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
147
      layout: PropsLayout,
ๆ„š้“'s avatar
ๆ„š้“ committed
148
      children,
149
      location: { pathname },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
150
      isMobile,
151
      menuData,
152
      breadcrumbNameMap,
153
      route: { routes },
154
      fixedHeader,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
155
    } = this.props;
156

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
157
    const isTop = PropsLayout === 'topmenu';
158
    const routerConfig = this.getRouterAuthority(pathname, routes);
159
    const contentStyle = !fixedHeader ? { paddingTop: 0 } : {};
afc163's avatar
afc163 committed
160 161
    const layout = (
      <Layout>
jim's avatar
jim committed
162
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
163 164
          <SiderMenu
            logo={logo}
afc163's avatar
afc163 committed
165
            theme={navTheme}
jim's avatar
jim committed
166
            onCollapse={this.handleMenuCollapse}
167
            menuData={menuData}
168
            isMobile={isMobile}
jim's avatar
jim committed
169
            {...this.props}
jim's avatar
jim committed
170 171
          />
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
172 173 174 175 176 177
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
178 179 180 181
          <Header
            menuData={menuData}
            handleMenuCollapse={this.handleMenuCollapse}
            logo={logo}
182
            isMobile={isMobile}
183 184
            {...this.props}
          />
185
          <Content className={styles.content} style={contentStyle}>
186
            <Authorized authority={routerConfig} noMatch={<Exception403 />}>
187 188 189
              {children}
            </Authorized>
          </Content>
jim's avatar
jim committed
190
          <Footer />
191
        </Layout>
afc163's avatar
afc163 committed
192 193 194
      </Layout>
    );
    return (
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
195
      <React.Fragment>
196
        <DocumentTitle title={this.getPageTitle(pathname, breadcrumbNameMap)}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
197 198 199 200 201 202 203 204 205
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
      </React.Fragment>
206 207 208 209
    );
  }
}

210
export default connect(({ global, setting, menu }) => ({
211
  collapsed: global.collapsed,
jim's avatar
jim committed
212
  layout: setting.layout,
213
  menuData: menu.menuData,
214
  breadcrumbNameMap: menu.breadcrumbNameMap,
jim's avatar
jim committed
215
  ...setting,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
216 217 218 219 220
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));