xto-fronted 0.4.24 → 0.4.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,125 +1,245 @@
1
- <script setup lang="ts">
2
- import { computed } from 'vue'
3
- import { useAppStore } from '@/stores/app'
4
- import { useMenuStore } from '@/stores/menu'
5
- import Sidebar from './Sidebar.vue'
6
- import Header from './Header.vue'
7
- import TopMenu from './TopMenu.vue'
8
-
9
- const appStore = useAppStore()
10
- const menuStore = useMenuStore()
11
-
12
- const sidebarWidth = computed(() =>
13
- appStore.isCollapsed ? '64px' : '210px'
14
- )
15
-
16
- // 布局模式
17
- const layoutMode = computed(() => appStore.layout)
18
-
19
- // 是否显示左侧菜单
20
- const showSidebar = computed(() => layoutMode.value === 'sidebar' || layoutMode.value === 'mix')
21
-
22
- // 是否显示顶部菜单
23
- const showTopMenu = computed(() => layoutMode.value === 'top')
24
-
25
- // 是否显示Header(顶部菜单模式下不显示,因为功能已移到TopMenu)
26
- const showHeader = computed(() => layoutMode.value !== 'top')
27
-
28
- // 混合模式下只显示当前顶部菜单的子菜单
29
- const mixSubMenus = computed(() => {
30
- if (layoutMode.value !== 'mix') return menuStore.menuList
31
- // 混合模式需要根据顶部选中的菜单显示子菜单
32
- // 这里暂时返回全部菜单,后续可根据实际需求调整
33
- return menuStore.menuList
34
- })
35
- </script>
36
-
37
- <template>
38
- <div class="layout" :class="`layout--${layoutMode}`">
39
- <!-- 左侧菜单布局 -->
40
- <aside v-if="showSidebar" class="layout__aside" :style="{ width: sidebarWidth }">
41
- <Sidebar :menu-list="layoutMode === 'mix' ? mixSubMenus : menuStore.menuList" />
42
- </aside>
43
-
44
- <!-- 顶部菜单布局(包含右侧操作功能) -->
45
- <div v-if="showTopMenu" class="layout__top-menu">
46
- <TopMenu />
47
- </div>
48
-
49
- <div class="layout__main">
50
- <!-- Header仅在sidebar和mix模式下显示 -->
51
- <header v-if="showHeader" class="layout__header">
52
- <Header />
53
- </header>
54
- <main class="layout__content">
55
- <router-view />
56
- </main>
57
- </div>
58
- </div>
59
- </template>
60
-
61
- <style lang="scss" scoped>
62
- .layout {
63
- display: flex;
64
- width: 100%;
65
- height: 100%;
66
-
67
- // 左侧菜单模式
68
- &--sidebar {
69
- flex-direction: row;
70
- }
71
-
72
- // 顶部菜单模式
73
- &--top {
74
- flex-direction: column;
75
-
76
- .layout__aside {
77
- display: none;
78
- }
79
-
80
- .layout__main {
81
- flex: 1;
82
- }
83
- }
84
-
85
- // 混合菜单模式
86
- &--mix {
87
- flex-direction: row;
88
- }
89
-
90
- &__aside {
91
- transition: width 0.3s;
92
- overflow: hidden;
93
- flex-shrink: 0;
94
- height: 100%;
95
- }
96
-
97
- &__top-menu {
98
- width: 100%;
99
- height: 50px;
100
- background-color: var(--bg-color);
101
- border-bottom: 1px solid var(--color-border-lighter);
102
- }
103
-
104
- &__main {
105
- flex: 1;
106
- display: flex;
107
- flex-direction: column;
108
- overflow: hidden;
109
- height: 100%;
110
- }
111
-
112
- &__header {
113
- height: 50px;
114
- background-color: var(--bg-color);
115
- border-bottom: 1px solid var(--color-border-lighter);
116
- flex-shrink: 0;
117
- }
118
-
119
- &__content {
120
- flex: 1;
121
- overflow: auto;
122
- background-color: var(--bg-color-page);
123
- }
124
- }
1
+ <script setup lang="ts">
2
+ import { computed, watch } from 'vue'
3
+ import { useRoute } from 'vue-router'
4
+ import { useAppStore } from '@/stores/app'
5
+ import { useMenuStore } from '@/stores/menu'
6
+ import Sidebar from './Sidebar.vue'
7
+ import Header from './Header.vue'
8
+ import TopMenu from './TopMenu.vue'
9
+ import MixTopMenu from './MixTopMenu.vue'
10
+
11
+ const route = useRoute()
12
+ const appStore = useAppStore()
13
+ const menuStore = useMenuStore()
14
+
15
+ const sidebarWidth = computed(() =>
16
+ appStore.isCollapsed ? '64px' : '210px'
17
+ )
18
+
19
+ // 布局模式
20
+ const layoutMode = computed(() => appStore.layout)
21
+
22
+ // 混合模式下当前选中一级菜单的子菜单(二级及以下)
23
+ const mixSubMenus = computed(() => {
24
+ if (layoutMode.value !== 'mix') return []
25
+
26
+ const topMenuPath = appStore.activeTopMenuPath
27
+ if (!topMenuPath) return []
28
+
29
+ // 找到当前选中的一级菜单
30
+ const topMenu = menuStore.menuList.find(m => m.menuUrl === topMenuPath)
31
+ // 返回其children(二级菜单),如果没有children则返回空数组
32
+ return topMenu?.children || []
33
+ })
34
+
35
+ // 混合模式下是否显示左侧菜单(有二级菜单才显示)
36
+ const showMixSidebar = computed(() => {
37
+ return layoutMode.value === 'mix' && mixSubMenus.value.length > 0
38
+ })
39
+
40
+ // 根据当前路由自动匹配一级菜单
41
+ const syncTopMenuByRoute = (path: string) => {
42
+ if (layoutMode.value === 'mix' && menuStore.menuList.length > 0) {
43
+ // 如果已经有选中的一级菜单,且当前路径不在菜单中(如404页面),保持不变
44
+ if (appStore.activeTopMenuPath) {
45
+ // 检查当前路径是否在任何菜单中
46
+ let pathInMenu = false
47
+ for (const menu of menuStore.menuList) {
48
+ if (menu.menuUrl === path) {
49
+ pathInMenu = true
50
+ break
51
+ }
52
+ if (menu.children && menu.children.length > 0) {
53
+ if (checkPathInChildren(menu.children, path)) {
54
+ pathInMenu = true
55
+ break
56
+ }
57
+ }
58
+ }
59
+ // 如果路径不在菜单中,保持当前的activeTopMenuPath不变
60
+ if (!pathInMenu) {
61
+ return
62
+ }
63
+ }
64
+
65
+ // 遍历一级菜单,尝试匹配
66
+ for (const menu of menuStore.menuList) {
67
+ // 检查是否直接是一级菜单路径
68
+ if (menu.menuUrl === path) {
69
+ appStore.setActiveTopMenuPath(menu.menuUrl)
70
+ return
71
+ }
72
+ // 检查是否是该一级菜单的子菜单(包括多级子菜单)
73
+ if (menu.children && menu.children.length > 0) {
74
+ const isInChildren = checkPathInChildren(menu.children, path)
75
+ if (isInChildren) {
76
+ appStore.setActiveTopMenuPath(menu.menuUrl)
77
+ return
78
+ }
79
+ }
80
+ }
81
+ // 如果没匹配到且没有activeTopMenuPath,默认选中第一个有子菜单的一级菜单,或第一个菜单
82
+ if (!appStore.activeTopMenuPath) {
83
+ const firstMenuWithChildren = menuStore.menuList.find(m => m.children && m.children.length > 0)
84
+ const defaultMenu = firstMenuWithChildren || menuStore.menuList[0]
85
+ if (defaultMenu) {
86
+ appStore.setActiveTopMenuPath(defaultMenu.menuUrl)
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ // 递归检查路径是否在子菜单中
93
+ const checkPathInChildren = (children: any[], path: string): boolean => {
94
+ for (const child of children) {
95
+ if (child.menuUrl === path) {
96
+ return true
97
+ }
98
+ if (child.children && child.children.length > 0) {
99
+ if (checkPathInChildren(child.children, path)) {
100
+ return true
101
+ }
102
+ }
103
+ }
104
+ return false
105
+ }
106
+
107
+ // 监听路由变化,同步一级菜单选中状态
108
+ watch(
109
+ () => route.path,
110
+ (path) => {
111
+ syncTopMenuByRoute(path)
112
+ },
113
+ { immediate: true }
114
+ )
115
+
116
+ // 监听菜单列表变化,重新同步
117
+ watch(
118
+ () => menuStore.menuList,
119
+ () => {
120
+ if (layoutMode.value === 'mix') {
121
+ syncTopMenuByRoute(route.path)
122
+ }
123
+ }
124
+ )
125
+ </script>
126
+
127
+ <template>
128
+ <div class="layout" :class="`layout--${layoutMode}`">
129
+ <!-- sidebar模式:左侧菜单 + Header + 内容 -->
130
+ <template v-if="layoutMode === 'sidebar'">
131
+ <aside class="layout__aside" :style="{ width: sidebarWidth }">
132
+ <Sidebar :menu-list="menuStore.menuList" />
133
+ </aside>
134
+ <div class="layout__main">
135
+ <header class="layout__header">
136
+ <Header />
137
+ </header>
138
+ <main class="layout__content">
139
+ <router-view />
140
+ </main>
141
+ </div>
142
+ </template>
143
+
144
+ <!-- top模式:顶部菜单 + 内容 -->
145
+ <template v-if="layoutMode === 'top'">
146
+ <div class="layout__top-menu">
147
+ <TopMenu />
148
+ </div>
149
+ <div class="layout__main">
150
+ <main class="layout__content">
151
+ <router-view />
152
+ </main>
153
+ </div>
154
+ </template>
155
+
156
+ <!-- mix模式:顶部一级菜单 + 左侧二级菜单 + 内容 -->
157
+ <template v-if="layoutMode === 'mix'">
158
+ <div class="layout__mix-top">
159
+ <MixTopMenu />
160
+ </div>
161
+ <div class="layout__mix-body">
162
+ <!-- 只有当一级菜单有子菜单时才显示左侧菜单 -->
163
+ <aside v-if="showMixSidebar" class="layout__aside" :style="{ width: sidebarWidth }">
164
+ <Sidebar :menu-list="mixSubMenus" />
165
+ </aside>
166
+ <div class="layout__main">
167
+ <main class="layout__content">
168
+ <router-view />
169
+ </main>
170
+ </div>
171
+ </div>
172
+ </template>
173
+ </div>
174
+ </template>
175
+
176
+ <style lang="scss" scoped>
177
+ .layout {
178
+ display: flex;
179
+ width: 100%;
180
+ height: 100%;
181
+
182
+ // 左侧菜单模式
183
+ &--sidebar {
184
+ flex-direction: row;
185
+ }
186
+
187
+ // 顶部菜单模式
188
+ &--top {
189
+ flex-direction: column;
190
+ }
191
+
192
+ // 混合菜单模式
193
+ &--mix {
194
+ flex-direction: column;
195
+ }
196
+
197
+ &__aside {
198
+ transition: width 0.3s;
199
+ overflow: hidden;
200
+ flex-shrink: 0;
201
+ height: 100%;
202
+ }
203
+
204
+ &__top-menu {
205
+ width: 100%;
206
+ height: 50px;
207
+ background-color: var(--bg-color);
208
+ border-bottom: 1px solid var(--color-border-lighter);
209
+ }
210
+
211
+ &__mix-top {
212
+ width: 100%;
213
+ height: 50px;
214
+ background-color: var(--bg-color);
215
+ border-bottom: 1px solid var(--color-border-lighter);
216
+ }
217
+
218
+ &__mix-body {
219
+ display: flex;
220
+ flex: 1;
221
+ overflow: hidden;
222
+ }
223
+
224
+ &__main {
225
+ flex: 1;
226
+ display: flex;
227
+ flex-direction: column;
228
+ overflow: hidden;
229
+ height: 100%;
230
+ }
231
+
232
+ &__header {
233
+ height: 50px;
234
+ background-color: var(--bg-color);
235
+ border-bottom: 1px solid var(--color-border-lighter);
236
+ flex-shrink: 0;
237
+ }
238
+
239
+ &__content {
240
+ flex: 1;
241
+ overflow: auto;
242
+ background-color: var(--bg-color-page);
243
+ }
244
+ }
125
245
  </style>
package/src/stores/app.ts CHANGED
@@ -22,6 +22,8 @@ export const useAppStore = defineStore('app', () => {
22
22
  const showBreadcrumb = ref<boolean>(local.get<boolean>('showBreadcrumb') ?? true)
23
23
  const primaryColor = ref<string>(local.get<string>('primaryColor') || '#409eff')
24
24
  const cachedViews = ref<string[]>([])
25
+ // 混合模式下选中的一级菜单路径
26
+ const activeTopMenuPath = ref<string>(local.get<string>('activeTopMenuPath') || '')
25
27
 
26
28
  // 计算属性
27
29
  const themeClass = computed(() => (isDark.value ? 'dark' : 'light'))
@@ -74,6 +76,17 @@ export const useAppStore = defineStore('app', () => {
74
76
  const setLayout = (mode: LayoutMode) => {
75
77
  layout.value = mode
76
78
  local.set('layout', mode)
79
+ // 切换布局时清除一级菜单选中状态
80
+ if (mode !== 'mix') {
81
+ activeTopMenuPath.value = ''
82
+ local.remove('activeTopMenuPath')
83
+ }
84
+ }
85
+
86
+ // 设置混合模式选中的一级菜单
87
+ const setActiveTopMenuPath = (path: string) => {
88
+ activeTopMenuPath.value = path
89
+ local.set('activeTopMenuPath', path)
77
90
  }
78
91
 
79
92
  // 切换标签页
@@ -144,6 +157,7 @@ export const useAppStore = defineStore('app', () => {
144
157
  showBreadcrumb,
145
158
  primaryColor,
146
159
  cachedViews,
160
+ activeTopMenuPath,
147
161
  themeClass,
148
162
  setAppName,
149
163
  setIndexPath,
@@ -151,6 +165,7 @@ export const useAppStore = defineStore('app', () => {
151
165
  toggleCollapse,
152
166
  setTheme,
153
167
  setLayout,
168
+ setActiveTopMenuPath,
154
169
  toggleTabs,
155
170
  toggleFooter,
156
171
  toggleBreadcrumb,