| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <template>
- <aside v-if="sidebarDepth > 0" class="sidebar">
- <div v-if="topLabel" class="sb-label sb-top">{{ topLabel }}</div>
- <div v-if="items.length" class="sb-section">
- <div
- v-for="item in items"
- :key="item.id"
- :class="['sb-item', { active: isActive(item) }]"
- @click="onClick(item)"
- >
- <span class="ico">●</span>
- <span class="label">{{ item.name }}</span>
- </div>
- </div>
- <div v-else class="sb-placeholder">该分类暂无子菜单</div>
- </aside>
- </template>
- <script setup>
- import { computed, onMounted } from 'vue'
- import { useRouter, useRoute } from 'vue-router'
- import { useMenu } from '@/store/menu'
- const router = useRouter()
- const route = useRoute()
- const { state, load, sidebarItems, sidebarDepth, cascadeSelection, topBarLevelCount } = useMenu()
- // 侧边栏要展示的项由菜单 store 派生:始终是菜单树的最后一层
- const items = computed(() => sidebarItems.value)
- const sidebarVisible = computed(() => sidebarDepth.value > 0)
- // 侧边栏标题:取上边栏最后一行选中项的名称(即层级最深的可见项)
- const topLabel = computed(() => {
- if (!sidebarVisible.value) return ''
- const sel = cascadeSelection.value
- const idx = topBarLevelCount.value - 1
- return sel[idx]?.name || sel[0]?.name || ''
- })
- onMounted(() => {
- if (!state.loaded) load()
- })
- function isActive(item) {
- if (!item?.route) return false
- return route.path === item.route || route.path.startsWith(`${item.route}/`)
- }
- function onClick(item) {
- if (!item?.route) return
- if (route.path !== item.route) router.push(item.route)
- }
- function iconChar(item) {
- if (!item?.icon) return '●'
- const trimmed = String(item.icon).trim()
- if (!trimmed) return '●'
- // 简单把首字符作为图标占位(未引入图标库时退化为单字符)
- return trimmed.charAt(0).toUpperCase()
- }
- </script>
- <style scoped>
- .sidebar {
- width: 208px;
- flex: none;
- background: #0c2b5e;
- color: rgba(255, 255, 255, 0.78);
- padding: 14px 0;
- overflow-y: auto;
- }
- .sb-section {
- padding: 0 12px;
- margin-bottom: 16px;
- }
- .sb-label {
- font-size: 10.5px;
- color: rgba(255, 255, 255, 0.4);
- padding: 6px 14px;
- margin-bottom: 6px;
- letter-spacing: 1px;
- }
- .sb-top {
- font-size: 12px;
- color: rgba(255, 255, 255, 0.7);
- letter-spacing: 0.5px;
- }
- .sb-item {
- display: flex;
- align-items: center;
- gap: 11px;
- padding: 9px 14px;
- border-radius: 6px;
- font-size: 12.5px;
- cursor: pointer;
- color: rgba(255, 255, 255, 0.78);
- margin-bottom: 2px;
- transition: 0.15s;
- }
- .sb-item:hover {
- background: #143a80;
- color: #fff;
- }
- .sb-item.active {
- background: #fff;
- color: #0c2b5e;
- font-weight: 600;
- }
- .sb-item.active .ico {
- color: #1552d0;
- }
- .sb-item .ico {
- width: 14px;
- height: 14px;
- font-size: 13px;
- display: flex;
- align-items: center;
- justify-content: center;
- line-height: 1;
- color: rgba(255, 255, 255, 0.6);
- font-weight: 700;
- flex: none;
- }
- .sb-item .label {
- flex: 1;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .sb-item .num {
- font-size: 10px;
- color: #fff;
- background: #e13b3b;
- padding: 1px 5px;
- border-radius: 8px;
- font-weight: 600;
- }
- .sb-divider {
- height: 1px;
- background: rgba(255, 255, 255, 0.1);
- margin: 8px 14px;
- }
- .sb-placeholder {
- padding: 12px 26px;
- font-size: 12px;
- color: rgba(255, 255, 255, 0.4);
- }
- </style>
|