hookehuyr

fix: 修复购物车总价和价格格式化中的空值问题

修复了购物车总价计算中可能存在的空值问题,确保当 `item.price` 或 `item.quantity` 为空时不会导致计算错误。同时,修复了价格格式化函数中对空值的处理,避免显示异常。
......@@ -93,7 +93,7 @@ export function provideCart(mode = CartMode.MULTIPLE) {
// 计算购物车总价
function getTotalPrice() {
return cartItems.value.reduce(
(total, item) => total + item.price * item.quantity,
(total, item) => total + ((item.price ?? 0) * (item.quantity ?? 0)),
0
)
}
......
......@@ -45,7 +45,7 @@
<div class="flex flex-1">
<div class="w-12 h-12 rounded-lg overflow-hidden mr-3 flex-shrink-0">
<img
:src="item.imageUrl || '/assets/images/course-placeholder.jpg'"
:src="item.cover || 'https://cdn.ipadbiz.cn/mlaj/images/default_block.png'"
:alt="item.title"
class="w-full h-full object-cover"
@error="handleImageError"
......@@ -54,7 +54,7 @@
<div class="flex-1">
<p class="font-medium text-sm line-clamp-1">{{ item.title }}</p>
<p class="text-xs text-gray-500">
{{ item.type === 'course' ? '课程' : '活动' }} · {{ item.quantity }} 份
{{ item.type === 'course' ? '课程' : '活动' }} · {{ item?.quantity }} 份
</p>
</div>
<button
......@@ -72,7 +72,7 @@
</button>
</div>
<div class="ml-2 text-right">
<p class="font-medium text-sm">{{ formatPrice(item.price * item.quantity) }}</p>
<p class="font-medium text-sm">{{ formatPrice(item?.price * item.quantity) }}</p>
<p class="text-xs text-gray-500">{{ formatPrice(item.price) }} / 份</p>
</div>
</div>
......@@ -302,7 +302,8 @@ const itemToDelete = ref(null)
// Format price with Chinese Yuan symbol
const formatPrice = (price) => {
return `¥${price.toFixed(2)}`
if (!price && price !== 0) return '¥0.00'
return `¥${Number(price).toFixed(2)}`
}
// Handle image error
......