zhou zhou
3 天以前 0a1d91e42e6c5af96e1108e9ebcc37e99eb3b22c
rsf-design/src/store/modules/worktab.js
@@ -2,6 +2,28 @@
import { ref, computed } from 'vue'
import { router } from '@/router'
import { useCommon } from '@/hooks/core/useCommon'
import { normalizeIcon } from '@/router/adapters/backendMenuAdapter.js'
function normalizeLegacyTabPath(path) {
  if (path === '/dashboard') {
    return '/dashboard/console'
  }
  return path
}
const normalizeTabState = (tab) => {
  if (!tab || typeof tab !== 'object') {
    return tab
  }
  return {
    ...tab,
    path: normalizeLegacyTabPath(tab.path),
    icon: normalizeIcon(tab.icon)
  }
}
const useWorktabStore = defineStore(
  'worktabStore',
  () => {
@@ -11,25 +33,30 @@
    const hasOpenedTabs = computed(() => opened.value.length > 0)
    const hasMultipleTabs = computed(() => opened.value.length > 1)
    const currentTabIndex = computed(() =>
      current.value.path ? opened.value.findIndex((tab) => tab.path === current.value.path) : -1
      current.value.path
        ? opened.value.findIndex((tab) => tab.path === normalizeLegacyTabPath(current.value.path))
        : -1
    )
    const findTabIndex = (path) => {
      return opened.value.findIndex((tab) => tab.path === path)
      const resolvedPath = normalizeLegacyTabPath(path)
      return opened.value.findIndex((tab) => tab.path === resolvedPath)
    }
    const getTab = (path) => {
      return opened.value.find((tab) => tab.path === path)
      const resolvedPath = normalizeLegacyTabPath(path)
      return opened.value.find((tab) => tab.path === resolvedPath)
    }
    const isTabClosable = (tab) => {
      return !tab.fixedTab
    }
    const safeRouterPush = (tab) => {
      if (!tab.path) {
      const resolvedPath = normalizeLegacyTabPath(tab.path)
      if (!resolvedPath) {
        console.warn('尝试跳转到无效路径的标签页')
        return
      }
      try {
        router.push({
          path: tab.path,
          path: resolvedPath,
          query: tab.query
        })
      } catch (error) {
@@ -37,7 +64,8 @@
      }
    }
    const openTab = (tab) => {
      if (!tab.path) {
      const resolvedPath = normalizeLegacyTabPath(tab.path)
      if (!resolvedPath) {
        console.warn('尝试打开无效的标签页')
        return
      }
@@ -49,11 +77,14 @@
        existingIndex = opened.value.findIndex((t) => t.name === tab.name)
      }
      if (existingIndex === -1) {
        existingIndex = findTabIndex(tab.path)
        existingIndex = findTabIndex(resolvedPath)
      }
      if (existingIndex === -1) {
        const insertIndex = tab.fixedTab ? findFixedTabInsertIndex() : opened.value.length
        const newTab = { ...tab }
        const newTab = normalizeTabState({
          ...tab,
          path: resolvedPath
        })
        if (tab.fixedTab) {
          opened.value.splice(insertIndex, 0, newTab)
        } else {
@@ -62,9 +93,9 @@
        current.value = newTab
      } else {
        const existingTab = opened.value[existingIndex]
        opened.value[existingIndex] = {
        opened.value[existingIndex] = normalizeTabState({
          ...existingTab,
          path: tab.path,
          path: resolvedPath,
          params: tab.params,
          query: tab.query,
          title: tab.title || existingTab.title,
@@ -72,7 +103,7 @@
          keepAlive: tab.keepAlive ?? existingTab.keepAlive,
          name: tab.name || existingTab.name,
          icon: tab.icon || existingTab.icon
        }
        })
        current.value = opened.value[existingIndex]
      }
    }
@@ -246,8 +277,9 @@
              if (routes.some((r) => r.name === tab.name)) return true
            }
            if (tab.path) {
              const resolvedPath = normalizeLegacyTabPath(tab.path)
              const resolved = routerInstance.resolve({
                path: tab.path,
                path: resolvedPath,
                query: tab.query || void 0
              })
              return resolved.matched.length > 0
@@ -257,15 +289,22 @@
            return false
          }
        }
        const validTabs = opened.value.filter((tab) => isTabRouteValid(tab))
        const validTabs = opened.value.filter((tab) => isTabRouteValid(tab)).map(normalizeTabState)
        if (validTabs.length !== opened.value.length) {
          console.warn('发现无效的标签页路由,已自动清理')
        }
        if (
          validTabs.length !== opened.value.length ||
          validTabs.some((tab, index) => tab.icon !== opened.value[index]?.icon)
        ) {
          opened.value = validTabs
        }
        const isCurrentValid = current.value && isTabRouteValid(current.value)
        if (!isCurrentValid && validTabs.length > 0) {
          console.warn('当前激活标签无效,已自动切换')
          current.value = validTabs[0]
        } else if (isCurrentValid) {
          current.value = normalizeTabState(current.value)
        } else if (!isCurrentValid) {
          current.value = {}
        }