BasicLayout.js 5.63 KB
Newer Older
jim's avatar
jim committed
1 2
import React from 'react';
import { Layout } from 'antd';
3 4
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
jim's avatar
jim committed
5
import { Route, Redirect, Switch } from 'dva/router';
afc163's avatar
afc163 committed
6 7
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
8
import pathToRegexp from 'path-to-regexp';
偏右's avatar
偏右 committed
9
import SiderMenu from '../components/SiderMenu';
愚道's avatar
愚道 committed
10
import NotFound from '../pages/Exception/404';
ddcat1115's avatar
ddcat1115 committed
11
import { getRoutes } from '../utils/utils';
ddcat1115's avatar
ddcat1115 committed
12
import Authorized from '../utils/Authorized';
jim's avatar
jim committed
13
import SettingDarwer from '../components/SettingDarwer';
14
import logo from '../assets/logo.svg';
jim's avatar
jim committed
15 16
import Footer from './Footer';
import Header from './Header';
17
import Context from './MenuContext';
18

jim's avatar
jim committed
19
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
20
const { AuthorizedRoute, check } = Authorized;
ddcat1115's avatar
ddcat1115 committed
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * 获取面包屑映射
 * @param {Object} menuData 菜单配置
 * @param {Object} routerData 路由配置
 */
const getBreadcrumbNameMap = (menuData, routerData) => {
  const result = {};
  const childResult = {};
  for (const i of menuData) {
    if (!routerData[i.path]) {
      result[i.path] = i;
    }
    if (i.children) {
      Object.assign(childResult, getBreadcrumbNameMap(i.children, routerData));
    }
  }
  return Object.assign({}, routerData, result, childResult);
};

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

66
class BasicLayout extends React.PureComponent {
jim's avatar
jim committed
67
  getContext() {
ddcat1115's avatar
ddcat1115 committed
68
    const { location, routerData, menuData } = this.props;
ddcat1115's avatar
ddcat1115 committed
69 70
    return {
      location,
ddcat1115's avatar
ddcat1115 committed
71
      breadcrumbNameMap: getBreadcrumbNameMap(menuData, routerData),
ddcat1115's avatar
ddcat1115 committed
72
    };
73
  }
陈帅's avatar
陈帅 committed
74

75
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
76
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
77 78
    const { pathname } = location;
    let title = 'Ant Design Pro';
jim's avatar
jim committed
79 80 81 82 83 84 85 86 87
    let currRouterData = null;
    // match params path
    Object.keys(routerData).forEach(key => {
      if (pathToRegexp(key).test(pathname)) {
        currRouterData = routerData[key];
      }
    });
    if (currRouterData && currRouterData.name) {
      title = `${currRouterData.name} - Ant Design Pro`;
ddcat1115's avatar
ddcat1115 committed
88
    }
ddcat1115's avatar
ddcat1115 committed
89
    return title;
90
  }
陈帅's avatar
陈帅 committed
91

jim's avatar
jim committed
92
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
93 94
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
95
      return {
96
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
97 98 99 100
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
101

jim's avatar
jim committed
102 103 104 105 106 107 108
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
109

110 111 112 113
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
114 115

    const redirect = urlParams.searchParams.get('redirect');
116
    // Remove the parameters in the url
jim's avatar
jim committed
117 118 119 120
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
ddcat1115's avatar
ddcat1115 committed
121 122
      const { routerData } = this.props;
      // get the first authorized route path in routerData
jim's avatar
jim committed
123 124 125
      const authorizedPath = Object.keys(routerData).find(
        item => check(routerData[item].authority, item) && item !== '/'
      );
ddcat1115's avatar
ddcat1115 committed
126
      return authorizedPath;
jim's avatar
jim committed
127
    }
128
    return redirect;
jim's avatar
jim committed
129
  };
陈帅's avatar
陈帅 committed
130

jim's avatar
jim committed
131
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
132 133
    const { dispatch } = this.props;
    dispatch({
134 135 136
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
137
  };
jim's avatar
jim committed
138

139
  render() {
陈帅's avatar
陈帅 committed
140 141 142 143 144 145 146 147 148
    const {
      isMobile,
      redirectData,
      routerData,
      silderTheme,
      layout: PropsLayout,
      match,
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
149
    const bashRedirect = this.getBashRedirect();
jim's avatar
jim committed
150
    const myRedirectData = redirectData || [];
afc163's avatar
afc163 committed
151 152
    const layout = (
      <Layout>
jim's avatar
jim committed
153
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
154 155 156
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
157
            theme={silderTheme}
jim's avatar
jim committed
158
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
159
            {...this.props}
jim's avatar
jim committed
160 161
          />
        )}
jim's avatar
jim committed
162
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
163
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
jim's avatar
jim committed
164
          <Content style={this.getContentStyle()}>
165
            <Switch>
jim's avatar
jim committed
166 167 168 169 170 171 172 173 174 175 176 177 178
              {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"
                />
              ))}
179 180 181
              <Redirect exact from="/" to={bashRedirect} />
              <Route render={NotFound} />
            </Switch>
afc163's avatar
afc163 committed
182
          </Content>
jim's avatar
jim committed
183
          <Footer />
184
        </Layout>
afc163's avatar
afc163 committed
185 186 187 188 189 190
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
jim's avatar
jim committed
191
          {params => (
jim's avatar
jim committed
192 193 194
            <Context.Provider value={this.getContext()}>
              <div className={classNames(params)}>
                {layout}
jim's avatar
jim committed
195
                <SettingDarwer />
jim's avatar
jim committed
196 197
              </div>
            </Context.Provider>
jim's avatar
jim committed
198
          )}
afc163's avatar
afc163 committed
199
        </ContainerQuery>
200 201 202 203 204
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
205
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
206
  collapsed: global.collapsed,
jim's avatar
jim committed
207 208
  layout: setting.layout,
  ...setting,
209
}))(BasicLayout);