优惠券的使用功能优化

This commit is contained in:
xiaowang
2026-02-28 17:09:45 +08:00
parent e74dc3faa4
commit c95394260d
9 changed files with 631 additions and 49 deletions
+17 -12
View File
@@ -45,29 +45,28 @@
</template>
<script setup lang="ts">
import { computed, ref, onMounted } from 'vue'
import { computed, inject, onMounted, ref } 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 cartCount = inject('cartCount', ref(0))
// 注入更新购物车数量的方法
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
// 是否有未读通知
const hasNotification = ref(true)
const hasNotification = computed(() => 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)
}
// 这里我们不需要做任何事情,因为全局状态会自动更新
// 但我们仍然需要这个方法,因为 App.vue 会调用它
console.log('BottomNav loadCartCount called')
}
// 页面加载时获取购物车数量
@@ -75,10 +74,16 @@ onMounted(() => {
// 检查当前是否是后台页面,如果是则不加载购物车数据
const isAdminPage = window.location.pathname.startsWith('/admin')
if (!isAdminPage) {
loadCartCount()
// 调用全局更新方法
updateCartCount()
}
})
// 提供一个方法让其他组件调用更新购物车数量
defineExpose({
loadCartCount
})
// 根据当前路由判断激活的导航项
const currentRoute = computed(() => {
const path = route.path