wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
<template>
    <div class="a-menu">
        <div
            class="a-menu__item"
            v-for="(item, index) in list"
            :key="item.id"
            :class="{
                'is-active': index == active
            }"
            @click="select(index)"
        >
            <cl-svg class="mr-3" :name="item.icon" :size="16" v-if="item.icon" />
            <span class="text-[12px] tracking-wider whitespace-nowrap">{{ item.meta?.label }}</span>
        </div>
    </div>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'a-menu'
});
 
import { computed, ref, watch } from 'vue';
import { useBase } from '/$/base';
import { useCool } from '/@/cool';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';
 
const { router, route } = useCool();
const { menu } = useBase();
const { t } = useI18n();
 
// 选中标识
const active = ref(0);
 
// 组列表
const list = computed(() => {
    return menu.group.filter(e => e.isShow);
});
 
// 选择导航
function select(index: number) {
    if (index == active.value) {
        return false;
    }
 
    // 选中的组
    const item = list.value[index];
 
    // 获取第一个菜单地址
    const url = menu.getPath(item);
 
    if (url) {
        // 设置左侧菜单
        menu.setMenu(index);
 
        // 跳转
        router.push(url);
    } else {
        ElMessage.warning(t('{label} 没有子菜单,请先添加', { label: item.meta?.label }));
    }
}
 
// 刷新
function refresh() {
    let index = 0;
 
    function deep(e: Menu.Item, i: number) {
        switch (e.type) {
            case 0:
                if (e.children) {
                    e.children.forEach(e => {
                        deep(e, i);
                    });
                }
 
                break;
            case 1:
                if (route.path.includes(e.path)) {
                    index = i;
                }
                break;
            default:
                break;
        }
    }
 
    // 遍历所有分组
    list.value.forEach(deep);
 
    // 确认选择
    active.value = index;
 
    // 设置该分组下的菜单
    menu.setMenu(index);
}
 
// 监听变化
watch(
    () => [route.path, menu.group.length],
    () => {
        refresh();
    },
    {
        immediate: true
    }
);
</script>
 
<style lang="scss" scoped>
.a-menu {
    display: flex;
    align-items: center;
    flex: 1;
    user-select: none;
 
    &__item {
        display: flex;
        align-items: center;
        height: 32px;
        padding: 0 16px 0 12px;
        border: 0;
        color: var(--el-color-info);
        position: relative;
        background-color: transparent;
        border-radius: 6px;
        cursor: pointer;
        border: 1px solid transparent;
        margin-right: 6px;
        transition: all 0.3s;
 
        &.is-active {
            border-color: var(--el-color-primary-light-8);
            color: var(--el-color-primary);
        }
 
        &.is-active,
        &:hover {
            background-color: var(--el-color-primary-light-9);
        }
 
        &:last-child {
            margin-right: 0;
        }
    }
 
    &__name {
        margin-left: 8px;
    }
}
</style>