zhangwei
2025-06-26 f397a6cfd118ae36022576374ed4a1fd9e15448d
src/router/utils.ts
@@ -120,14 +120,14 @@
/** 查找对应 `path` 的路由信息 */
function findRouteByPath(path: string, routes: RouteRecordRaw[]) {
  let res = routes.find((item: { path: string }) => item.path == path);
  let res = routes?.find((item: { path: string }) => item.path == path);
  if (res) {
    return isProxy(res) ? toRaw(res) : res;
  } else {
    for (let i = 0; i < routes.length; i++) {
    for (let i = 0; i < routes?.length; i++) {
      if (
        routes[i].children instanceof Array &&
        routes[i].children.length > 0
        routes[i]?.children instanceof Array &&
        routes[i]?.children.length > 0
      ) {
        res = findRouteByPath(path, routes[i].children);
        if (res) {
@@ -151,21 +151,21 @@
/** 处理动态路由(后端返回的路由) */
function handleAsyncRoutes(routeList) {
  if (routeList.length === 0) {
  if (routeList?.length === 0) {
    usePermissionStoreHook().handleWholeMenus(routeList);
  } else {
    formatFlatteningRoutes(addAsyncRoutes(routeList)).map(
      (v: RouteRecordRaw) => {
        // 防止重复添加路由
        if (
          router.options.routes[0].children.findIndex(
          router.options.routes[0].children?.findIndex(
            value => value.path === v.path
          ) !== -1
        ) {
          return;
        } else {
          // 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转
          router.options.routes[0].children.push(v);
          router.options.routes[0].children?.push(v);
          // 最终路由进行升序
          ascending(router.options.routes[0].children);
          if (!router.hasRoute(v?.name)) router.addRoute(v);
@@ -227,7 +227,7 @@
 * @returns 返回处理后的一维路由
 */
function formatFlatteningRoutes(routesList: RouteRecordRaw[]) {
  if (routesList.length === 0) return routesList;
  if (routesList?.length === 0) return routesList;
  let hierarchyList = buildHierarchyTree(routesList);
  for (let i = 0; i < hierarchyList.length; i++) {
    if (hierarchyList[i].children) {
@@ -246,7 +246,7 @@
 * @returns 返回将一维数组重新处理成规定路由的格式
 */
function formatTwoStageRoutes(routesList: RouteRecordRaw[]) {
  if (routesList.length === 0) return routesList;
  if (routesList?.length === 0) return routesList;
  const newRoutesList: RouteRecordRaw[] = [];
  routesList.forEach((v: RouteRecordRaw) => {
    if (v.path === "/") {
@@ -387,6 +387,8 @@
    usePermissionStoreHook().wholeMenus[0]?.children[0]
  );
  tag && useMultiTagsStoreHook().handleTags("push", topMenu);
  console.log(topMenu, "topMenu");
  return topMenu;
}