BasicLayout.js 5.28 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
  console.log(meun, router);
陈帅's avatar
陈帅 committed
33 34 35 36 37 38 39 40 41 42 43
  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
44
});
45

afc163's avatar
afc163 committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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
64 65 66 67
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
68 69 70
  },
};

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

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

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

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

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

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

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

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

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

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

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