RightContent.jsx 5.12 KB
Newer Older
1 2 3 4
/*
 * @Author: dengchao 754083046@qq.com
 * @Date: 2022-05-09 11:19:45
 * @LastEditors: dengchao 754083046@qq.com
邓超's avatar
邓超 committed
5
 * @LastEditTime: 2023-02-06 17:06:06
6 7 8
 * @FilePath: \maintenance\src\components\GlobalHeader\RightContent.jsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
邓超's avatar
邓超 committed
9
import React, { useContext, useState, useEffect } from 'react';
10
import { RouteContext } from '@ant-design/pro-layout';
张烨's avatar
张烨 committed
11 12
import { Breadcrumb } from 'antd';
import check from '@/components/Authorized/CheckPermissions';
邓超's avatar
邓超 committed
13
import { Link, useLocation, matchPath } from 'react-router-dom';
dengxiaofeng's avatar
dengxiaofeng committed
14
import HeaderSearch from '../HeaderSearch';
Maofei94's avatar
Maofei94 committed
15
import Colophon from '../Colophon';
dengxiaofeng's avatar
dengxiaofeng committed
16
import Avatar from './AvatarDropdown';
dengxiaofeng's avatar
dengxiaofeng committed
17
import styles from './index.less';
dengxiaofeng's avatar
dengxiaofeng committed
18

张烨's avatar
张烨 committed
19 20 21 22 23 24 25
const routesOptions = route => {
  const buildOption = routeConfig => ({
    label: <Link to={routeConfig.path}>{routeConfig.name}</Link>,
    value: routeConfig.name,
  });
  return route.routes
    .map(r => {
张烨's avatar
张烨 committed
26
      if (r.hideMenu) return null;
张烨's avatar
张烨 committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
      if (r.authority) {
        if (r.routes) {
          return routesOptions(r);
        }
        return check(r.authority, buildOption(r), null);
      }
      if (r.routes) {
        return routesOptions(r);
      }
      return buildOption(r);
    })
    .flat()
    .filter(Boolean);
};

dengxiaofeng's avatar
dengxiaofeng committed
42 43
const GlobalHeaderRight = props => {
  const { theme, layout } = props;
邓超's avatar
邓超 committed
44
  const [dosRouter, setDosRouter] = useState('');
邓超's avatar
邓超 committed
45
  const [breadCrumbsRoutes, setBreadCrumbsRoutes] = useState([]);
邓超's avatar
邓超 committed
46
  const { pathname } = useLocation();
邓超's avatar
邓超 committed
47 48 49

  let className = styles.right;
  const value = useContext(RouteContext);
邓超's avatar
邓超 committed
50
  useEffect(() => {
邓超's avatar
邓超 committed
51 52
    let list = getBreadcrumbs();
    setBreadCrumbsRoutes(list);
邓超's avatar
邓超 committed
53 54
    setDosRouter(pathname);
  }, [pathname]);
张烨's avatar
张烨 committed
55

dengxiaofeng's avatar
dengxiaofeng committed
56
  if (theme === 'dark' && layout === 'top') {
dengxiaofeng's avatar
dengxiaofeng committed
57
    className = `${styles.right}  ${styles.dark}`;
dengxiaofeng's avatar
dengxiaofeng committed
58
  }
邓超's avatar
邓超 committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  const flattenRoutes = arr => {
    return arr.reduce((res, next) => {
      return res.concat(Array.isArray(next.routes) ? [next, ...flattenRoutes(next.routes)] : next);
    }, []);
  };
  const getBreadcrumbs = () => {
    // 初始化匹配数组match
    let matches = [];
    pathname
      // 取得路径名,然后将路径分割成每一路由部分.
      .split('?')[0]
      .split('/')
      // 对每一部分执行一次调用`getBreadcrumb()`的reduce.
      .reduce((prev, curSection) => {
        // 将最后一个路由部分与当前部分合并,比如当路径为 `/x/xx/xxx` 时,pathSection分别检查 `/x` `/x/xx` `/x/xx/xxx` 的匹配,并分别生成面包屑
        const pathSection = `${prev}/${curSection}`;
        // 对于 拆分的路径,从 flattenRoutes 中查找对应的路由
        const breadcrumb = getBreadcrumb(flattenRoutes(props.routes), curSection, pathSection);

        // 将面包屑导入到matches数组中
        matches.push(breadcrumb);

        // 传递给下一次reduce的路径部分
        return pathSection;
      });
    return matches;
  };
  const getBreadcrumb = (routes, curSection, pathSection) => {
    let matchRoute = routes.find(ele => {
      const { path } = ele;
      return matchPath(pathSection, { path, exact: true });
    });
dengxiaofeng's avatar
dengxiaofeng committed
91

邓超's avatar
邓超 committed
92 93
    return {
      breadcrumbName: matchRoute?.name,
皮倩雯's avatar
皮倩雯 committed
94 95
      path: matchRoute?.path,
      component: matchRoute?.component,
邓超's avatar
邓超 committed
96 97 98 99 100 101 102 103 104 105 106
    };
  };
  const itemRender = (route, params, routes, paths) => {
    const last = routes.indexOf(route) === routes.length - 1;
    return <span>{route.breadcrumbName}</span>;
    // return last ? (
    //   <span>{route.breadcrumbName}</span>
    // ) : (
    //   <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
    // );
  };
dengxiaofeng's avatar
dengxiaofeng committed
107 108
  return (
    <div className={className}>
109
      {/* <span className={styles.left}>{value.title}</span> */}
邓超's avatar
邓超 committed
110 111 112 113 114 115 116 117 118 119
      {/* <Breadcrumb
        routes={value.breadcrumb.routes}
        // {...value.breadcrumb}
        className={styles.lineHeight}
      /> */}
      <Breadcrumb
        routes={breadCrumbsRoutes}
        itemRender={itemRender}
        className={styles.lineHeight}
      />
120
      <div style={{ flex: 1 }} />
121
      {/* <div style={{ margin: '0 10px' }}>
邓超's avatar
邓超 committed
122 123 124 125 126 127 128
        <a
          target="_blank"
          href={`${window.location.origin}/civmanage/site/content${dosRouter}`}
          rel="noopener noreferer"
        >
          文档说明
        </a>
129
      </div> */}
130
      {/* <Colophon /> */}
131
      {/* <div style={{ margin: '0 10px' }}>
132 133 134 135 136 137 138
        <a
          target="_blank"
          href={`${window.location.origin}/cityoms3/4.0.html`}
          rel="noopener noreferer"
        >
          运维平台4.0
        </a>
139
      </div> */}
140
      {/* <HeaderSearch
141
        className={`${styles.action} ${styles.search} ${styles.toRight}`}
dengxiaofeng's avatar
dengxiaofeng committed
142
        placeholder="站内搜索"
dengxiaofeng's avatar
dengxiaofeng committed
143
        defaultValue=""
张烨's avatar
张烨 committed
144 145
        filterOption={(inputValue, option) =>
          option.value.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
146
        }
张烨's avatar
张烨 committed
147
        options={[...routesOptions(value.route)]}
148
      /> */}
张烨's avatar
张烨 committed
149
      {/* <NoticeIcon className={`${styles.action} ${styles.toRight}`} /> */}
150
      <Avatar className={styles.toRight} />
dengxiaofeng's avatar
dengxiaofeng committed
151 152 153 154 155 156
      {/* <SelectLang className={styles.action} /> */}
    </div>
  );
};

export default GlobalHeaderRight;