BasicLayout.js 5.58 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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