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

@@ -93,9 +93,7 @@ const props = defineProps({
// 价格数据数组
priceData: {
type: Array,
default: () => [
{date: '', price: '-', stock: '0'}
],
default: () => [{ date: "", price: "-", stock: "0" }],
},
// 默认选中日期
@@ -209,8 +207,8 @@ const getPriceForDate = (dateStr) => {
if (!props.priceData || !Array.isArray(props.priceData)) {
return null;
}
const priceItem = props.priceData.find(item => item.date === dateStr);
const priceItem = props.priceData.find((item) => item.date === dateStr);
return priceItem ? priceItem.price : null;
};
@@ -387,22 +385,58 @@ const handleRangeSelection = (dateInfo) => {
return;
}
// 生成范围内所有日期的数组
const dateRange = generateDateRange(rangeStart.value, rangeEnd.value);
emit("range-select", {
startDate: rangeStart.value,
endDate: rangeEnd.value,
startPrice: getPriceForDate(rangeStart.value),
endPrice: getPriceForDate(rangeEnd.value),
totalDays: daysBetween,
dateRange: dateRange, // 新增:范围内所有日期的数组
});
}
};
// 生成日期范围内所有日期的数组
const generateDateRange = (startDate, endDate) => {
const dateRange = [];
const start = new Date(startDate);
const end = new Date(endDate);
// 如果是同一天,只返回一个日期
if (startDate === endDate) {
return [
{
date: startDate,
price: getPriceForDate(startDate),
},
];
}
// 生成范围内所有日期
const current = new Date(start);
while (current <= end) {
const dateStr = current.toISOString().split("T")[0];
dateRange.push({
date: dateStr,
price: getPriceForDate(dateStr),
});
current.setDate(current.getDate() + 1);
}
return dateRange;
};
// 计算两个日期之间的天数
const calculateDaysBetween = (startDate, endDate) => {
const start = new Date(startDate);
const end = new Date(endDate);
const diffTime = Math.abs(end - start);
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
const days = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
// 如果是同一天返回1天而不是0天
return days === 0 ? 1 : days;
};
// 监听visible属性变化控制uni-popup显示
@@ -441,4 +475,4 @@ onBeforeUnmount(() => {
<style lang="scss" scoped>
// 引入样式文件
@import "./styles/index.scss";
</style>
</style>

View File

@@ -1,7 +1,7 @@
// 颜色系统
$primary-color: #1890ff;
$primary-light: #e6f7ff;
$primary-dark: #0050b3;
$primary-dark: #1890ff;
// 中性色
$text-primary: #262626;