2026-02-13 17:34:32 +08:00
|
|
|
<template>
|
|
|
|
|
<el-config-provider>
|
2026-03-21 14:33:33 +08:00
|
|
|
<ConfigProvider :locale="antdLocale">
|
|
|
|
|
<div class="app-container">
|
|
|
|
|
<BottomNav v-if="!isHiddenNav" />
|
|
|
|
|
<div class="page-content" :class="{ 'no-nav-padding': isHiddenNav }">
|
|
|
|
|
<router-view v-slot="{ Component }">
|
|
|
|
|
<transition name="page-fade" mode="out-in">
|
|
|
|
|
<component :is="Component" :key="route.path" />
|
|
|
|
|
</transition>
|
|
|
|
|
</router-view>
|
|
|
|
|
</div>
|
2026-02-13 17:34:32 +08:00
|
|
|
</div>
|
2026-03-21 14:33:33 +08:00
|
|
|
</ConfigProvider>
|
2026-02-13 17:34:32 +08:00
|
|
|
</el-config-provider>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2026-02-28 17:09:45 +08:00
|
|
|
import { computed, ref, provide, watch } from 'vue'
|
2026-02-13 17:34:32 +08:00
|
|
|
import { useRoute } from 'vue-router'
|
2026-03-21 14:33:33 +08:00
|
|
|
import { ConfigProvider } from 'ant-design-vue'
|
2026-02-13 17:34:32 +08:00
|
|
|
import BottomNav from './components/BottomNav.vue'
|
2026-02-28 17:09:45 +08:00
|
|
|
import { cartApi } from './api/cart'
|
2026-03-21 14:33:33 +08:00
|
|
|
import antdLocale from './config/antdv-locale'
|
2026-02-13 17:34:32 +08:00
|
|
|
|
|
|
|
|
const route = useRoute()
|
|
|
|
|
|
|
|
|
|
const isHiddenNav = computed(() => {
|
2026-02-26 17:54:18 +08:00
|
|
|
// 检查当前路由及其所有父路由是否有 hideNav: true
|
2026-02-28 14:32:28 +08:00
|
|
|
// 或者检查是否是后台页面
|
|
|
|
|
const result = route.matched.some(r => r.meta.hideNav) || route.matched.some(r => r.meta.isAdmin)
|
2026-02-26 17:54:18 +08:00
|
|
|
return result
|
2026-02-13 17:34:32 +08:00
|
|
|
})
|
2026-02-28 17:09:45 +08:00
|
|
|
|
|
|
|
|
// 全局购物车数量状态
|
|
|
|
|
const cartCount = ref(0)
|
|
|
|
|
|
|
|
|
|
// 加载购物车数量
|
|
|
|
|
const loadCartCount = async () => {
|
|
|
|
|
try {
|
2026-03-03 14:39:02 +08:00
|
|
|
// 检查用户是否已登录
|
|
|
|
|
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
|
|
|
|
|
if (!token) {
|
|
|
|
|
cartCount.value = 0
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 10:13:20 +08:00
|
|
|
const response = await cartApi.getCartList() as any
|
|
|
|
|
const data = response || []
|
|
|
|
|
const cartList = Array.isArray(data) ? data : []
|
|
|
|
|
const totalQuantity = cartList.reduce((sum: number, item: any) => sum + (item.quantity || 0), 0)
|
2026-02-28 17:09:45 +08:00
|
|
|
cartCount.value = totalQuantity
|
|
|
|
|
} catch (error) {
|
2026-03-03 14:39:02 +08:00
|
|
|
cartCount.value = 0
|
2026-02-28 17:09:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 提供更新购物车数量的方法
|
|
|
|
|
const updateCartCount = async () => {
|
|
|
|
|
await loadCartCount()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
provide('updateCartCount', updateCartCount)
|
|
|
|
|
provide('cartCount', cartCount)
|
|
|
|
|
|
|
|
|
|
// 监听路由变化,更新购物车数量
|
|
|
|
|
watch(() => route.path, async () => {
|
|
|
|
|
// 检查当前是否是后台页面,如果是则不加载购物车数据
|
|
|
|
|
const isAdminPage = route.matched.some(r => r.meta.isAdmin)
|
|
|
|
|
if (!isAdminPage) {
|
|
|
|
|
await loadCartCount()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 页面加载时获取购物车数量
|
|
|
|
|
loadCartCount()
|
2026-02-13 17:34:32 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
#app {
|
|
|
|
|
font-family: Avenir, Helvetica, Arial, sans-serif;
|
|
|
|
|
-webkit-font-smoothing: antialiased;
|
|
|
|
|
-moz-osx-font-smoothing: grayscale;
|
|
|
|
|
color: #2c3e50;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.app-container {
|
|
|
|
|
min-height: 100vh;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.page-content {
|
|
|
|
|
flex: 1;
|
|
|
|
|
padding-bottom: 60px;
|
|
|
|
|
position: relative;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 当导航栏隐藏时,移除底部 padding */
|
|
|
|
|
.page-content.no-nav-padding {
|
|
|
|
|
padding-bottom: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.fade-enter-active,
|
|
|
|
|
.fade-leave-active {
|
|
|
|
|
transition: opacity 0.2s ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.fade-enter-from {
|
|
|
|
|
opacity: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.fade-leave-to {
|
|
|
|
|
opacity: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.page-fade-enter-active,
|
|
|
|
|
.page-fade-leave-active {
|
|
|
|
|
transition: opacity 0.2s ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.page-fade-enter-from {
|
|
|
|
|
opacity: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.page-fade-leave-to {
|
|
|
|
|
opacity: 0;
|
|
|
|
|
}
|
2026-03-21 14:33:33 +08:00
|
|
|
</style>
|