BasicLayout.js 6.11 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 74 75 76
  constructor(props) {
    super(props);
    const { routerData, menuData } = this.props;
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData, routerData);
  }

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

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

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

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

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

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

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

150
  render() {
愚道's avatar
愚道 committed
151
    // TODO remove old router code
陈帅's avatar
陈帅 committed
152 153
    const {
      isMobile,
愚道's avatar
愚道 committed
154 155
      // redirectData,
      // routerData,
陈帅's avatar
陈帅 committed
156 157
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
158
      children,
陈帅's avatar
陈帅 committed
159
      location: { pathname },
陈帅's avatar
陈帅 committed
160 161
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
愚道's avatar
愚道 committed
162 163
    // const bashRedirect = this.getBashRedirect();
    // const myRedirectData = redirectData || [];
afc163's avatar
afc163 committed
164 165
    const layout = (
      <Layout>
jim's avatar
jim committed
166
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
167 168 169
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
170
            theme={silderTheme}
jim's avatar
jim committed
171
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
172
            {...this.props}
jim's avatar
jim committed
173 174
          />
        )}
jim's avatar
jim committed
175
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
176
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
jim's avatar
jim committed
177
          <Content style={this.getContentStyle()}>
愚道's avatar
愚道 committed
178 179
            {children}
            {/* <Switch> TODO remove
jim's avatar
jim committed
180 181 182 183 184 185 186 187 188 189 190 191 192
              {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"
                />
              ))}
193 194
              <Redirect exact from="/" to={bashRedirect} />
              <Route render={NotFound} />
愚道's avatar
愚道 committed
195
            </Switch> */}
afc163's avatar
afc163 committed
196
          </Content>
jim's avatar
jim committed
197
          <Footer />
198
        </Layout>
afc163's avatar
afc163 committed
199 200
      </Layout>
    );
陈帅's avatar
陈帅 committed
201
    const getPageTitle = memoizeOne(this.getPageTitle);
afc163's avatar
afc163 committed
202
    return (
陈帅's avatar
陈帅 committed
203
      <DocumentTitle title={getPageTitle(pathname)}>
afc163's avatar
afc163 committed
204
        <ContainerQuery query={query}>
jim's avatar
jim committed
205
          {params => (
jim's avatar
jim committed
206 207 208
            <Context.Provider value={this.getContext()}>
              <div className={classNames(params)}>
                {layout}
jim's avatar
jim committed
209
                <SettingDarwer />
jim's avatar
jim committed
210 211
              </div>
            </Context.Provider>
jim's avatar
jim committed
212
          )}
afc163's avatar
afc163 committed
213
        </ContainerQuery>
214 215 216 217 218
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
219
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
220
  collapsed: global.collapsed,
jim's avatar
jim committed
221 222
  layout: setting.layout,
  ...setting,
陈帅's avatar
陈帅 committed
223
}))(BasicLayout);