160 lines
3.6 KiB
Vue
160 lines
3.6 KiB
Vue
<template>
|
|
<el-config-provider>
|
|
<ConfigProvider :locale="antdLocale">
|
|
<div class="app-container">
|
|
<BottomNav v-if="!isHiddenNav" />
|
|
<div class="page-content" :class="{ 'no-nav-padding': isHiddenNav }">
|
|
<div
|
|
class="page-renderer"
|
|
:class="{
|
|
'centered-user-shell': shouldCenterUserContent,
|
|
'admin-renderer': isAdminRoute
|
|
}"
|
|
>
|
|
<router-view v-slot="{ Component }">
|
|
<transition name="page-fade" mode="out-in">
|
|
<component :is="Component" :key="route.path" />
|
|
</transition>
|
|
</router-view>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</ConfigProvider>
|
|
</el-config-provider>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, ref, provide, watch } from 'vue'
|
|
import { useRoute } from 'vue-router'
|
|
import { ConfigProvider } from 'ant-design-vue'
|
|
import BottomNav from './components/BottomNav.vue'
|
|
import { cartApi } from './api/cart'
|
|
import antdLocale from './config/antdv-locale'
|
|
|
|
const route = useRoute()
|
|
|
|
const isHiddenNav = computed(() => {
|
|
// 检查当前路由及其所有父路由是否有 hideNav: true
|
|
// 或者检查是否是后台页面
|
|
const result = route.matched.some(r => r.meta.hideNav) || route.matched.some(r => r.meta.isAdmin)
|
|
return result
|
|
})
|
|
|
|
const isAdminRoute = computed(() => route.matched.some(r => r.meta.isAdmin))
|
|
|
|
const shouldCenterUserContent = computed(() => {
|
|
if (isAdminRoute.value) {
|
|
return false
|
|
}
|
|
|
|
return route.name !== 'auth' && route.name !== 'not-found'
|
|
})
|
|
|
|
// 全局购物车数量状态
|
|
const cartCount = ref(0)
|
|
|
|
// 加载购物车数量
|
|
const loadCartCount = async () => {
|
|
try {
|
|
// 检查用户是否已登录
|
|
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
|
|
if (!token) {
|
|
cartCount.value = 0
|
|
return
|
|
}
|
|
|
|
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)
|
|
cartCount.value = totalQuantity
|
|
} catch (error) {
|
|
cartCount.value = 0
|
|
}
|
|
}
|
|
|
|
// 提供更新购物车数量的方法
|
|
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()
|
|
</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;
|
|
}
|
|
|
|
.page-renderer {
|
|
min-height: 100%;
|
|
}
|
|
|
|
.page-renderer.centered-user-shell > * {
|
|
width: 100%;
|
|
max-width: var(--frontend-page-max-width);
|
|
margin: 0 auto;
|
|
}
|
|
|
|
/* 当导航栏隐藏时,移除底部 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;
|
|
}
|
|
</style>
|