wujingjing
2025-04-09 2b8b2cac4fe3f05474459a034bc4034f2d7aa0cb
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
<template>
    <div class="pc-chat_aside flex-0 relative" :style="`width:252px;transition: 0.7s ease-in;`">
        <div class="aside_top">
            <div class="logo flex-items-center justify-between">
                <div class="flex items-center">
                    <img src="/static/images/logo/logoWithNoName.png" alt="logo" class="layout-logo-medium-img" />
                    <span class="font-extrabold text-xl text-white tracking-wide"> WI水务智能平台</span>
                </div>
            </div>
        </div>
        <!-- Logo 部分 -->
        <!-- <div class="h-[40px] w-full bg-[#0084ff] flex justify-between items-center text-white px-2">
            <span>GIS系统</span>
        </div> -->
        <!-- 菜单项 -->
        <div class="menu-container">
            <el-menu :default-active="activeId" class="wi-menu !w-full" :unique-opened="true" @select="handleSelect">
                <template v-for="item in menuData">
                    <el-sub-menu :key="`sub-${item.id}`" v-if="item.children?.length" :index="item.id">
                        <template #title>
                            <i v-if="item.icon" :class="item.icon"></i>
                            <span>{{ item.title }}</span>
                        </template>
 
                        <template v-for="subItem in item.children">
                            <el-sub-menu :key="`sub-${subItem.id}`" v-if="subItem.children?.length" :index="subItem.id">
                                <template #title>
                                    <i v-if="subItem.icon" :class="subItem.icon"></i>
                                    <span>{{ subItem.title }}</span>
                                </template>
                                <!-- 三级菜单项 -->
                                <el-menu-item v-for="child in subItem.children" :key="`item-${child.id}`" :index="child.id">
                                    <i v-if="child.icon" :class="child.icon"></i>
                                    <span>{{ child.title }}</span>
                                </el-menu-item>
                            </el-sub-menu>
                            <!-- 二级菜单项 -->
                            <el-menu-item v-else :key="`item-${subItem.id}`" :index="subItem.id">
                                <i v-if="subItem.icon" :class="subItem.icon"></i>
                                <span>{{ subItem.title }}</span>
                            </el-menu-item>
                        </template>
                    </el-sub-menu>
                    <el-menu-item v-else :key="`item-${item.id}`" :index="item.id">
                        <i v-if="item.icon" :class="item.icon"></i>
                        <span>{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </el-menu>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import { computed, ref, onMounted, watch } from 'vue';
import { useRouter } from 'vue-router';
import type { MenuItemData } from './types';
 
const router = useRouter();
const activeId = ref('');
 
// 示例菜单数据
const menuData = ref<MenuItemData[]>([
    {
        id: 'menu1',
        title: '首页',
        icon: 'icon-park-outline-chart-line',
        path: '/gis/situation',
    },
    {
        id: 'menu2',
        title: '统计',
        icon: 'icon-park-outline-folder',
        children: [
            {
                id: '自定义统计',
                title: '自定义统计',
                path: '/gis/situation',
            },
            {
                id: 'menu2-2',
                title: '管线统计',
                path: '/gis/situation',
            },
        ],
    },
]);
 
// 计算属性:有子菜单的项
const parentMenuItems = computed(() => menuData.value.filter((item) => item.children?.length));
 
// 计算属性:没有子菜单的项
const leafMenuItems = computed(() => menuData.value.filter((item) => !item.children?.length));
 
const handleSelect = (index: string) => {
    activeId.value = index;
    const findPath = (items: MenuItemData[]): string | undefined => {
        for (const item of items) {
            if (item.id === index) return item.path;
            if (item.children) {
                const path = findPath(item.children);
                if (path) return path;
            }
        }
    };
    console.log('🚀 ~ index:', index);
    const path = findPath(menuData.value);
    if (path) {
        router.push(path);
    }
};
 
// 通过 path 找 index
const findIndex = (items: MenuItemData[]): string | undefined => {
    for (const item of items) {
        if (item.path === router.currentRoute.value.path) return item.id;
        if (item.children) {
            const index = findIndex(item.children);
            if (index) return index;
        }
    }
};
watch(router.currentRoute, (newRoute) => {
    activeId.value = findIndex(menuData.value) || '';
});
</script>
 
<style scoped lang="scss">
.pc-chat_aside {
    height: 100%;
    box-sizing: border-box;
    background-color: var(--color-bg-side);
    overflow: visible;
    transition: width 0.1s ease-in;
    position: relative;
    display: flex;
    flex-direction: column;
}
 
.aside_top {
    box-sizing: border-box;
    position: relative;
    width: 100%;
    padding: 18px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
 
.layout-logo-medium-img {
    width: 28px;
    margin-right: 7px;
}
 
.menu-container {
    flex: 1;
    overflow-y: auto;
 
    :deep(.wi-menu) {
        border-right: none;
        background-color: transparent;
        --hover-menu-bg: rgba(255, 255, 255, 0.1);
        --menu-width: 100% !important;
 
        --active-menu-bg: #333534 !important;
        --active-menu-color: var(--color-btn-base) !important;
 
        .el-menu {
            background-color: transparent;
            width: var(--menu-width) !important;
        }
 
        // 鼠标 hover 时颜色
 
        .el-menu-item {
            height: 40px;
            line-height: 40px;
            color: #ccc;
 
            &:hover {
                background: var(--hover-menu-bg) !important;
            }
 
            &.is-active {
                background: var(--active-menu-bg) !important;
                color: var(--active-menu-color);
            }
 
            i {
                margin-right: 8px;
            }
        }
 
        .el-sub-menu {
            .el-menu {
                width: var(--menu-width) !important;
            }
            --next-bg-menuBarActiveColor: unset !important;
 
            .el-sub-menu__title {
                height: 40px;
                line-height: 40px;
                color: #ccc;
 
                &:hover {
                    background: var(--hover-menu-bg) !important;
                }
 
                i {
                    margin-right: 8px;
                }
            }
 
            &.is-active {
                .el-sub-menu__title {
                    color: var(--active-menu-color);
                }
            }
        }
 
        // 子菜单弹出层样式
        .el-menu--popup {
            background-color: var(--color-bg-side);
            border: 1px solid rgba(255, 255, 255, 0.1);
            padding: 0;
 
            .el-menu-item {
                background-color: transparent;
                color: #ccc;
 
                &:hover {
                    background-color: rgba(255, 255, 255, 0.1);
                }
 
                &.is-active {
                    background: var(--active-menu-bg);
                    color: var(--active-menu-color);
                }
            }
        }
    }
}
</style>