feat(admin): 以固定双列替代桌面二级悬浮菜单

This commit is contained in:
2026-12-01 19:39:25 +08:00
parent e8da0054fa
commit d8a16b0778
12 changed files with 888 additions and 118 deletions
+10 -4
View File
@@ -1,17 +1,18 @@
<script setup lang="ts">
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useThrottleFn, useWindowSize } from '@vueuse/core'
import { useWindowSize } from '@vueuse/core'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { ScreenEnum } from './enums/appEnums'
import useAppStore from './stores/modules/app'
import { useAdminDark } from './stores/modules/setting'
import useSettingStore, { useAdminDark } from './stores/modules/setting'
import useUserStore from './stores/modules/user'
import ChatNotifyToast from './components/chat-notify-toast/index.vue'
import RevisitDueToast from './components/revisit-due-toast/index.vue'
const appStore = useAppStore()
const settings = useSettingStore()
// Resolve the saved appearance, system mode and palette before routes render.
useAdminDark()
const userStore = useUserStore()
@@ -31,7 +32,12 @@ const elConfig = {
const { width } = useWindowSize()
watch(
width,
useThrottleFn((value) => {
(value, previous) => {
// Resizing within one desktop breakpoint must not reopen the fixed
// navigation pane after the user explicitly closed it.
if (settings.adminUi === 'v2' && previous !== undefined &&
value > ScreenEnum.SM && previous > ScreenEnum.SM &&
(value < ScreenEnum.MD) === (previous < ScreenEnum.MD)) return
if (value > ScreenEnum.SM) {
appStore.setMobile(false)
appStore.toggleCollapsed(false)
@@ -42,7 +48,7 @@ watch(
if (value < ScreenEnum.MD) {
appStore.toggleCollapsed(true)
}
}),
},
{
immediate: true
}
@@ -0,0 +1,118 @@
<template>
<ul class="dual-branch">
<li v-for="node in nodes" :key="node.key">
<component
:is="node.children.length ? 'button' : AppLink"
:type="node.children.length ? 'button' : undefined"
:to="node.children.length ? undefined : node.target"
:rel="node.external ? 'noopener noreferrer' : undefined"
class="dual-row"
:class="{ 'is-active': node.key === leafKey }"
:style="{ '--branch-depth': depth }"
:title="node.title"
:aria-label="node.title"
:aria-current="node.key === leafKey ? 'page' : undefined"
:aria-expanded="node.children.length ? opened.includes(node.key) : undefined"
@click="node.children.length && toggle(node.key)"
>
<icon v-if="node.icon" :name="node.icon" :size="18" class="dual-row-icon" aria-hidden="true" />
<span class="dual-label">{{ node.title }}</span>
<svg v-if="node.children.length" class="dual-chevron" aria-hidden="true"
viewBox="0 0 16 16" width="14" height="14" fill="none">
<path d="m4 6 4 4 4-4" stroke="currentColor" stroke-width="1.5"
stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</component>
<dual-branch
v-if="node.children.length && opened.includes(node.key)"
:nodes="node.children"
:depth="depth + 1"
:leaf-key="leafKey"
:ancestor-keys="ancestorKeys"
:route-key="routeKey"
:unique="unique"
/>
</li>
</ul>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue'
import AppLink from '@/components/app-link/index.vue'
import type { DualNode } from './dual-model'
const props = withDefaults(defineProps<{
nodes: DualNode[]
leafKey?: string
ancestorKeys: string[]
routeKey: string
unique: boolean
depth?: number
}>(), { depth: 0 })
// Each list owns its sibling disclosures. Router changes reveal the active
// chain (including on direct load); a user can still close that chain in place.
const opened = ref<string[]>([])
watch([() => props.ancestorKeys, () => props.routeKey, () => props.unique], () => {
const ancestor = props.nodes.find(node => node.children.length && props.ancestorKeys.includes(node.key))
if (ancestor) {
if (props.unique) opened.value = [ancestor.key]
else if (!opened.value.includes(ancestor.key)) opened.value.push(ancestor.key)
} else if (props.unique) {
opened.value = []
}
}, { immediate: true })
function toggle(key: string) {
if (opened.value.includes(key)) opened.value = opened.value.filter(item => item !== key)
else if (props.unique) opened.value = [key]
else opened.value.push(key)
}
</script>
<style scoped>
.dual-branch { margin: 0; padding: 0; list-style: none; }
.dual-row {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
height: 42px;
margin-bottom: 4px;
padding-inline: calc(12px + min(3, var(--branch-depth, 0)) * 14px) 12px;
border: 0;
border-radius: 6px;
background: transparent;
color: var(--zyt-nav-text);
font-size: 14px;
line-height: 1.35;
text-align: start;
text-decoration: none;
cursor: pointer;
}
.dual-row:hover { background: var(--zyt-nav-hover); }
.dual-row.is-active {
background: var(--zyt-nav-active);
color: var(--zyt-nav-selected);
}
.dual-row-icon { flex: none; color: var(--zyt-nav-icon); }
.dual-row.is-active .dual-row-icon { color: inherit; }
.dual-label {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dual-chevron {
flex: none;
color: var(--zyt-nav-arrow);
transition: transform .16s ease;
}
.dual-row[aria-expanded='true'] > .dual-chevron { transform: rotate(180deg); }
.dual-row:focus-visible { outline: 2px solid var(--zyt-nav-selected); outline-offset: -2px; }
html:not(.dark) .side--light .dual-row.is-active { box-shadow: inset 3px 0 var(--zyt-primary); }
@media (prefers-reduced-motion: reduce) { .dual-chevron { transition: none; } }
</style>
@@ -0,0 +1,154 @@
import {
parseQuery,
type LocationQuery,
type RouteLocationNormalizedLoaded,
type RouteRecordRaw
} from 'vue-router'
import { MenuEnum } from '@/enums/appEnums'
import { getNormalPath, objectToQuery } from '@/utils/util'
import { isExternal } from '@/utils/validate'
export interface DualNode {
key: string
path: string
target: string
title: string
icon?: string
children: DualNode[]
external: boolean
}
function resolvePath(path: string, parent?: string) {
if (parent === undefined || isExternal(path)) return path
// The read-only router assigns absolute IDs even to deeply nested entries.
if (import.meta.env.MODE === 'live-readonly' && path.startsWith('/nav/')) return path
return getNormalPath(`${parent}/${path}`)
}
function menuTarget(path: string, query: unknown) {
let suffix = ''
if (typeof query === 'string' && query) {
try {
suffix = objectToQuery(JSON.parse(query))
} catch {
suffix = query
}
} else if (query && typeof query === 'object') {
suffix = objectToQuery(query)
}
suffix = suffix.replace(/^\?/, '')
return suffix ? `${path}${path.includes('?') ? '&' : '?'}${suffix}` : path
}
/** Project only the current account's supplied menu tree; permission strings are not new routes. */
export function buildDualMenu(routes: RouteRecordRaw[]): DualNode[] {
function visit(records: RouteRecordRaw[], parentPath?: string, parentKey = ''): DualNode[] {
const nodes: DualNode[] = []
const occurrences = new Map<string, number>()
for (const route of records) {
const meta = route.meta
if (meta?.hidden || meta?.type === MenuEnum.BUTTON) continue
const path = resolvePath(route.path, parentPath)
const target = menuTarget(path, meta?.query)
// Index children share their parent's path; query variants also need distinct keys.
const count = occurrences.get(target) || 0
occurrences.set(target, count + 1)
const key = `${parentKey}/${encodeURIComponent(target)}:${count}`
const children = visit(route.children || [], path, key)
// A catalogue is not an authorized landing page. A real page may have hidden details.
if (
!children.length &&
(meta?.type === MenuEnum.CATALOGUE ||
(route.children?.length && meta?.type !== MenuEnum.MENU))
)
continue
nodes.push({
key,
path,
target,
title: meta?.title || '',
icon: meta?.icon,
children,
external: isExternal(path)
})
}
return nodes
}
return visit(routes)
}
/** Entering a module must never open an external site as a side effect. */
export function firstDualLeaf(node: DualNode): DualNode | undefined {
if (!node.children.length) return node.external ? undefined : node
for (const child of node.children) {
const leaf = firstDualLeaf(child)
if (leaf) return leaf
}
}
function splitLocation(location: string) {
const href = location.split('#', 1)[0]
const q = href.indexOf('?')
const path = q < 0 ? href : href.slice(0, q)
return {
path: path.replace(/\/+$/, '') || '/',
query: q < 0 ? undefined : parseQuery(href.slice(q + 1))
}
}
// Compare decoded Vue Router values, not query-string order. Extra page filters are allowed.
function matchQuery(defaults: LocationQuery, current: LocationQuery) {
const keys = Object.keys(defaults)
const matches = keys.every((key) => {
const expected = defaults[key]
const actual = current[key]
return Array.isArray(expected)
? Array.isArray(actual) &&
expected.length === actual.length &&
expected.every((v, i) => v === actual[i])
: expected === actual
})
if (!matches) return -1
if (keys.length === Object.keys(current).length) return Number.POSITIVE_INFINITY
return keys.length
}
interface DualSelection {
root?: DualNode
leaf?: DualNode
/** Includes the root and every group to expand, never the highlighted leaf. */
ancestors: DualNode[]
}
export function findDualSelection(
roots: DualNode[],
route: RouteLocationNormalizedLoaded
): DualSelection {
const active = splitLocation(route.meta.activeMenu || route.path)
const current = active.query ?? route.query
let selection: DualSelection = { ancestors: [] }
let bestScore = Number.NEGATIVE_INFINITY
function visit(nodes: DualNode[], parents: DualNode[]) {
for (const node of nodes) {
if (node.external) continue
const groups = node.children.length ? [...parents, node] : parents
const target = splitLocation(node.target)
if (target.path === active.path) {
// Empty index leaves take precedence over grouping records at the very same path.
const score = node.children.length ? -2 : matchQuery(target.query || {}, current)
if (score > bestScore) {
bestScore = score
selection = {
root: parents[0] || node,
leaf: node.children.length ? undefined : node,
ancestors: groups
}
}
}
if (node.children.length) visit(node.children, groups)
}
}
visit(roots, [])
return selection
}
@@ -0,0 +1,222 @@
<template>
<div class="dual-sidebar" :class="{ 'dual-sidebar--compact': app.isCollapsed }"
@keydown.space="activateLinkWithSpace">
<nav class="dual-primary" aria-label="一级菜单">
<app-link v-if="settings.showLogo" class="logo dual-brand" to="/"
:aria-label="brandName ? brandName + ',返回首页' : '返回首页'" :title="brandName || '首页'">
<image-contain class="logo-mark" :height="34" :width="34" fit="contain"
:src="app.config.web_logo" alt="" />
</app-link>
<div v-else class="admin-nav-heading dual-brand" aria-hidden="true">
<svg viewBox="0 0 24 24" width="24" height="24" fill="none">
<path d="M4 6.5h10M4 12h16M4 17.5h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
<circle cx="19" cy="6.5" r="2" fill="currentColor"/>
</svg>
</div>
<ul ref="rootList" class="dual-roots">
<li v-for="root in roots" :key="root.key">
<component
:is="root.children.length ? 'button' : AppLink"
:type="root.children.length ? 'button' : undefined"
:to="root.children.length ? undefined : root.target"
:rel="root.external ? 'noopener noreferrer' : undefined"
class="dual-root"
:class="{ 'is-active': activeRoot?.key === root.key }"
:aria-expanded="root.children.length ? !app.isCollapsed && activeRoot?.key === root.key : undefined"
:aria-current="!root.children.length && selection.leaf?.key === root.key ? 'page' : undefined"
:aria-label="root.title"
:title="root.title"
@click="chooseRoot(root)"
>
<span class="dual-root-icon" aria-hidden="true">
<icon v-if="root.icon" :name="root.icon" :size="20" />
<template v-else>{{ Array.from(root.title.trim())[0] }}</template>
</span>
<span v-if="!app.isCollapsed" class="dual-root-label">{{ root.title }}</span>
</component>
</li>
</ul>
</nav>
<nav v-if="!app.isCollapsed && activeRoot?.children.length" class="dual-secondary"
aria-label="二级菜单" :style="{ width: settings.sideWidth + 'px' }">
<div class="dual-heading" :title="panelHeading">{{ panelHeading }}</div>
<div ref="branchList" class="dual-scroll" :key="activeRoot.key">
<dual-branch
:nodes="activeRoot.children"
:leaf-key="selection.leaf?.key"
:ancestor-keys="ancestorKeys"
:route-key="route.fullPath"
:unique="settings.isUniqueOpened"
/>
</div>
</nav>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter, type RouteRecordRaw } from 'vue-router'
import AppLink from '@/components/app-link/index.vue'
import useAppStore from '@/stores/modules/app'
import useSettingStore from '@/stores/modules/setting'
import DualBranch from './dual-branch.vue'
import { buildDualMenu, findDualSelection, firstDualLeaf, type DualNode } from './dual-model'
const props = defineProps<{ routes: RouteRecordRaw[] }>()
const app = useAppStore()
const settings = useSettingStore()
const router = useRouter()
const route = useRoute()
const roots = computed(() => buildDualMenu(props.routes))
const selection = computed(() => findDualSelection(roots.value, route))
const ancestorKeys = computed(() => selection.value.ancestors.map(node => node.key))
// Only an external-only directory needs a local selection: it has no internal
// destination. Never open an external site while selecting a group or syncing a route.
const externalGroup = ref<string>()
watch([() => route.fullPath, roots], () => { externalGroup.value = undefined })
const activeRoot = computed(() => externalGroup.value
? roots.value.find(node => node.key === externalGroup.value)
: selection.value.root)
const brandName = computed(() => String(app.config.web_name || ''))
const panelHeading = computed(() =>
(settings.showLogo && brandName.value) || activeRoot.value?.title || ''
)
const rootList = ref<HTMLElement>()
const branchList = ref<HTMLElement>()
function revealActiveItems() {
for (const list of [rootList.value, branchList.value]) {
const item = list?.querySelector('.is-active')
if (!list || !item) continue
const row = item.getBoundingClientRect()
const box = list.getBoundingClientRect()
if (row.top < box.top) list.scrollTop += row.top - box.top
else if (row.bottom > box.bottom) list.scrollTop += row.bottom - box.bottom
}
}
onMounted(revealActiveItems)
watch([activeRoot, selection, () => app.isCollapsed], revealActiveItems, { flush: 'post' })
function chooseRoot(node: DualNode) {
if (!node.children.length) {
if (!node.external) externalGroup.value = undefined
return // The AppLink handles normal navigation and modifier-clicks.
}
if (app.isCollapsed) app.toggleCollapsed(false)
const leaf = firstDualLeaf(node)
externalGroup.value = leaf ? undefined : node.key
if (leaf) void router.push(leaf.target)
}
function activateLinkWithSpace(event: KeyboardEvent) {
// Buttons already handle both keys natively. Extend anchors with Space
// without intercepting their Enter or modified pointer navigation.
if (!(event.target instanceof HTMLAnchorElement)) return
event.preventDefault()
if (!event.repeat) event.target.click()
}
</script>
<style scoped>
.dual-sidebar {
--rail-width: 80px;
display: flex;
flex: 1 1 0;
min-height: 0;
background: var(--zyt-nav-bg);
color: var(--zyt-nav-text);
}
.dual-sidebar--compact { --rail-width: 64px; }
.dual-primary, .dual-secondary {
display: flex;
flex: none;
flex-direction: column;
min-height: 0;
}
.dual-primary { width: var(--rail-width); }
.dual-sidebar .dual-primary > .dual-brand {
flex: none;
justify-content: center;
width: 100%;
height: var(--navbar-height);
padding: 0;
}
.dual-roots, .dual-scroll {
flex: 1;
min-height: 0;
overflow-x: hidden;
overflow-y: auto;
overscroll-behavior: contain;
scrollbar-width: thin;
}
.dual-roots {
display: flex;
flex-direction: column;
gap: 6px;
margin: 0;
padding: 4px 8px 12px;
list-style: none;
scrollbar-width: none; /* Keep icon centers aligned with the logo on scroll. */
}
.dual-roots::-webkit-scrollbar { display: none; }
.dual-root {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 5px;
width: 100%;
height: 60px;
padding: 0 2px;
border: 0;
border-radius: 5px;
background: transparent;
color: var(--zyt-nav-text);
text-align: center;
cursor: pointer;
}
.dual-sidebar--compact .dual-root { height: 46px; }
.dual-root:hover { background: var(--zyt-nav-hover); }
.dual-root.is-active {
background: var(--zyt-nav-active);
color: var(--zyt-nav-selected);
}
.dual-root-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
color: var(--zyt-nav-icon);
font-size: 16px;
line-height: 1;
}
.dual-root.is-active .dual-root-icon { color: inherit; }
.dual-root-label, .dual-heading {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dual-root-label { width: 100%; font-size: 12px; line-height: 16px; }
.dual-secondary {
box-sizing: border-box;
border-inline-start: 1px solid var(--zyt-card-border);
}
.dual-heading {
flex: none;
padding-inline: 20px;
color: var(--zyt-nav-title);
font-size: 17px;
font-weight: 600;
line-height: var(--navbar-height);
}
.dual-scroll { padding: 4px 8px 12px; }
.dual-root:focus-visible, .dual-brand:focus-visible {
outline: 2px solid var(--zyt-nav-selected);
outline-offset: -2px;
}
</style>
@@ -1,23 +1,26 @@
<template>
<div class="side" :class="['side--' + sideTheme, { 'side--collapsed': isCollapsed }]" :style="sideStyle">
<side-logo v-if="settingStore.showLogo" :show-title="!isCollapsed" :theme="sideTheme" />
<div v-else-if="settingStore.adminUi === 'v2'" class="admin-nav-heading" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none" width="24" height="24">
<path d="M4 6.5h10M4 12h16M4 17.5h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
<circle cx="19" cy="6.5" r="2" fill="currentColor"/>
</svg>
<span v-if="!isCollapsed">工作导航</span>
</div>
<side-menu
:routes="routes"
:is-collapsed="isCollapsed"
:width="settingStore.sideWidth"
:unique-opened="settingStore.isUniqueOpened"
:config="menuProp"
:theme="sideTheme"
:classic="settingStore.adminUi === 'legacy'"
@select="handleSelect"
/>
<dual-sidebar v-if="useDualSidebar" :routes="routes" />
<template v-else>
<side-logo v-if="settingStore.showLogo" :show-title="!isCollapsed" :theme="sideTheme" />
<div v-else-if="settingStore.adminUi === 'v2'" class="admin-nav-heading" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none" width="24" height="24">
<path d="M4 6.5h10M4 12h16M4 17.5h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
<circle cx="19" cy="6.5" r="2" fill="currentColor"/>
</svg>
<span v-if="!isCollapsed">工作导航</span>
</div>
<side-menu
:routes="routes"
:is-collapsed="isCollapsed"
:width="settingStore.sideWidth"
:unique-opened="settingStore.isUniqueOpened"
:config="menuProp"
:theme="sideTheme"
:classic="settingStore.adminUi === 'legacy'"
@select="handleSelect"
/>
</template>
</div>
</template>
@@ -26,6 +29,7 @@ import useAppStore from '@/stores/modules/app'
import useSettingStore from '@/stores/modules/setting'
import useUserStore from '@/stores/modules/user'
import DualSidebar from './dual-sidebar.vue'
import SideLogo from './logo.vue'
import SideMenu from './menu.vue'
@@ -39,6 +43,11 @@ const isCollapsed = computed(() => {
})
const settingStore = useSettingStore()
// The fixture and read-only preview clients retain their own menu contracts.
const useDualSidebar = computed(() =>
settingStore.adminUi === 'v2' && !appStore.isMobile &&
!['live-readonly', 'local-preview'].includes(import.meta.env.MODE)
)
const sideTheme = computed(() => settingStore.sideTheme)
const userStore = useUserStore()
+4 -1
View File
@@ -33,7 +33,10 @@ const getHasTabIndex = (fullPath: string, tabList: TabItem[]) => {
/** 同一菜单页可能因 query、重定向等产生不同 fullPath,用 name + 解析后的 path 去重 */
const getTabMergeKey = (item: Pick<TabItem, 'name' | 'path'>) => {
if (item.name == null || item.name === '') return ''
return `${String(item.name)}::${item.path}`
// Router resolves an index child both with and without its parent's final
// slash. Only normalize the tab identity, never the navigation URL.
const path = item.path.replace(/\/+$/, '') || '/'
return `${String(item.name)}::${path}`
}
const isCannotAddRoute = (route: RouteLocationNormalized, router: Router) => {