This commit is contained in:
2026-05-20 13:42:46 +08:00
parent 9d2a481072
commit 567b9d0519
8 changed files with 734 additions and 345 deletions

View File

@ -35,6 +35,7 @@
"vue": "^3.5.29", "vue": "^3.5.29",
"vue-i18n": "^11.4.2", "vue-i18n": "^11.4.2",
"vue-router": "^5.0.3", "vue-router": "^5.0.3",
"vuedraggable": "^4.1.0",
"xlsx": "^0.18.5" "xlsx": "^0.18.5"
}, },
"devDependencies": { "devDependencies": {

View File

@ -65,6 +65,9 @@ importers:
vue-router: vue-router:
specifier: ^5.0.3 specifier: ^5.0.3
version: 5.0.3(@vue/compiler-sfc@3.5.29)(pinia@3.0.4(typescript@5.9.3)(vue@3.5.29(typescript@5.9.3)))(vue@3.5.29(typescript@5.9.3)) version: 5.0.3(@vue/compiler-sfc@3.5.29)(pinia@3.0.4(typescript@5.9.3)(vue@3.5.29(typescript@5.9.3)))(vue@3.5.29(typescript@5.9.3))
vuedraggable:
specifier: ^4.1.0
version: 4.1.0(vue@3.5.29(typescript@5.9.3))
xlsx: xlsx:
specifier: ^0.18.5 specifier: ^0.18.5
version: 0.18.5 version: 0.18.5
@ -2398,6 +2401,9 @@ packages:
resolution: {integrity: sha512-LJhUYUvItdQ0LkJTmPeaEObWXAqFyfmP85x0tch/ez9cahmhlBBLbIqDFnvBnUJGagb0JbIQrkBs1wJ+yRYpEw==} resolution: {integrity: sha512-LJhUYUvItdQ0LkJTmPeaEObWXAqFyfmP85x0tch/ez9cahmhlBBLbIqDFnvBnUJGagb0JbIQrkBs1wJ+yRYpEw==}
engines: {node: '>= 10.0.0', npm: '>= 3.0.0'} engines: {node: '>= 10.0.0', npm: '>= 3.0.0'}
sortablejs@1.14.0:
resolution: {integrity: sha512-pBXvQCs5/33fdN1/39pPL0NZF20LeRbLQ5jtnheIPN9JQAaufGjKdWduZn4U7wCtVuzKhmRkI0DFYHYRbB2H1w==}
source-map-js@1.2.1: source-map-js@1.2.1:
resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@ -2666,6 +2672,11 @@ packages:
typescript: typescript:
optional: true optional: true
vuedraggable@4.1.0:
resolution: {integrity: sha512-FU5HCWBmsf20GpP3eudURW3WdWTKIbEIQxh9/8GE806hydR9qZqRRxRE3RjqX7PkuLuMQG/A7n3cfj9rCEchww==}
peerDependencies:
vue: ^3.0.1
webpack-virtual-modules@0.6.2: webpack-virtual-modules@0.6.2:
resolution: {integrity: sha512-66/V2i5hQanC51vBQKPH4aI8NMAcBW59FVBs+rC7eGHupMyfn34q7rZIE+ETlJ+XTevqfUhVVBgSUNSW2flEUQ==} resolution: {integrity: sha512-66/V2i5hQanC51vBQKPH4aI8NMAcBW59FVBs+rC7eGHupMyfn34q7rZIE+ETlJ+XTevqfUhVVBgSUNSW2flEUQ==}
@ -4957,6 +4968,8 @@ snapshots:
ip-address: 10.2.0 ip-address: 10.2.0
smart-buffer: 4.2.0 smart-buffer: 4.2.0
sortablejs@1.14.0: {}
source-map-js@1.2.1: {} source-map-js@1.2.1: {}
speakingurl@14.0.1: {} speakingurl@14.0.1: {}
@ -5243,6 +5256,11 @@ snapshots:
optionalDependencies: optionalDependencies:
typescript: 5.9.3 typescript: 5.9.3
vuedraggable@4.1.0(vue@3.5.29(typescript@5.9.3)):
dependencies:
sortablejs: 1.14.0
vue: 3.5.29(typescript@5.9.3)
webpack-virtual-modules@0.6.2: {} webpack-virtual-modules@0.6.2: {}
which-module@2.0.1: {} which-module@2.0.1: {}

View File

@ -28,16 +28,17 @@ const breadcrumbList = computed<BreadcrumbItem[]>(() => tabsStore.currentBreadcr
<style scoped lang="scss"> <style scoped lang="scss">
.breadcrumb-container { .breadcrumb-container {
padding: 12px 16px; display: flex;
background: #fff; align-items: center;
border-bottom: 1px solid var(--el-border-color-light); height: 100%;
:deep(.el-breadcrumb) { :deep(.el-breadcrumb) {
font-size: 14px; font-size: 14px;
white-space: nowrap;
.el-breadcrumb__item { .el-breadcrumb__item {
.el-breadcrumb__inner { .el-breadcrumb__inner {
color: #606266; color: var(--el-text-color-regular);
font-weight: normal; font-weight: normal;
&:hover { &:hover {
@ -45,17 +46,17 @@ const breadcrumbList = computed<BreadcrumbItem[]>(() => tabsStore.currentBreadcr
} }
&.is-link { &.is-link {
color: #606266; color: var(--el-text-color-regular);
} }
} }
&:last-child { &:last-child {
.el-breadcrumb__inner { .el-breadcrumb__inner {
color: #909399; color: var(--el-text-color-primary);
cursor: default; cursor: default;
&:hover { &:hover {
color: #909399; color: var(--el-text-color-primary);
} }
} }
} }

View File

@ -1,18 +1,30 @@
<template> <template>
<header class="layout-header"> <header class="layout-header">
<!-- 左侧Logo 和系统名称 --> <!-- 左侧Logo系统名称和菜单收缩按钮 -->
<div class="header-left"> <div class="header-left">
<div class="logo"> <div class="logo-section">
<img src="@/assets/images/logo.webp" alt="logo" /> <img src="@/assets/images/logo.webp" alt="logo" class="logo-img" />
<span class="system-name">Admin System</span> <span class="system-name">Admin System</span>
<!-- 菜单收缩按钮 - 在标题右边 -->
<el-button
class="collapse-btn"
:icon="sidebarCollapsed ? Expand : Fold"
@click="toggleSidebar"
text
/>
</div> </div>
</div> </div>
<!-- 中间面包屑导航 -->
<div class="header-center">
<Breadcrumb />
</div>
<!-- 右侧功能按钮和用户信息 --> <!-- 右侧功能按钮和用户信息 -->
<div class="header-right"> <div class="header-right">
<!-- 全屏 --> <!-- 全屏 -->
<el-tooltip :content="isFullscreen ? '退出全屏' : '全屏'"> <el-tooltip :content="isFullscreen ? '退出全屏' : '全屏'">
<el-button :icon="isFullscreen ? Crop : FullScreen" circle @click="toggleFullscreen" /> <el-button :icon="isFullscreen ? Crop : FullScreen" text @click="toggleFullscreen" />
</el-tooltip> </el-tooltip>
<!-- 语言选择 --> <!-- 语言选择 -->
@ -21,8 +33,8 @@
<!-- 用户信息下拉菜单 --> <!-- 用户信息下拉菜单 -->
<el-dropdown @command="handleCommand" class="user-dropdown"> <el-dropdown @command="handleCommand" class="user-dropdown">
<div class="user-info"> <div class="user-info">
<el-avatar :size="32" class="user-avatar"> <el-avatar :size="28" class="user-avatar">
<el-icon :size="20"><User /></el-icon> <el-icon :size="16"><User /></el-icon>
</el-avatar> </el-avatar>
<span class="username">{{ activeUserName }}</span> <span class="username">{{ activeUserName }}</span>
<el-icon class="dropdown-icon"><ArrowDown /></el-icon> <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
@ -51,28 +63,38 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue' import { computed } from 'vue'
import { import {
Bell,
Sunny,
Moon,
FullScreen, FullScreen,
Crop, Crop,
User, User,
Lock, Lock,
SwitchButton, SwitchButton,
ArrowDown ArrowDown,
Fold,
Expand
} from '@element-plus/icons-vue' } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/modules/user' import { useUserStore } from '@/stores/modules/user'
import { useLanguageStore } from '@/stores/modules/language' import { useLanguageStore } from '@/stores/modules/language'
import LanguageSelect from '@/components/LanguageSelect/index.vue' import LanguageSelect from '@/components/LanguageSelect/index.vue'
import Breadcrumb from './Breadcrumb.vue'
import { getStorage } from '@/utils/storage' import { getStorage } from '@/utils/storage'
const emit = defineEmits(['toggle-sidebar'])
const router = useRouter() const router = useRouter()
const userStore = useUserStore() const userStore = useUserStore()
const languageStore = useLanguageStore() const languageStore = useLanguageStore()
//
const sidebarCollapsed = ref(false)
//
const toggleSidebar = () => {
sidebarCollapsed.value = !sidebarCollapsed.value
emit('toggle-sidebar', sidebarCollapsed.value)
}
// - 使 userStore // - 使 userStore
const activeUserName = computed(() => userStore.userInfo?.username || '-') const activeUserName = computed(() => userStore.userInfo?.username || '-')
const unreadCount = ref(3) //
// //
const isFullscreen = ref(false) const isFullscreen = ref(false)
@ -95,12 +117,6 @@ onMounted(() => {
}) })
}) })
//
const handleNotification = () => {
ElMessage.info('暂无新消息')
unreadCount.value = 0
}
// //
const handleCommand = (command: string) => { const handleCommand = (command: string) => {
switch (command) { switch (command) {
@ -133,13 +149,13 @@ const handleLogout = () => {
<style scoped lang="scss"> <style scoped lang="scss">
.layout-header { .layout-header {
height: 60px; height: 50px;
background: #fff; background: #fff;
border-bottom: 1px solid var(--el-border-color-light); border-bottom: 1px solid var(--el-border-color-light);
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
padding: 0 20px; padding: 0 16px;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
position: fixed; position: fixed;
top: 0; top: 0;
@ -150,58 +166,75 @@ const handleLogout = () => {
.header-left { .header-left {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 20px; width: 200px;
flex-shrink: 0;
.logo { .logo-section {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 10px; gap: 8px;
width: 100%;
img { .logo-img {
height: 32px; height: 28px;
width: auto; width: auto;
flex-shrink: 0;
} }
.system-name { .system-name {
font-size: 18px; font-size: 8px;
font-weight: 600; font-weight: 600;
color: var(--el-text-color-primary); color: var(--el-text-color-primary);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
flex: 1;
}
.collapse-btn {
padding: 4px 8px;
color: var(--el-text-color-regular);
flex-shrink: 0;
margin-left: auto;
&:hover {
color: var(--el-color-primary);
} }
} }
} }
}
.header-center {
flex: 1;
display: flex;
// justify-content: center;
padding: 0 20px;
}
.header-right { .header-right {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 12px; gap: 8px;
min-width: 200px;
.notification-badge { justify-content: flex-end;
:deep(.el-badge__content) {
transform: translateY(-50%) translateX(50%);
}
}
.el-button { .el-button {
border: none; padding: 4px 8px;
background: transparent;
padding: 8px;
color: var(--el-text-color-regular); color: var(--el-text-color-regular);
&:hover { &:hover {
background: var(--el-fill-color-light);
color: var(--el-color-primary); color: var(--el-color-primary);
} }
} }
.user-dropdown { .user-dropdown {
margin-left: 8px;
cursor: pointer; cursor: pointer;
.user-info { .user-info {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 8px; gap: 6px;
padding: 4px 8px; padding: 2px 8px;
border-radius: 4px; border-radius: 4px;
transition: background 0.3s; transition: background 0.3s;
@ -214,9 +247,9 @@ const handleLogout = () => {
} }
.username { .username {
font-size: 14px; font-size: 13px;
color: var(--el-text-color-primary); color: var(--el-text-color-primary);
max-width: 100px; max-width: 80px;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
white-space: nowrap; white-space: nowrap;

View File

@ -1,18 +1,15 @@
<template> <template>
<section class="layout"> <section class="layout">
<!-- 顶部导航栏固定在顶部 --> <!-- 顶部导航栏固定在顶部 -->
<Header /> <Header @toggle-sidebar="handleToggleSidebar" />
<!-- 主体内容区域 --> <!-- 主体内容区域 -->
<section class="main-container"> <section class="main-container" :class="{ 'is-collapsed': sidebarCollapsed }">
<!-- 左侧侧边菜单固定在左侧 --> <!-- 左侧侧边菜单固定在左侧 -->
<Sidebar /> <Sidebar :collapsed="sidebarCollapsed" />
<!-- 右侧内容区面包屑 + 页签 + 主体内容 --> <!-- 右侧内容区页签 + 主体内容 -->
<section class="content-container"> <section class="content-container">
<!-- 面包屑导航 -->
<!-- <Breadcrumb />-->
<!-- 页签标签栏 --> <!-- 页签标签栏 -->
<!-- <Tabs /> --> <!-- <Tabs /> -->
@ -42,11 +39,17 @@ defineOptions({
}) })
import Header from './Header.vue' import Header from './Header.vue'
import Sidebar from './Sidebar.vue' import Sidebar from './Sidebar.vue'
// import Breadcrumb from './Breadcrumb.vue' import Tabs from './Tabs.vue'
// import Tabs from './Tabs.vue'
const route = useRoute() const route = useRoute()
console.log('route.path', route)
//
const sidebarCollapsed = ref(false)
//
const handleToggleSidebar = (collapsed: boolean) => {
sidebarCollapsed.value = collapsed
}
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
@ -58,8 +61,15 @@ console.log('route.path', route)
.main-container { .main-container {
display: flex; display: flex;
height: calc(100vh - 60px); height: calc(100vh - 50px);
margin-top: 60px; margin-top: 50px;
transition: margin-left 0.3s ease;
&.is-collapsed {
.content-container {
margin-left: 64px;
}
}
.content-container { .content-container {
flex: 1; flex: 1;
@ -67,6 +77,7 @@ console.log('route.path', route)
flex-direction: column; flex-direction: column;
overflow: hidden; overflow: hidden;
margin-left: 200px; margin-left: 200px;
transition: margin-left 0.3s ease;
.main-content { .main-content {
flex: 1; flex: 1;

View File

@ -1,10 +1,11 @@
<template> <template>
<aside class="sidebar"> <aside class="sidebar" :class="{ 'is-collapsed': collapsed }">
<!-- 菜单区域 --> <!-- 菜单区域 -->
<el-menu <el-menu
class="sidebar-menu" class="sidebar-menu"
:unique-opened="true" :unique-opened="true"
:default-active="activeMenu" :default-active="activeMenu"
:collapse="collapsed"
router router
> >
<template v-for="menu in menuList" :key="menu.id"> <template v-for="menu in menuList" :key="menu.id">
@ -50,6 +51,10 @@ import {
} from '@element-plus/icons-vue' } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/modules/user' import { useUserStore } from '@/stores/modules/user'
defineProps<{
collapsed?: boolean
}>()
const route = useRoute() const route = useRoute()
const userStore = useUserStore() const userStore = useUserStore()
@ -88,12 +93,31 @@ const iconMap: Record<string, any> = {
overflow-y: auto; overflow-y: auto;
overflow-x: hidden; overflow-x: hidden;
z-index: 999; z-index: 999;
transition: width 0.3s ease;
&.is-collapsed {
width: 64px;
:deep(.el-menu--collapse) {
width: 64px;
.el-sub-menu__title,
.el-menu-item {
padding-left: 20px !important;
padding-right: 20px !important;
}
}
}
.sidebar-menu { .sidebar-menu {
height: 100%; height: 100%;
border: none; border: none;
background: transparent; background: transparent;
&:not(.el-menu--collapse) {
width: 200px;
}
:deep(.el-menu-item) { :deep(.el-menu-item) {
height: 50px; height: 50px;
line-height: 50px; line-height: 50px;

View File

@ -1,52 +1,180 @@
<template> <template>
<div class="tabs-container"> <div class="tabs-container">
<el-tabs v-model="tabsStore.activeTab" type="card" closable @tab-click="handleTabClick" <!-- 左侧滚动按钮 -->
@tab-remove="handleTabRemove"> <button class="scroll-btn scroll-btn-left" @click="scrollLeft" :class="{ 'is-disabled': !canScrollLeft }">
<el-tab-pane v-for="(tab, index) in tabsStore.tabs" :key="tab.path" :label="tab.title" :name="tab.path"> <el-icon><ArrowLeft /></el-icon>
<template #label> </button>
<div class="tab-label" :class="{ 'is-context-active': contextMenuPath === tab.path }" draggable="true"
@dragstart="handleDragStart($event, index)" @dragover.prevent="handleDragOver($event, index)" <!-- 标签列表区域 -->
@drop="handleDrop($event, index)" @dragend="handleDragEnd" <div class="tabs-scroll-area" ref="tabsContainerRef">
@contextmenu.prevent="handleContextMenu($event, tab.path)"> <draggable
v-model="tabsList"
item-key="path"
class="tabs-list"
:animation="300"
ghost-class="ghost"
chosen-class="chosen"
drag-class="drag"
handle=".tab-label"
@end="handleDragEnd"
>
<template #item="{ element: tab }">
<div
class="tab-item"
:class="{ 'is-active': tabsStore.activeTab === tab.path }"
@click="handleTabClick(tab)"
@contextmenu.prevent="handleContextMenu($event, tab.path)"
>
<div class="tab-label">
<span class="tab-text">{{ tab.title }}</span> <span class="tab-text">{{ tab.title }}</span>
</div> </div>
<span
v-if="tab.path !== '/dashboard'"
class="tab-close"
@click.stop="handleTabRemove(tab.path)"
>
<el-icon><Close /></el-icon>
</span>
</div>
</template> </template>
</el-tab-pane> </draggable>
</el-tabs> </div>
<!-- 右键菜单 --> <!-- 右侧滚动按钮 -->
<el-dropdown popper-class="el-droMenu" :popper-style="style" ref="contextMenuRef" trigger="contextmenu" <button class="scroll-btn scroll-btn-right" @click="scrollRight" :class="{ 'is-disabled': !canScrollRight }">
@command="handleContextMenuCommand" @visible-change="handleMenuVisibleChange"> <el-icon><ArrowRight /></el-icon>
<span class="context-menu-trigger"></span> </button>
<!-- 当前页面下拉菜单 -->
<el-dropdown trigger="click" @command="handleDropdownCommand" placement="bottom-end" ref="pageDropdownRef">
<button class="current-page-btn" @click.stop="togglePageDropdown">
<el-icon><MoreFilled /></el-icon>
</button>
<template #dropdown> <template #dropdown>
<el-dropdown-menu> <div class="dropdown-header">已打开的页面</div>
<el-dropdown-item command="refresh">刷新当前页</el-dropdown-item> <el-dropdown-menu class="page-dropdown-menu">
<el-dropdown-item command="close">关闭当前</el-dropdown-item> <el-dropdown-item
<el-dropdown-item command="closeOther">关闭其他</el-dropdown-item> v-for="tab in tabsStore.tabs"
<el-dropdown-item command="closeLeft">关闭左侧</el-dropdown-item> :key="tab.path"
<el-dropdown-item command="closeRight">关闭右侧</el-dropdown-item> :command="tab.path"
<el-dropdown-item command="closeAll" divided>关闭全部</el-dropdown-item> :class="{ 'is-active-item': tabsStore.activeTab === tab.path }"
>
<span class="dropdown-item-text">{{ tab.title }}</span>
<el-icon
v-if="tab.path !== '/dashboard'"
class="dropdown-item-close"
@click.stop="handleTabRemove(tab.path)"
>
<Close />
</el-icon>
</el-dropdown-item>
</el-dropdown-menu> </el-dropdown-menu>
</template> </template>
</el-dropdown> </el-dropdown>
<!-- 右键菜单 -->
<teleport to="body">
<div
v-if="contextMenuVisible"
class="context-menu-dropdown"
:style="contextMenuStyle"
@click.stop
>
<div class="context-menu-item" @click="handleContextMenuCommand('refresh')">
<el-icon><Refresh /></el-icon>
<span>刷新当前页</span>
</div>
<div class="context-menu-item" @click="handleContextMenuCommand('close')">
<el-icon><Close /></el-icon>
<span>关闭当前</span>
</div>
<div class="context-menu-item" @click="handleContextMenuCommand('closeOther')">
<el-icon><Switch /></el-icon>
<span>关闭其他</span>
</div>
<div class="context-menu-item" @click="handleContextMenuCommand('closeLeft')">
<el-icon><ArrowLeft /></el-icon>
<span>关闭左侧</span>
</div>
<div class="context-menu-item" @click="handleContextMenuCommand('closeRight')">
<el-icon><ArrowRight /></el-icon>
<span>关闭右侧</span>
</div>
<div class="context-menu-divider"></div>
<div class="context-menu-item" @click="handleContextMenuCommand('closeAll')">
<el-icon><FolderRemove /></el-icon>
<span>关闭全部</span>
</div>
</div>
</teleport>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useTabsStore } from '@/stores/modules/tabs' import { useTabsStore } from '@/stores/modules/tabs'
import type { TabItem } from '@/stores/modules/tabs' import type { TabItem } from '@/stores/modules/tabs'
import draggable from 'vuedraggable'
import { Close, Refresh, Switch, ArrowLeft, ArrowRight, FolderRemove, MoreFilled } from '@element-plus/icons-vue'
const router = useRouter() const router = useRouter()
const route = useRoute() const route = useRoute()
const tabsStore = useTabsStore() const tabsStore = useTabsStore()
const contextMenuRef = ref() const tabsContainerRef = ref<HTMLElement>()
const pageDropdownRef = ref()
// //
let contextMenuPath = '' const contextMenuVisible = ref(false)
const contextMenuX = ref(0)
const contextMenuY = ref(0)
const contextMenuPath = ref('')
// //
let dragIndex: number | null = null const canScrollLeft = ref(false)
let dropIndex: number | null = null const canScrollRight = ref(false)
//
const contextMenuStyle = computed(() => ({
left: contextMenuX.value + 'px',
top: contextMenuY.value + 'px'
}))
// tabs
const tabsList = computed({
get: () => tabsStore.tabs,
set: (val) => tabsStore.setTabs(val)
})
//
const checkScroll = () => {
if (tabsContainerRef.value) {
const { scrollLeft, scrollWidth, clientWidth } = tabsContainerRef.value
canScrollLeft.value = scrollLeft > 0
canScrollRight.value = scrollLeft + clientWidth < scrollWidth - 1
}
}
//
const scrollLeft = () => {
if (tabsContainerRef.value && canScrollLeft.value) {
tabsContainerRef.value.scrollBy({ left: -200, behavior: 'smooth' })
setTimeout(checkScroll, 300)
}
}
//
const scrollRight = () => {
if (tabsContainerRef.value && canScrollRight.value) {
tabsContainerRef.value.scrollBy({ left: 200, behavior: 'smooth' })
setTimeout(checkScroll, 300)
}
}
//
const togglePageDropdown = () => {
if (pageDropdownRef.value) {
pageDropdownRef.value.handleOpen()
}
}
// //
onMounted(() => { onMounted(() => {
@ -57,28 +185,68 @@ onMounted(() => {
router.replace({ path: targetTab.path, query: targetTab.query }) router.replace({ path: targetTab.path, query: targetTab.query })
} }
} }
//
nextTick(checkScroll)
//
tabsContainerRef.value?.addEventListener('scroll', checkScroll)
//
document.addEventListener('click', handleGlobalClick)
// tab
document.addEventListener('contextmenu', handleGlobalContextMenu)
}) })
onUnmounted(() => {
tabsContainerRef.value?.removeEventListener('scroll', checkScroll)
document.removeEventListener('click', handleGlobalClick)
document.removeEventListener('contextmenu', handleGlobalContextMenu)
})
// -
const handleGlobalClick = (e: MouseEvent) => {
closeContextMenu()
closePageDropdown()
}
// -
const handleGlobalContextMenu = (e: MouseEvent) => {
// tabs
const target = e.target as HTMLElement
const tabItem = target.closest('.tab-item')
// tab
if (!tabItem) {
closeContextMenu()
}
}
// //
watch( watch(
() => route.fullPath, () => route.fullPath,
(newPath) => { () => {
if (route.meta?.title && !route.meta?.isAuthPage) { if (route.meta?.title && !route.meta?.isAuthPage) {
tabsStore.addTab({ tabsStore.addTab({
title: route.meta.title as string, title: route.meta.title as string,
path: route.path, path: route.path,
query: route.query query: route.query
}) })
//
nextTick(checkScroll)
} }
}, },
{ immediate: true } { immediate: true }
) )
// //
const handleTabClick = (tab: any) => { const handleTabClick = (tab: TabItem) => {
const targetTab = tabsStore.tabs.find(item => item.path === tab.paneName) //
if (targetTab) { closeContextMenu()
router.push({ path: targetTab.path, query: targetTab.query })
tabsStore.setActiveTab(tab.path)
if (tab.path !== route.path) {
router.push({ path: tab.path, query: tab.query })
} }
} }
@ -92,11 +260,32 @@ const handleTabRemove = (path: any) => {
router.push({ path: targetTab.path, query: targetTab.query }) router.push({ path: targetTab.path, query: targetTab.query })
} }
} }
//
nextTick(checkScroll)
}
//
const handleDropdownCommand = (path: string) => {
handleTabClick({ path, title: '', query: {} })
}
//
const closePageDropdown = () => {
if (pageDropdownRef.value) {
pageDropdownRef.value.handleClose()
}
}
//
const handleDragEnd = () => {
// tabsStore.setTabs computed
} }
// //
const handleContextMenuCommand = (command: string) => { const handleContextMenuCommand = (command: string) => {
const currentPath = contextMenuPath || tabsStore.activeTab // 使
const currentPath = contextMenuPath.value || tabsStore.activeTab
closeContextMenu()
switch (command) { switch (command) {
case 'refresh': case 'refresh':
@ -105,21 +294,63 @@ const handleContextMenuCommand = (command: string) => {
case 'close': case 'close':
if (currentPath) { if (currentPath) {
handleTabRemove(currentPath) handleTabRemove(currentPath)
//
if (currentPath === route.path && tabsStore.activeTab) {
const newTab = tabsStore.tabs.find(item => item.path === tabsStore.activeTab)
if (newTab) {
router.push({ path: newTab.path, query: newTab.query })
}
}
} }
break break
case 'closeOther': case 'closeOther':
if (currentPath) { if (currentPath) {
tabsStore.closeOtherTabs(currentPath) tabsStore.closeOtherTabs(currentPath)
//
if (currentPath !== tabsStore.activeTab && tabsStore.activeTab) {
const newTab = tabsStore.tabs.find(item => item.path === tabsStore.activeTab)
if (newTab) {
router.push({ path: newTab.path, query: newTab.query })
}
}
} }
break break
case 'closeLeft': case 'closeLeft':
if (currentPath) { if (currentPath) {
//
const currentIndex = tabsStore.tabs.findIndex(item => item.path === route.path)
const rightClickIndex = tabsStore.tabs.findIndex(item => item.path === currentPath)
//
const willCloseCurrentPage = currentIndex > rightClickIndex
tabsStore.closeLeftTabs(currentPath) tabsStore.closeLeftTabs(currentPath)
//
if (willCloseCurrentPage) {
const targetTab = tabsStore.tabs.find(item => item.path === currentPath)
if (targetTab) {
router.push({ path: targetTab.path, query: targetTab.query })
}
}
} }
break break
case 'closeRight': case 'closeRight':
if (currentPath) { if (currentPath) {
//
const currentIndex = tabsStore.tabs.findIndex(item => item.path === route.path)
const rightClickIndex = tabsStore.tabs.findIndex(item => item.path === currentPath)
//
const willCloseCurrentPage = currentIndex < rightClickIndex
tabsStore.closeRightTabs(currentPath) tabsStore.closeRightTabs(currentPath)
//
if (willCloseCurrentPage) {
const targetTab = tabsStore.tabs.find(item => item.path === currentPath)
if (targetTab) {
router.push({ path: targetTab.path, query: targetTab.query })
}
}
} }
break break
case 'closeAll': case 'closeAll':
@ -129,152 +360,184 @@ const handleContextMenuCommand = (command: string) => {
} }
} }
// / // -
const handleMenuVisibleChange = (visible: boolean) => {
if (!visible) {
contextMenuPath = ''
}
}
const style = ref('')
//
const handleContextMenu = (e: MouseEvent, path: string) => { const handleContextMenu = (e: MouseEvent, path: string) => {
contextMenuPath = path
//
tabsStore.setActiveTab(path)
const targetTab = tabsStore.tabs.find(item => item.path === path)
if (targetTab && path !== route.path) {
router.push({ path: targetTab.path, query: targetTab.query })
}
// dropdown
const dropdown = contextMenuRef.value
if (dropdown) {
// dropdown
const dropdownInstance = dropdown
//
const x = e.clientX
const y = e.clientY
// 使 Element Plus
dropdownInstance?.handleOpen()
//
nextTick().then(() => {
const menu = document.querySelector('.el-droMenu')
if (menu) {
style.value = `position: fixed; z-index: 9999; left: ${x}px; top: ${y}px;`
}
})
}
}
//
const handleDragStart = (e: DragEvent, index: number) => {
dragIndex = index
if (e.dataTransfer) {
e.dataTransfer.effectAllowed = 'move'
e.dataTransfer.setData('text/plain', index.toString())
}
//
const target = e.target as HTMLElement
target.classList.add('dragging')
}
//
const handleDragOver = (e: DragEvent, index: number) => {
e.preventDefault() e.preventDefault()
if (e.dataTransfer) { e.stopPropagation()
e.dataTransfer.dropEffect = 'move'
} //
dropIndex = index closePageDropdown()
//
contextMenuPath.value = path
//
const menuWidth = 160
const menuHeight = 240
let x = e.clientX
let y = e.clientY
//
if (x + menuWidth > window.innerWidth) {
x = window.innerWidth - menuWidth - 10
} }
// //
const handleDragEnd = (e: DragEvent) => { if (y + menuHeight > window.innerHeight) {
const target = e.target as HTMLElement y = window.innerHeight - menuHeight - 10
target.classList.remove('dragging')
dragIndex = null
dropIndex = null
} }
// //
const handleDrop = (e: DragEvent, index: number) => { x = Math.max(10, x)
e.preventDefault() y = Math.max(10, y)
if (dragIndex === null || dragIndex === index) {
return contextMenuX.value = x
contextMenuY.value = y
contextMenuVisible.value = true
} }
// tabs //
const newTabs = [...tabsStore.tabs] const closeContextMenu = () => {
const [draggedTab] = newTabs.splice(dragIndex, 1) contextMenuVisible.value = false
if (draggedTab) { contextMenuPath.value = ''
newTabs.splice(index, 0, draggedTab)
// store
tabsStore.setTabs(newTabs)
}
//
const target = e.target as HTMLElement
target.classList.remove('dragging')
} }
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.tabs-container { .tabs-container {
height: 50px; height: 42px;
box-sizing: content-box; box-sizing: content-box;
background: linear-gradient(180deg, #ffffff 0%, #fafbfc 100%); background: var(--el-bg-color);
border-bottom: 1px solid var(--el-border-color-light); border-bottom: 1px solid var(--el-border-color-light);
padding: 8px 20px 0; padding: 0 40px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
position: relative; position: relative;
z-index: 10; z-index: 10;
display: flex;
align-items: center;
:deep(.el-tabs) { .scroll-btn {
.el-tabs__header { position: absolute;
margin: 0; top: 50%;
border: none; transform: translateY(-50%);
width: 28px;
height: 28px;
display: flex;
align-items: center;
justify-content: center;
background: var(--el-fill-color-light);
border: 1px solid var(--el-border-color-light);
border-radius: 4px;
cursor: pointer;
color: var(--el-text-color-secondary);
transition: all 0.2s ease;
z-index: 5;
&:hover:not(.is-disabled) {
background: var(--el-fill-color);
color: var(--el-color-primary);
border-color: var(--el-color-primary-light-5);
} }
.el-tabs__nav-scroll { &.is-disabled {
overflow: visible !important; opacity: 0.4;
cursor: not-allowed;
} }
.el-tabs__nav { &.scroll-btn-left {
border: none !important; left: 6px;
gap: 4px;
} }
.el-tabs__item { &.scroll-btn-right {
border: 1px solid var(--el-border-color-lighter) !important; right: 66px;
border-bottom: none !important; }
margin-right: 0; }
border-radius: 8px 8px 0 0;
background: linear-gradient(180deg, #f8f9fb 0%, #f0f2f5 100%); .tabs-scroll-area {
color: #5a5e66; flex: 1;
height: 38px; height: 100%;
line-height: 38px; overflow-x: auto;
padding: 0 18px; overflow-y: hidden;
scrollbar-width: none;
-ms-overflow-style: none;
&::-webkit-scrollbar {
display: none;
}
}
.tabs-list {
display: flex;
align-items: center;
height: 100%;
gap: 2px;
min-width: max-content;
}
.tab-item {
display: flex;
align-items: center;
height: 32px;
padding: 0 12px;
background: var(--el-fill-color-light);
border: 1px solid var(--el-border-color-light);
border-bottom: none;
border-radius: 4px 4px 0 0;
color: var(--el-text-color-regular);
font-size: 13px; font-size: 13px;
cursor: pointer; cursor: pointer;
user-select: none; user-select: none;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); transition: all 0.2s ease;
position: relative; white-space: nowrap;
overflow: hidden; flex-shrink: 0;
// .tab-label {
&::before { display: flex;
content: ''; align-items: center;
position: absolute; cursor: grab;
top: 0;
left: 0; &:active {
right: 0; cursor: grabbing;
height: 3px; }
background: linear-gradient(90deg, var(--el-color-primary), var(--el-color-primary-light-3));
transform: scaleX(0);
transition: transform 0.3s ease;
border-radius: 8px 8px 0 0;
} }
// .tab-text {
max-width: 120px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.tab-close {
display: flex;
align-items: center;
justify-content: center;
margin-left: 6px;
width: 16px;
height: 16px;
border-radius: 50%;
font-size: 12px;
opacity: 0.6;
transition: all 0.2s ease;
&:hover {
opacity: 1;
background: var(--el-color-danger-light-9);
color: var(--el-color-danger);
}
}
&:hover {
background: var(--el-fill-color);
color: var(--el-text-color-primary);
}
&.is-active {
background: #fff;
color: var(--el-color-primary);
border-color: var(--el-border-color);
font-weight: 500;
position: relative;
&::after { &::after {
content: ''; content: '';
position: absolute; position: absolute;
@ -283,113 +546,134 @@ const handleDrop = (e: DragEvent, index: number) => {
right: 0; right: 0;
height: 2px; height: 2px;
background: #fff; background: #fff;
transform: scaleX(0); }
transition: transform 0.3s ease;
} }
&:hover { //
color: var(--el-color-primary); &.ghost {
background: linear-gradient(180deg, #fff 0%, #f5f7fa 100%); opacity: 0.5;
border-color: var(--el-color-primary-light-6) !important; background: var(--el-color-primary-light-9);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08); border: 1px dashed var(--el-color-primary);
}
&.chosen {
opacity: 0.8;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
transform: translateY(-2px); transform: translateY(-2px);
&::before {
transform: scaleX(0.7);
} }
&::after { &.drag {
transform: scaleX(1); opacity: 1;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
transform: translateY(-4px) scale(1.02);
} }
} }
&.is-active, .current-page-btn {
&.is-context-active { position: absolute;
background: #fff; right: 6px;
color: var(--el-color-primary); top: 50%;
// border-bottom: 1px solid #fff !important; transform: translateY(-50%);
border-color: var(--el-color-primary-light-4) !important; width: 28px;
box-shadow: 0 4px 16px rgba(var(--el-color-primary-rgb), 0.15); height: 28px;
font-weight: 500; display: flex;
z-index: 2; align-items: center;
justify-content: center;
&::before { background: var(--el-fill-color-light);
transform: scaleX(1); border: 1px solid var(--el-border-color-light);
} border-radius: 4px;
cursor: pointer;
&::after { color: var(--el-text-color-secondary);
transform: scaleX(1);
}
}
//
&.is-context-active:not(.is-active) {
background: linear-gradient(180deg, #fff 0%, #f0f7ff 100%);
border-color: var(--el-color-primary-light-5) !important;
}
//
.el-icon-close {
width: 16px;
height: 16px;
border-radius: 50%;
transition: all 0.2s ease; transition: all 0.2s ease;
font-size: 12px; padding: 0;
margin-left: 6px;
vertical-align: middle;
&:hover { &:hover {
background: var(--el-color-danger); background: var(--el-fill-color);
color: #fff; color: var(--el-color-primary);
transform: scale(1.15) rotate(90deg); border-color: var(--el-color-primary-light-5);
}
} }
} }
// //
.tab-label { .context-menu-dropdown {
display: inline-flex; position: fixed;
z-index: 999999;
background: var(--el-bg-color);
border: 1px solid var(--el-border-color-light);
border-radius: 4px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
padding: 4px 0;
min-width: 160px;
.context-menu-item {
display: flex;
align-items: center; align-items: center;
width: 100%; gap: 8px;
height: 100%; padding: 8px 16px;
gap: 6px; font-size: 13px;
color: var(--el-text-color-regular);
cursor: pointer;
transition: all 0.15s ease;
.tab-text { &:hover {
display: inline-block; background: var(--el-fill-color-light);
max-width: 150px; color: var(--el-color-primary);
overflow: auto; }
.el-icon {
font-size: 14px;
}
}
.context-menu-divider {
height: 1px;
background: var(--el-border-color-light);
margin: 4px 0;
}
}
//
.dropdown-header {
padding: 8px 12px;
font-size: 12px;
color: var(--el-text-color-secondary);
border-bottom: 1px solid var(--el-border-color-light);
background: var(--el-fill-color-lighter);
}
.page-dropdown-menu {
max-height: 300px;
overflow-y: auto;
:deep(.el-dropdown-menu__item) {
display: flex;
align-items: center;
justify-content: space-between;
padding-right: 12px;
&.is-active-item {
background: var(--el-color-primary-light-9);
color: var(--el-color-primary);
}
.dropdown-item-text {
flex: 1;
overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
white-space: nowrap; white-space: nowrap;
} }
&.dragging { .dropdown-item-close {
opacity: 0.5; margin-left: 8px;
cursor: move; font-size: 12px;
} opacity: 0.6;
}
}
.el-tabs__nav-wrap::after {
display: none;
}
//
.el-tabs__nav-prev,
.el-tabs__nav-next {
width: 32px;
line-height: 38px;
color: var(--el-text-color-secondary);
transition: all 0.2s ease;
border-radius: 4px;
&:hover { &:hover {
color: var(--el-color-primary); opacity: 1;
background: var(--el-fill-color-light); color: var(--el-color-danger);
} }
} }
} }
.context-menu-trigger {
display: none;
}
} }
</style> </style>

View File

@ -41,35 +41,35 @@ export const useTabsStore = defineStore('tabs', () => {
/** /**
* *
*
* -
* -
*/ */
const findBreadcrumbPath = (path: string): BreadcrumbItem[] => { const findBreadcrumbPath = (path: string): BreadcrumbItem[] => {
const list: BreadcrumbItem[] = [] const list: BreadcrumbItem[] = []
// 添加首页 // 如果是首页,直接返回首页
if (path === '/dashboard') {
list.push({ list.push({
title: '首页', title: '首页',
path: '/dashboard' path: '/dashboard'
}) })
// 如果是首页,直接返回
if (path === '/dashboard') {
return list return list
} }
// 从菜单中查找当前路由的层级信息 // 从菜单中查找当前路由的层级信息
const findMenuPath = (menus: any[], targetPath: string, currentPath: any[] = []): any[] | null => { const findMenuPath = (menus: any[], targetPath: string, parentMenu: any = null): any => {
for (const menu of menus) { for (const menu of menus) {
const menuPath = menu.path || menu.api || `/menu-${menu.id}` const menuPath = menu.path || menu.api || `/menu-${menu.id}`
const newPath = [...currentPath, menu]
if (menuPath === targetPath) { if (menuPath === targetPath) {
return newPath return { current: menu, parent: parentMenu }
} }
// 兼容 children 和 son 两种子菜单字段名 // 兼容 children 和 son 两种子菜单字段名
const subMenus = menu.children || menu.son const subMenus = menu.children || menu.son
if (subMenus && subMenus.length > 0) { if (subMenus && subMenus.length > 0) {
const result = findMenuPath(subMenus, targetPath, newPath) const result = findMenuPath(subMenus, targetPath, menu)
if (result) { if (result) {
return result return result
} }
@ -78,23 +78,41 @@ export const useTabsStore = defineStore('tabs', () => {
return null return null
} }
const menuPath = findMenuPath(userStore.sideMenu, path) const menuResult = findMenuPath(userStore.sideMenu, path)
console.log('menuPath:', menuPath);
if (menuPath) { if (menuResult) {
console.log('list:', menuPath) const { current, parent } = menuResult
menuPath.forEach((menu: any, index: number) => {
// 跳过第一个(首页已添加) // 如果存在父级菜单(一级菜单)
if (index === 0 && menu.label === '首页') { if (parent) {
return // 获取第一个二级菜单的路径
} const firstChildMenus = parent.children || parent.son
const firstChildPath = firstChildMenus && firstChildMenus.length > 0
? (firstChildMenus[0].path || firstChildMenus[0].api || `/menu-${firstChildMenus[0].id}`)
: parent.path || parent.api || `/menu-${parent.id}`
// 第一个面包屑是一级菜单的名字,但点击跳转第一个二级菜单
list.push({ list.push({
title: menu.label, title: parent.label,
path: menu.path || menu.api || `/menu-${menu.id}` path: firstChildPath
})
}) })
} }
;
// 添加当前菜单(二级菜单)
list.push({
title: current.label,
path: current.path || current.api || `/menu-${current.id}`
})
}
// 如果没有找到菜单信息,默认添加首页
if (list.length === 0) {
list.push({
title: '首页',
path: '/dashboard'
})
}
return list return list
} }
@ -104,7 +122,6 @@ export const useTabsStore = defineStore('tabs', () => {
const existIndex = tabs.value.findIndex(item => item.path === tab.path) const existIndex = tabs.value.findIndex(item => item.path === tab.path)
// 计算面包屑路径 // 计算面包屑路径
const breadcrumbs = findBreadcrumbPath(tab.path) const breadcrumbs = findBreadcrumbPath(tab.path)
console.log('aaaaaaaaaaaaa:', breadcrumbs);
if (existIndex === -1) { if (existIndex === -1) {
// 新标签页,添加到列表 // 新标签页,添加到列表