MainLayout.vue
16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<template>
<div class="main-layout">
<!-- 左侧菜单栏 -->
<aside class="sidebar" :class="{ collapsed: sidebarCollapsed }">
<div class="sidebar-header">
<h2 class="logo">Apple ERP</h2>
<button
class="collapse-btn"
@click="toggleSidebar"
:title="sidebarCollapsed ? '展开菜单' : '收起菜单'"
>
{{ sidebarCollapsed ? '→' : '←' }}
</button>
</div>
<nav class="sidebar-nav">
<ul class="nav-list">
<li v-for="item in menuItems" :key="item.path || item.name" class="nav-item">
<!-- 一级菜单 -->
<div v-if="!item.children" class="nav-item-single">
<a
@click="handleMenuClick(item.path)"
class="nav-link"
:class="{ active: $route.path === item.path }"
>
<span class="nav-icon">{{ item.icon }}</span>
<span class="nav-text" v-show="!sidebarCollapsed">{{ item.name }}</span>
</a>
</div>
<!-- 有子菜单的一级菜单 -->
<div v-else class="nav-item-group">
<div
class="nav-link nav-link-parent"
:class="{ active: isParentActive(item) }"
@click="toggleSubmenu(item)"
>
<span class="nav-icon">{{ item.icon }}</span>
<span class="nav-text" v-show="!sidebarCollapsed">{{ item.name }}</span>
<span class="nav-arrow" v-show="!sidebarCollapsed">{{ item.expanded ? '▼' : '▶' }}</span>
</div>
<!-- 二级菜单 -->
<ul v-if="item.expanded && !sidebarCollapsed" class="submenu">
<li v-for="child in item.children" :key="child.path" class="submenu-item">
<a
@click="child.path ? handleMenuClick(child.path) : null"
class="nav-link nav-link-child"
:class="{ active: $route.path === child.path }"
>
<span class="nav-icon">{{ child.icon }}</span>
<span class="nav-text">{{ child.name }}</span>
</a>
</li>
</ul>
</div>
</li>
</ul>
</nav>
</aside>
<!-- 主内容区域 -->
<div class="main-content">
<!-- 顶部导航栏 -->
<header class="top-header">
<div class="header-left">
<button class="menu-toggle" @click="toggleSidebar">
☰
</button>
<h1 class="page-title">{{ currentPageTitle }}</h1>
</div>
<div class="header-right">
<div class="user-info">
<span class="welcome-text">欢迎,{{ userInfo?.username || '用户' }}</span>
<div class="user-actions">
<button class="logout-btn" @click="handleLogout">退出登录</button>
</div>
</div>
</div>
</header>
<!-- 标签页导航栏 -->
<div class="tab-navigation">
<div class="tab-scroll-left" @click="scrollTabsLeft">
<span class="scroll-arrow">‹‹</span>
</div>
<div class="tab-container" ref="tabContainer">
<div
v-for="tab in openTabs"
:key="tab.id"
class="tab-item"
:class="{ active: tab.active }"
@click="switchToTab(tab)"
>
<span class="tab-name">{{ tab.name }}</span>
<span
v-if="tab.closable"
@click.stop="closeTab(tab)"
class="tab-close"
>×</span>
</div>
</div>
<div class="tab-scroll-right" @click="scrollTabsRight">
<span class="scroll-arrow">››</span>
</div>
</div>
<!-- 页面内容 -->
<main class="page-content">
<router-view />
</main>
<!-- 底部栏 -->
<footer class="main-footer">
<div class="footer-content">
<p class="copyright">© 2025 Erry Copyright</p>
</div>
</footer>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch, nextTick } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { logoutApi } from '../api/auth'
const router = useRouter()
const route = useRoute()
// 侧边栏状态
const sidebarCollapsed = ref(false)
// 用户信息
const userInfo = ref<any>(null)
// 标签页容器引用
const tabContainer = ref<HTMLElement>()
// 菜单项配置
const menuItems = ref([
{ name: '首页', path: '/main/dashboard', icon: '🏠' },
{ name: '订单管理', path: '/main/order', icon: '📋' },
{ name: '出库查询', path: '/main/delivery', icon: '📦' },
{ name: '发票管理', path: '/main/invoice', icon: '🧾' },
{ name: '产品管理', path: '/main/product', icon: '📦' },
{ name: '经销商管理', path: '/main/dealer', icon: '🏢' },
{ name: '返利管理', path: '/main/rebate', icon: '💰' },
{ name: '异常工单', path: '/main/exception-workorder', icon: '⚠️' },
{
name: '系统设置',
icon: '⚙️',
expanded: false,
children: [
{ name: '用户管理', path: '/main/users', icon: '👤' },
{ name: '角色管理', path: '/main/sys/role', icon: '🛡️' },
{ name: '菜单管理', path: '/main/sys/menu', icon: '📝' },
{
name: '字典管理',
path: '/main/sys/dict',
icon: '📖',
expanded: false,
children: [
{ name: '字典类型', path: '/main/sys/dict', icon: '📋' },
{ name: '字典项', path: '/main/sys/dict-item', icon: '📝' }
]
},
{ name: '日志管理', path: '/main/sys/log', icon: '📊' }
]
}
])
// 打开的标签页
const openTabs = ref([
{ id: 1, name: '首页', path: '/main/dashboard', active: true, closable: false }
])
// 当前页面标题
const currentPageTitle = computed(() => {
const activeTab = openTabs.value.find(tab => tab.active)
return activeTab ? activeTab.name : 'Apple经销商ERP系统'
})
// 处理菜单点击
const handleMenuClick = (path: string) => {
if (path && path !== route.path) {
console.log('菜单导航:', path)
// 检查是否已经存在该标签页
const existingTab = openTabs.value.find(tab => tab.path === path)
if (existingTab) {
// 切换到已存在的标签页
switchToTab(existingTab)
} else {
// 创建新标签页
const menuItem = findMenuItemByPath(path)
if (menuItem) {
addNewTab(menuItem.name, path)
}
}
router.push(path).catch(err => {
console.error('路由跳转失败:', err)
})
}
}
// 根据路径查找菜单项
const findMenuItemByPath = (path: string) => {
for (const item of menuItems.value) {
if (item.path === path) {
return item
}
if (item.children && Array.isArray(item.children)) {
const child = item.children.find((child: any) => child.path === path)
if (child) return child
}
}
return null
}
// 添加新标签页
const addNewTab = (name: string, path: string) => {
// 先取消所有标签页的激活状态
openTabs.value.forEach(tab => tab.active = false)
// 添加新标签页
const newTab = {
id: Date.now(),
name: name,
path: path,
active: true,
closable: true
}
openTabs.value.push(newTab)
}
// 切换到指定标签页
const switchToTab = (tab: any) => {
// 取消所有标签页的激活状态
openTabs.value.forEach(t => t.active = false)
// 激活当前标签页
tab.active = true
// 跳转到对应路由
if (tab.path !== route.path) {
router.push(tab.path)
}
}
// 关闭标签页
const closeTab = (tab: any) => {
if (!tab.closable) return
const tabIndex = openTabs.value.findIndex(t => t.id === tab.id)
if (tabIndex === -1) return
// 如果关闭的是当前激活的标签页
if (tab.active) {
// 找到下一个要激活的标签页
let nextActiveTab = null
// 优先选择右侧的标签页
if (tabIndex < openTabs.value.length - 1) {
nextActiveTab = openTabs.value[tabIndex + 1]
} else if (tabIndex > 0) {
nextActiveTab = openTabs.value[tabIndex - 1]
}
// 移除当前标签页
openTabs.value.splice(tabIndex, 1)
// 激活下一个标签页
if (nextActiveTab) {
nextActiveTab.active = true
router.push(nextActiveTab.path)
} else {
// 如果没有其他标签页,跳转到首页
router.push('/main/dashboard')
}
} else {
// 如果关闭的不是当前激活的标签页,直接移除
openTabs.value.splice(tabIndex, 1)
}
}
// 标签页滚动功能
const scrollTabsLeft = () => {
if (tabContainer.value) {
tabContainer.value.scrollBy({ left: -200, behavior: 'smooth' })
}
}
const scrollTabsRight = () => {
if (tabContainer.value) {
tabContainer.value.scrollBy({ left: 200, behavior: 'smooth' })
}
}
// 切换子菜单展开/收起
const toggleSubmenu = (item: any) => {
item.expanded = !item.expanded
// 如果父级菜单有路径,则同时导航到该路径
if (item.path && item.path !== route.path) {
handleMenuClick(item.path)
}
}
// 检查父菜单是否激活
const isParentActive = (item: any) => {
if (!item.children) return false
return item.children.some((child: any) => child.path === route.path)
}
// 切换侧边栏
const toggleSidebar = () => {
sidebarCollapsed.value = !sidebarCollapsed.value
}
// 退出登录
const handleLogout = async () => {
if (confirm('确定要退出登录吗?')) {
try {
// 调用后端退出登录接口
await logoutApi()
} catch (error) {
console.error('退出登录接口调用失败:', error)
} finally {
// 无论接口调用成功与否,都清除本地存储
localStorage.removeItem('token')
localStorage.removeItem('userInfo')
// 跳转到登录页
router.push('/')
}
}
}
// 监听路由变化,自动管理标签页
watch(() => route.path, (newPath) => {
// 检查是否已经存在该路径的标签页
const existingTab = openTabs.value.find(tab => tab.path === newPath)
if (existingTab) {
// 如果存在,激活该标签页
openTabs.value.forEach(tab => tab.active = false)
existingTab.active = true
} else {
// 如果不存在,创建新标签页
const menuItem = findMenuItemByPath(newPath)
if (menuItem) {
addNewTab(menuItem.name, newPath)
}
}
// 自动展开对应的父菜单
for (const item of menuItems.value) {
if (item.children && Array.isArray(item.children)) {
const hasActiveChild = item.children.some((child: any) => child.path === newPath)
if (hasActiveChild) {
item.expanded = true
break
}
}
}
}, { immediate: true })
// 组件挂载时获取用户信息
onMounted(() => {
const storedUserInfo = localStorage.getItem('userInfo')
if (storedUserInfo) {
userInfo.value = JSON.parse(storedUserInfo)
}
})
</script>
<style scoped>
.main-layout {
display: flex;
height: 100vh;
background-color: #f5f5f5;
}
/* 左侧菜单栏 */
.sidebar {
width: 200px;
background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
color: white;
transition: width 0.3s ease;
overflow: hidden;
box-shadow: 2px 0 5px rgba(0,0,0,0.1);
z-index: 1000;
}
.sidebar.collapsed {
width: 50px;
}
.sidebar-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px;
border-bottom: 1px solid rgba(255,255,255,0.1);
}
.logo {
margin: 0;
font-size: 16px;
font-weight: bold;
color: #ecf0f1;
}
.collapse-btn {
background: none;
border: none;
color: white;
font-size: 14px;
cursor: pointer;
padding: 4px;
border-radius: 3px;
transition: background-color 0.3s;
}
.collapse-btn:hover {
background-color: rgba(255,255,255,0.1);
}
/* 导航菜单 */
.sidebar-nav {
padding: 16px 0;
}
.nav-list {
list-style: none;
margin: 0;
padding: 0;
}
.nav-item {
margin-bottom: 5px;
}
.nav-link {
display: flex;
align-items: center;
padding: 8px 12px;
color: #bdc3c7;
text-decoration: none;
transition: all 0.3s ease;
border-left: 3px solid transparent;
cursor: pointer;
}
.nav-link:hover {
background-color: rgba(255,255,255,0.1);
color: white;
}
.nav-link.active {
background-color: rgba(52, 152, 219, 0.2);
color: #3498db;
border-left-color: #3498db;
}
.nav-icon {
font-size: 16px;
margin-right: 10px;
min-width: 18px;
}
.nav-text {
font-size: 13px;
white-space: nowrap;
}
.nav-arrow {
font-size: 10px;
margin-left: auto;
transition: transform 0.3s ease;
}
/* 二级菜单样式 */
.nav-item-group {
margin-bottom: 5px;
}
.nav-link-parent {
cursor: pointer;
user-select: none;
}
.nav-link-parent:hover {
background-color: rgba(255,255,255,0.1);
color: white;
}
.submenu {
list-style: none;
margin: 0;
padding: 0;
background: rgba(0,0,0,0.1);
border-left: 2px solid #3498db;
}
.submenu-item {
margin: 0;
}
.nav-link-child {
padding: 6px 12px 6px 32px;
font-size: 12px;
color: #bdc3c7;
}
.nav-link-child:hover {
background-color: rgba(255,255,255,0.05);
color: white;
}
.nav-link-child.active {
background-color: rgba(52, 152, 219, 0.2);
color: #3498db;
}
/* 主内容区域 */
.main-content {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
}
/* 顶部导航栏 */
.top-header {
background: white;
padding: 0 16px;
height: 50px;
display: flex;
align-items: center;
justify-content: space-between;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
border-bottom: 1px solid #e0e0e0;
position: relative;
z-index: 100;
width: 100%;
min-height: 50px;
}
.header-left {
display: flex;
align-items: center;
}
.menu-toggle {
background: none;
border: none;
font-size: 16px;
cursor: pointer;
padding: 6px;
margin-right: 12px;
border-radius: 4px;
transition: background-color 0.3s;
}
.menu-toggle:hover {
background-color: #f0f0f0;
}
.page-title {
margin: 0;
font-size: 18px;
color: #333;
font-weight: 500;
}
.header-right {
display: flex;
align-items: center;
}
.user-info {
display: flex;
align-items: center;
gap: 15px;
}
.welcome-text {
color: #666;
font-size: 13px;
}
.logout-btn {
background: #e74c3c;
color: white;
border: none;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
transition: background-color 0.3s;
}
.logout-btn:hover {
background: #c0392b;
}
/* 标签页导航栏 */
.tab-navigation {
background: white;
border-bottom: 1px solid #e0e0e0;
display: flex;
align-items: center;
height: 32px;
padding: 0 6px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.tab-scroll-left,
.tab-scroll-right {
background: #f5f5f5;
border: 1px solid #d9d9d9;
padding: 2px 6px;
cursor: pointer;
border-radius: 2px;
margin: 0 1px;
transition: all 0.2s;
}
.tab-scroll-left:hover,
.tab-scroll-right:hover {
background: #e6f7ff;
border-color: #91d5ff;
}
.scroll-arrow {
font-size: 10px;
color: #666;
font-weight: bold;
}
.tab-container {
flex: 1;
display: flex;
overflow-x: auto;
scrollbar-width: none;
-ms-overflow-style: none;
}
.tab-container::-webkit-scrollbar {
display: none;
}
.tab-item {
display: flex;
align-items: center;
padding: 4px 8px;
margin: 0 1px;
background: white;
border: 1px solid #d9d9d9;
border-radius: 3px;
cursor: pointer;
transition: all 0.2s;
white-space: nowrap;
min-width: 60px;
position: relative;
}
.tab-item:hover {
background: #f0f8ff;
border-color: #91d5ff;
}
.tab-item.active {
background: #1890ff;
border-color: #1890ff;
color: white;
}
.tab-name {
font-size: 11px;
margin-right: 4px;
}
.tab-close {
font-size: 12px;
cursor: pointer;
padding: 1px;
border-radius: 2px;
transition: all 0.2s;
}
.tab-item:not(.active) .tab-close {
color: #999;
}
.tab-item:not(.active) .tab-close:hover {
background: #f5f5f5;
color: #666;
}
.tab-item.active .tab-close {
color: white;
}
.tab-item.active .tab-close:hover {
background: rgba(255,255,255,0.2);
}
/* 页面内容 */
.page-content {
flex: 1;
padding: 0;
overflow-y: auto;
background-color: #f8f9fa;
position: relative;
}
/* 底部栏 */
.main-footer {
background: white;
color: #333;
padding: 8px 20px;
border-top: 1px solid #e0e0e0;
box-shadow: 0 -2px 4px rgba(0,0,0,0.1);
}
.footer-content {
display: flex;
justify-content: center;
align-items: center;
max-width: 1200px;
margin: 0 auto;
}
.copyright {
margin: 0;
font-size: 12px;
color: #666;
text-align: center;
}
/* 响应式设计 */
@media (max-width: 768px) {
.sidebar {
position: fixed;
left: -200px;
z-index: 1000;
height: 100vh;
}
.sidebar:not(.collapsed) {
left: 0;
}
.main-content {
margin-left: 0;
}
.tab-navigation {
height: 28px;
}
.tab-item {
min-width: 50px;
padding: 3px 6px;
}
.tab-name {
font-size: 10px;
}
}
</style>