feat: 商品详情的价格的计算处理

This commit is contained in:
2025-09-16 20:39:17 +08:00
parent 8d0c93206b
commit 6443b67d15
5 changed files with 142 additions and 13 deletions

View File

@@ -38,7 +38,7 @@
<view class="footer">
<view class="left">
<text class="label">价格</text>
<text class="price">{{ goodsData.specificationPrice || 399 }}</text>
<text class="price">{{ calculatedTotalPrice }}</text>
</view>
<view class="buy-button" @click="showConfirmPopup">立即抢购</view>
</view>
@@ -103,20 +103,34 @@ const selectedDate = ref({
});
const priceData = ref([]);
// 计算的总价格
const calculatedTotalPrice = ref(0);
// 获取商品详情数据
const goodsInfo = async (params) => {
const res = await goodsDetail(params);
goodsData.value = res.data;
// 初始化计算价格
calculatedTotalPrice.value = goodsData.value.specificationPrice || 0;
// 判断是酒店类型订单再获取获取商品日价格及库存
if (goodsData.value.commodityTypeCode === "0") {
configGoodsData();
getGoodsDailyPrice({
commodityId: goodsData.value.commodityId,
});
}
};
const configGoodsData = () => {
goodsData.value.startDate = selectedDate.value.startDate;
goodsData.value.endDate = selectedDate.value.endDate;
goodsData.value.totalDays = selectedDate.value.totalDays;
goodsData.value.calculatedTotalPrice = calculatedTotalPrice.value;
};
// 获取商品日价格及库存
const getGoodsDailyPrice = async (params) => {
const res = await commodityDailyPriceList(params);
@@ -272,6 +286,63 @@ const handleDateSelect = (data) => {
totalDays: data.totalDays,
};
// 根据商品类型计算价格
if (goodsData.value.commodityTypeCode === "0") {
// 酒店类型计算dateRange总价格排除最后一天同一天除外
if (data.dateRange && Array.isArray(data.dateRange)) {
// 获取默认价格作为回退值
const defaultPrice = Number(goodsData.value.specificationPrice) || 0;
// 检查价格是否有效的函数
const isValidPrice = (price) => {
return (
price !== null &&
price !== undefined &&
price !== "" &&
price !== "-" &&
!isNaN(Number(price)) &&
Number(price) > 0
);
};
// 如果是同一天数组长度为1使用该天的价格
if (data.dateRange.length === 1) {
const dayPrice = data.dateRange[0].price;
calculatedTotalPrice.value = isValidPrice(dayPrice)
? Number(dayPrice)
: defaultPrice;
console.log(
"同一天选择,价格:",
calculatedTotalPrice.value,
"(原价格:",
dayPrice,
")"
);
} else {
// 多天选择,排除最后一天
const dateRangeExcludingLast = data.dateRange.slice(0, -1);
calculatedTotalPrice.value = dateRangeExcludingLast.reduce(
(total, dateItem) => {
const dayPrice = dateItem.price;
const priceToAdd = isValidPrice(dayPrice)
? Number(dayPrice)
: defaultPrice;
return total + priceToAdd;
},
0
);
console.log(
"酒店类型计算总价格(排除最后一天):",
calculatedTotalPrice.value
);
}
configGoodsData();
}
} else {
// 非酒店类型如果没有dateRange使用默认价格
calculatedTotalPrice.value = goodsData.value.specificationPrice || 0;
}
// 日历组件会自动关闭,无需手动设置
calendarVisible.value = false;
};