|
|
|
@ -1,9 +1,8 @@
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { Breadcrumb } from 'antd';
|
|
|
|
import { Breadcrumb } from 'antd';
|
|
|
|
import { CloseOutlined } from '@ant-design/icons';
|
|
|
|
import { CloseCircleOutlined } from '@ant-design/icons';
|
|
|
|
import { history, useLocation, matchRoutes } from '@umijs/max';
|
|
|
|
import { history, useLocation } from '@umijs/max';
|
|
|
|
import './breadcrumb.less';
|
|
|
|
import './breadcrumb.less';
|
|
|
|
import routes from '../../../config/routes';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const CustomBreadcrumb = () => {
|
|
|
|
const CustomBreadcrumb = () => {
|
|
|
|
// 从sessionStorage初始化面包屑数据,与Vue实例保持一致
|
|
|
|
// 从sessionStorage初始化面包屑数据,与Vue实例保持一致
|
|
|
|
@ -15,6 +14,24 @@ const CustomBreadcrumb = () => {
|
|
|
|
|
|
|
|
|
|
|
|
const location = useLocation();
|
|
|
|
const location = useLocation();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 递归搜索window.dynamicRoute中匹配当前路径的路由项
|
|
|
|
|
|
|
|
const findMatchedRoute = (routes, pathname) => {
|
|
|
|
|
|
|
|
for (const route of routes) {
|
|
|
|
|
|
|
|
// 检查当前路由项是否匹配
|
|
|
|
|
|
|
|
if (route.key === pathname) {
|
|
|
|
|
|
|
|
return route;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果有子路由,递归搜索
|
|
|
|
|
|
|
|
if (route.children && route.children.length > 0) {
|
|
|
|
|
|
|
|
const matchedChild = findMatchedRoute(route.children, pathname);
|
|
|
|
|
|
|
|
if (matchedChild) {
|
|
|
|
|
|
|
|
return matchedChild;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前路由的真实信息
|
|
|
|
// 获取当前路由的真实信息
|
|
|
|
const getCurrentRoute = () => {
|
|
|
|
const getCurrentRoute = () => {
|
|
|
|
const pathname = location.pathname || '/';
|
|
|
|
const pathname = location.pathname || '/';
|
|
|
|
@ -22,23 +39,22 @@ const CustomBreadcrumb = () => {
|
|
|
|
|
|
|
|
|
|
|
|
console.log('当前路径:', pathname);
|
|
|
|
console.log('当前路径:', pathname);
|
|
|
|
|
|
|
|
|
|
|
|
// 使用matchRoutes匹配当前路径与路由配置
|
|
|
|
// 使用自定义函数在window.dynamicRoute中匹配当前路径
|
|
|
|
const matchedRoutes = matchRoutes(routes, pathname);
|
|
|
|
let matchedRoute = null;
|
|
|
|
|
|
|
|
if (window.dynamicRoute && Array.isArray(window.dynamicRoute)) {
|
|
|
|
console.log('匹配的路由:', matchedRoutes);
|
|
|
|
matchedRoute = findMatchedRoute(window.dynamicRoute, pathname);
|
|
|
|
|
|
|
|
}
|
|
|
|
// 如果有匹配的路由,获取最深层的匹配路由信息
|
|
|
|
|
|
|
|
if (matchedRoutes && matchedRoutes.length > 0) {
|
|
|
|
|
|
|
|
const matchedRoute = matchedRoutes[matchedRoutes.length - 1]; // 获取最深层的匹配路由
|
|
|
|
|
|
|
|
const routeConfig = matchedRoute.route;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('匹配的路由配置:', routeConfig);
|
|
|
|
console.log('匹配的路由:', matchedRoute);
|
|
|
|
|
|
|
|
|
|
|
|
// 根据路由配置文件,直接使用routeConfig.name作为标题,这样可以确保名称正确显示
|
|
|
|
// 如果有匹配的路由,获取路由信息
|
|
|
|
const routeTitle = routeConfig.name || '未知页面';
|
|
|
|
if (matchedRoute) {
|
|
|
|
|
|
|
|
// 使用route.label作为标题,route.key作为路径标识符
|
|
|
|
|
|
|
|
const routeTitle = matchedRoute.label || '未知页面';
|
|
|
|
|
|
|
|
const routeKey = matchedRoute.key || pathname;
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
name: routeConfig.name || pathname.replace(/\//g, '_').slice(1) || 'home',
|
|
|
|
name: routeKey.replace(/\//g, '_').slice(1) || 'home',
|
|
|
|
path: pathname,
|
|
|
|
path: pathname,
|
|
|
|
href,
|
|
|
|
href,
|
|
|
|
fullPath: href,
|
|
|
|
fullPath: href,
|
|
|
|
@ -49,18 +65,37 @@ const CustomBreadcrumb = () => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 手动处理特殊情况,确保即使matchRoutes不工作,也能显示正确的路由名称
|
|
|
|
// 如果没有从window.dynamicRoute中匹配到路由,使用原来的逻辑处理
|
|
|
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
|
|
// 将路径按照/分割,获取最后一段作为名称
|
|
|
|
const lastSegment = pathSegments[pathSegments.length - 1] || 'home';
|
|
|
|
const parts = pathname.split('/').filter(Boolean);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根路径处理
|
|
|
|
|
|
|
|
if (parts.length === 0) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
name: 'home',
|
|
|
|
|
|
|
|
path: pathname,
|
|
|
|
|
|
|
|
href,
|
|
|
|
|
|
|
|
fullPath: href,
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
|
|
|
|
title: '首页',
|
|
|
|
|
|
|
|
affix: true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取最后一段作为页面名称
|
|
|
|
|
|
|
|
const lastPart = parts[parts.length - 1];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 使用最后一段作为标题
|
|
|
|
|
|
|
|
const routeTitle = lastPart || '未知页面';
|
|
|
|
|
|
|
|
|
|
|
|
// 直接返回路径的最后一段作为名称
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
name: lastSegment,
|
|
|
|
name: lastPart.replace(/\//g, '_') || 'home',
|
|
|
|
path: pathname,
|
|
|
|
path: pathname,
|
|
|
|
href,
|
|
|
|
href,
|
|
|
|
fullPath: href,
|
|
|
|
fullPath: href,
|
|
|
|
meta: {
|
|
|
|
meta: {
|
|
|
|
title: lastSegment === 'home' ? '首页' : lastSegment,
|
|
|
|
title: routeTitle,
|
|
|
|
affix: pathname === '/'
|
|
|
|
affix: pathname === '/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
@ -214,8 +249,8 @@ const CustomBreadcrumb = () => {
|
|
|
|
className={`breadcrumb-item ${item.name === currentRouterName ? 'breadcrumb-item-active' : ''}`}
|
|
|
|
className={`breadcrumb-item ${item.name === currentRouterName ? 'breadcrumb-item-active' : ''}`}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
<span className="breadcrumb-item-content">
|
|
|
|
<span className="breadcrumb-item-content">
|
|
|
|
<span className="breadcrumb-item-text">{item.title}</span>
|
|
|
|
<span className={`breadcrumb-item-text ${item.name === currentRouterName ? 'breadcrumb-item-text-active' : ''}`}>{item.title}</span>
|
|
|
|
<CloseOutlined
|
|
|
|
<CloseCircleOutlined
|
|
|
|
className="breadcrumb-close-icon"
|
|
|
|
className="breadcrumb-close-icon"
|
|
|
|
onClick={(e) => {
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.stopPropagation();
|
|
|
|
|