Files
JayuCloud/Frontend/src/components/BottomNav.vue
T

185 lines
4.8 KiB
Vue
Raw Normal View History

<template>
<a-layout class="bottom-nav">
<a-layout-content class="nav-content">
<!-- 首页导航项 -->
<div class="nav-item" :class="{ active: currentRoute === 'home' }" @click="handleNavClick('home')">
<a-badge :count="0" :offset="[0, 0]">
<div class="nav-icon">
<HomeOutlined :style="{ fontSize: '24px' }" />
</div>
</a-badge>
<span class="nav-label">首页</span>
</div>
2026-03-05 16:08:27 +08:00
<!-- 作品导航项 -->
<div class="nav-item" :class="{ active: currentRoute === 'products' }" @click="handleNavClick('products')">
<a-badge :count="0" :offset="[0, 0]">
<div class="nav-icon">
<ShoppingOutlined :style="{ fontSize: '24px' }" />
</div>
</a-badge>
2026-03-05 16:08:27 +08:00
<span class="nav-label">作品</span>
</div>
<!-- 购物车导航项 -->
<div class="nav-item" :class="{ active: currentRoute === 'cart' }" @click="handleNavClick('cart')">
<a-badge :count="cartCount" :offset="[0, 0]">
<div class="nav-icon">
<ShoppingCartOutlined :style="{ fontSize: '24px' }" />
</div>
</a-badge>
<span class="nav-label">购物车</span>
</div>
<!-- 个人中心导航项 -->
<div class="nav-item" :class="{ active: currentRoute === 'profile' }" @click="handleNavClick('profile')">
<a-badge :dot="hasNotification" :offset="[0, 0]">
<div class="nav-icon">
<UserOutlined :style="{ fontSize: '24px' }" />
</div>
</a-badge>
<span class="nav-label">我的</span>
</div>
</a-layout-content>
</a-layout>
</template>
<script setup lang="ts">
2026-02-28 17:09:45 +08:00
import { computed, inject, onMounted, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { HomeOutlined, ShoppingOutlined, ShoppingCartOutlined, UserOutlined } from '@ant-design/icons-vue'
const router = useRouter()
const route = useRoute()
2026-02-28 17:09:45 +08:00
// 注入全局购物车数量状态
const cartCount = inject('cartCount', ref(0))
// 注入更新购物车数量的方法
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
// 是否有未读通知
2026-02-28 17:09:45 +08:00
const hasNotification = computed(() => true)
2026-02-28 14:32:28 +08:00
// 加载购物车数量
const loadCartCount = async () => {
2026-02-28 17:09:45 +08:00
// 这里我们不需要做任何事情,因为全局状态会自动更新
// 但我们仍然需要这个方法,因为 App.vue 会调用它
2026-02-28 14:32:28 +08:00
}
// 页面加载时获取购物车数量
onMounted(() => {
// 检查当前是否是后台页面,如果是则不加载购物车数据
const isAdminPage = window.location.pathname.startsWith('/admin')
if (!isAdminPage) {
2026-02-28 17:09:45 +08:00
// 调用全局更新方法
updateCartCount()
2026-02-28 14:32:28 +08:00
}
})
2026-02-28 17:09:45 +08:00
// 提供一个方法让其他组件调用更新购物车数量
defineExpose({
loadCartCount
})
// 根据当前路由判断激活的导航项
const currentRoute = computed(() => {
2026-03-05 15:15:59 +08:00
if (!route || !route.path) return 'home'
const path = route.path
if (path === '/') return 'home'
if (path === '/products') return 'products'
if (path === '/product') return 'products'
if (path === '/cart') return 'cart'
if (path === '/profile') return 'profile'
if (path === '/wishlist') return 'profile'
return 'home'
})
// 处理导航点击事件
const handleNavClick = (tab: string) => {
if (tab === 'home') {
router.push('/')
} else if (tab === 'products') {
router.push('/products')
} else if (tab === 'cart') {
router.push('/cart')
} else if (tab === 'profile') {
router.push('/profile')
}
}
</script>
<style scoped>
.bottom-nav {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 60px;
background: white;
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.08);
z-index: 1000;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
backface-visibility: hidden;
will-change: transform;
transform: translateZ(0);
transition: none !important;
}
.nav-content {
display: flex;
justify-content: space-around;
align-items: center;
height: 100%;
width: 100%;
max-width: 600px;
padding: 0 10px;
transition: none !important;
}
.nav-item {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 8px 12px;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
border-radius: 12px;
min-width: 60px;
}
.nav-item:hover {
background: rgba(24, 144, 255, 0.05);
transform: translateY(-2px);
}
.nav-item.active {
color: #1890ff;
}
.nav-item.active .nav-icon {
transform: scale(1.1);
}
.nav-icon {
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 4px;
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.nav-label {
font-size: 11px;
font-weight: 500;
transition: all 0.3s ease;
}
.nav-item.active .nav-label {
font-weight: 600;
}
</style>