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

181 lines
4.6 KiB
Vue

<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>
<!-- 商品导航项 -->
<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>
<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">
import { computed, ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { HomeOutlined, ShoppingOutlined, ShoppingCartOutlined, UserOutlined } from '@ant-design/icons-vue'
import { cartApi } from '../api/cart'
import { message } from 'ant-design-vue'
const router = useRouter()
const route = useRoute()
// 购物车商品数量
const cartCount = ref(0)
// 是否有未读通知
const hasNotification = ref(true)
// 加载购物车数量
const loadCartCount = async () => {
try {
const data = await cartApi.getCartList()
cartCount.value = data.reduce((sum: number, item: any) => sum + item.quantity, 0)
} catch (error) {
console.error('获取购物车数量失败:', error)
}
}
// 页面加载时获取购物车数量
onMounted(() => {
// 检查当前是否是后台页面,如果是则不加载购物车数据
const isAdminPage = window.location.pathname.startsWith('/admin')
if (!isAdminPage) {
loadCartCount()
}
})
// 根据当前路由判断激活的导航项
const currentRoute = computed(() => {
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>