BasicLayout.js 5.25 KB
Newer Older
愚道's avatar
愚道 committed
1 2
/* eslint-disable no-unused-vars */
// TODO remove eslint-disable
jim's avatar
jim committed
3 4
import React from 'react';
import { Layout } from 'antd';
5
import DocumentTitle from 'react-document-title';
陈帅's avatar
陈帅 committed
6
import memoizeOne from 'memoize-one';
7
import { connect } from 'dva';
愚道's avatar
愚道 committed
8
// import { Route, Redirect, Switch } from 'dva/router';
afc163's avatar
afc163 committed
9 10
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
11
import pathToRegexp from 'path-to-regexp';
陈帅's avatar
陈帅 committed
12
import { formatMessage } from 'umi/locale';
愚道's avatar
愚道 committed
13 14 15 16 17 18
import SiderMenu from '../../components/SiderMenu';
// import NotFound from '../Exception/404';
// import { getRoutes } from '../utils/utils';
import Authorized from '../../utils/Authorized';
import SettingDarwer from '../../components/SettingDarwer';
import logo from '../../assets/logo.svg';
jim's avatar
jim committed
19 20
import Footer from './Footer';
import Header from './Header';
21
import Context from './MenuContext';
22

jim's avatar
jim committed
23
const { Content } = Layout;
陈帅's avatar
陈帅 committed
24
const { check } = Authorized;
ddcat1115's avatar
ddcat1115 committed
25

26 27 28 29 30
/**
 * 获取面包屑映射
 * @param {Object} menuData 菜单配置
 * @param {Object} routerData 路由配置
 */
陈帅's avatar
陈帅 committed
31
const getBreadcrumbNameMap = memoizeOne((meun, router) => {
陈帅's avatar
陈帅 committed
32 33 34 35 36 37 38 39 40 41 42
  const routerMap = {};
  const mergeMeunAndRouter = meunData => {
    meunData.forEach(meunItem => {
      routerMap[meunItem.path] = Object.assign(meunItem, router);
      if (meunItem.children) {
        mergeMeunAndRouter(meunItem.children);
      }
    });
  };
  mergeMeunAndRouter(meun);
  return routerMap;
陈帅's avatar
陈帅 committed
43
});
44

afc163's avatar
afc163 committed
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,
yoyo837's avatar
yoyo837 committed
63 64 65 66
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
67 68 69
  },
};

70
class BasicLayout extends React.PureComponent {
陈帅's avatar
陈帅 committed
71 72 73
  constructor(props) {
    super(props);
    const { routerData, menuData } = this.props;
陈帅's avatar
陈帅 committed
74
    this.getPageTitle = memoizeOne(this.getPageTitle);
陈帅's avatar
陈帅 committed
75 76 77
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData, routerData);
  }

jim's avatar
jim committed
78
  getContext() {
陈帅's avatar
陈帅 committed
79
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
80 81
    return {
      location,
陈帅's avatar
陈帅 committed
82
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
83
    };
84
  }
陈帅's avatar
陈帅 committed
85

陈帅's avatar
陈帅 committed
86
  getPageTitle = pathname => {
jim's avatar
jim committed
87 88
    let currRouterData = null;
    // match params path
陈帅's avatar
陈帅 committed
89
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
90
      if (pathToRegexp(key).test(pathname)) {
陈帅's avatar
陈帅 committed
91
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
92 93
      }
    });
陈帅's avatar
陈帅 committed
94 95
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
96
    }
陈帅's avatar
陈帅 committed
97
    const message = formatMessage({
陈帅's avatar
陈帅 committed
98 99 100 101 102
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
陈帅's avatar
陈帅 committed
103

jim's avatar
jim committed
104
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
105 106
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
107
      return {
108
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
109 110 111 112
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
113

jim's avatar
jim committed
114 115 116 117 118 119 120
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
121

122 123 124 125
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
126 127

    const redirect = urlParams.searchParams.get('redirect');
128
    // Remove the parameters in the url
jim's avatar
jim committed
129 130 131 132
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
ddcat1115's avatar
ddcat1115 committed
133 134
      const { routerData } = this.props;
      // get the first authorized route path in routerData
jim's avatar
jim committed
135 136 137
      const authorizedPath = Object.keys(routerData).find(
        item => check(routerData[item].authority, item) && item !== '/'
      );
ddcat1115's avatar
ddcat1115 committed
138
      return authorizedPath;
jim's avatar
jim committed
139
    }
140
    return redirect;
jim's avatar
jim committed
141
  };
陈帅's avatar
陈帅 committed
142

jim's avatar
jim committed
143
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
144 145
    const { dispatch } = this.props;
    dispatch({
146 147 148
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
149
  };
jim's avatar
jim committed
150

151
  render() {
陈帅's avatar
陈帅 committed
152 153 154 155
    const {
      isMobile,
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
156
      children,
陈帅's avatar
陈帅 committed
157
      location: { pathname },
陈帅's avatar
陈帅 committed
158 159
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
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 165
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
166
            theme={silderTheme}
jim's avatar
jim committed
167
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
168
            {...this.props}
jim's avatar
jim committed
169 170
          />
        )}
jim's avatar
jim committed
171
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
172
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
陈帅's avatar
陈帅 committed
173
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
174
          <Footer />
175
        </Layout>
afc163's avatar
afc163 committed
176 177
      </Layout>
    );
陈帅's avatar
陈帅 committed
178

afc163's avatar
afc163 committed
179
    return (
陈帅's avatar
陈帅 committed
180
      <DocumentTitle title={this.getPageTitle(pathname)}>
afc163's avatar
afc163 committed
181
        <ContainerQuery query={query}>
jim's avatar
jim committed
182
          {params => (
jim's avatar
jim committed
183 184 185
            <Context.Provider value={this.getContext()}>
              <div className={classNames(params)}>
                {layout}
jim's avatar
jim committed
186
                <SettingDarwer />
jim's avatar
jim committed
187 188
              </div>
            </Context.Provider>
jim's avatar
jim committed
189
          )}
afc163's avatar
afc163 committed
190
        </ContainerQuery>
191 192 193 194 195
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
196
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
197
  collapsed: global.collapsed,
jim's avatar
jim committed
198 199
  layout: setting.layout,
  ...setting,
陈帅's avatar
陈帅 committed
200
}))(BasicLayout);