Trae AI 5 days ago
parent
commit
a89f4a18de
  1. BIN
      images/background.png
  2. 82
      pages/compare_price/index.js
  3. 44
      pages/goods-detail/goods-detail.js
  4. 4
      pages/goods-detail/goods-detail.wxml
  5. 16
      pages/goods-detail/goods-detail.wxss

BIN
images/background.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 672 KiB

82
pages/compare_price/index.js

@ -43,6 +43,22 @@ function processSpecifications(spec) {
return specs; return specs;
} }
// 格式化价格,最多显示一位小数
function formatPrice(price) {
if (price === null || price === undefined) {
return price;
}
// 转换为数字
const numPrice = parseFloat(price);
if (isNaN(numPrice)) {
return price;
}
// 格式化到一位小数
return numPrice.toFixed(1);
}
Page({ Page({
data: { data: {
// 页面数据 // 页面数据
@ -111,6 +127,22 @@ Page({
if (Array.isArray(goodsData.price)) { if (Array.isArray(goodsData.price)) {
goodsData.price = goodsData.price[0]; goodsData.price = goodsData.price[0];
} }
// 格式化价格,最多显示一位小数
goodsData.price = formatPrice(goodsData.price);
}
// 处理原始价格
if (goodsData.originalPrice) {
// 如果原始价格是字符串且包含逗号,只取第一个价格
if (typeof goodsData.originalPrice === 'string' && goodsData.originalPrice.includes(',')) {
goodsData.originalPrice = goodsData.originalPrice.split(',')[0].trim();
}
// 如果原始价格是数组,只取第一个价格
if (Array.isArray(goodsData.originalPrice)) {
goodsData.originalPrice = goodsData.originalPrice[0];
}
// 格式化原始价格,最多显示一位小数
goodsData.originalPrice = formatPrice(goodsData.originalPrice);
} }
// 处理当前商品的地区信息,只显示省份 // 处理当前商品的地区信息,只显示省份
@ -349,28 +381,6 @@ Page({
item.totalStock = displayStock; item.totalStock = displayStock;
item.originalTotalStock = totalStock; item.originalTotalStock = totalStock;
// 处理地区信息,只显示省份
if (item.region) {
// 提取省份信息
const provinceRegex = /^([^省]+省|[^自治区]+自治区|[^直辖市]+直辖市|[^特别行政区]+特别行政区)/;
const match = item.region.match(provinceRegex);
if (match) {
item.region = match[1];
}
}
// 计算价格涨幅
if (goodsData.price) {
const currentPrice = parseFloat(goodsData.price);
const itemPrice = parseFloat(item.price);
if (!isNaN(currentPrice) && !isNaN(itemPrice)) {
const priceDiff = itemPrice - currentPrice;
const pricePercent = ((priceDiff / currentPrice) * 100).toFixed(1);
item.priceDiff = priceDiff;
item.pricePercent = pricePercent;
}
}
return item; return item;
}); });
@ -559,6 +569,34 @@ Page({
}); });
} }
// 清理价格字段,确保只显示单一价格
if (item.price) {
// 如果价格是字符串且包含逗号,只取第一个价格
if (typeof item.price === 'string' && item.price.includes(',')) {
item.price = item.price.split(',')[0].trim();
}
// 如果价格是数组,只取第一个价格
if (Array.isArray(item.price)) {
item.price = item.price[0];
}
// 格式化价格,最多显示一位小数
item.price = formatPrice(item.price);
}
// 处理原始价格
if (item.originalPrice) {
// 如果原始价格是字符串且包含逗号,只取第一个价格
if (typeof item.originalPrice === 'string' && item.originalPrice.includes(',')) {
item.originalPrice = item.originalPrice.split(',')[0].trim();
}
// 如果原始价格是数组,只取第一个价格
if (Array.isArray(item.originalPrice)) {
item.originalPrice = item.originalPrice[0];
}
// 格式化原始价格,最多显示一位小数
item.originalPrice = formatPrice(item.originalPrice);
}
// 处理规格信息,将多个净重信息分割成数组 // 处理规格信息,将多个净重信息分割成数组
item.processedSpecs = []; item.processedSpecs = [];
if (item.specification) { if (item.specification) {

44
pages/goods-detail/goods-detail.js

@ -338,6 +338,22 @@ function formatDateTime(dateString) {
return dateString; return dateString;
} }
// 格式化价格,解决浮点数精度问题
function formatPrice(price) {
if (price === null || price === undefined) {
return price;
}
// 转换为数字
const numPrice = parseFloat(price);
if (isNaN(numPrice)) {
return price;
}
// 格式化到一位小数
return parseFloat(numPrice.toFixed(1));
}
// 格式化北京时间的函数 // 格式化北京时间的函数
function formatBeijingTime(dateString) { function formatBeijingTime(dateString) {
if (!dateString) return '未知时间'; if (!dateString) return '未知时间';
@ -793,9 +809,9 @@ Page({
} }
const priceRange = middlePrice < 20 ? 1 : 5; const priceRange = middlePrice < 20 ? 1 : 5;
const minPrice = middlePrice - priceRange; const minPrice = formatPrice(middlePrice - priceRange);
const maxPrice = middlePrice + priceRange; const maxPrice = formatPrice(middlePrice + priceRange);
const defaultPrice = parseFloat(middlePrice.toFixed(2)); const defaultPrice = formatPrice(middlePrice);
const priceThreshold = middlePrice < 20 ? 0.1 : 2; const priceThreshold = middlePrice < 20 ? 0.1 : 2;
console.log('计算后的价格数据:', { console.log('计算后的价格数据:', {
@ -855,9 +871,9 @@ Page({
} }
const priceRange = middlePrice < 20 ? 1 : 5; const priceRange = middlePrice < 20 ? 1 : 5;
const minPrice = middlePrice - priceRange; const minPrice = formatPrice(middlePrice - priceRange);
const maxPrice = middlePrice + priceRange; const maxPrice = formatPrice(middlePrice + priceRange);
const currentPrice = parseFloat(middlePrice.toFixed(2)); const currentPrice = formatPrice(middlePrice);
const priceThreshold = middlePrice < 20 ? 0.3 : 2; const priceThreshold = middlePrice < 20 ? 0.3 : 2;
console.log('选择规格 - 价格数据:', { console.log('选择规格 - 价格数据:', {
@ -926,7 +942,7 @@ Page({
if (bargainPrice > minPrice) { if (bargainPrice > minPrice) {
const step = bargainPrice < 20 ? 0.1 : 1; const step = bargainPrice < 20 ? 0.1 : 1;
const newPrice = parseFloat((bargainPrice - step).toFixed(2)); const newPrice = formatPrice(bargainPrice - step);
console.log('计算新价格:', newPrice); console.log('计算新价格:', newPrice);
this.updatePrice(newPrice); this.updatePrice(newPrice);
} }
@ -943,7 +959,7 @@ Page({
if (bargainPrice < maxPrice) { if (bargainPrice < maxPrice) {
const step = bargainPrice < 20 ? 0.1 : 1; const step = bargainPrice < 20 ? 0.1 : 1;
const newPrice = parseFloat((bargainPrice + step).toFixed(2)); const newPrice = formatPrice(bargainPrice + step);
console.log('计算新价格:', newPrice); console.log('计算新价格:', newPrice);
this.updatePrice(newPrice); this.updatePrice(newPrice);
} }
@ -954,7 +970,7 @@ Page({
const { minPrice, maxPrice } = this.data; const { minPrice, maxPrice } = this.data;
const clampedPrice = Math.max(minPrice, Math.min(maxPrice, newPrice)); const clampedPrice = Math.max(minPrice, Math.min(maxPrice, newPrice));
const progress = ((clampedPrice - minPrice) / (maxPrice - minPrice)) * 100; const progress = ((clampedPrice - minPrice) / (maxPrice - minPrice)) * 100;
const finalPrice = parseFloat(clampedPrice.toFixed(2)); const finalPrice = formatPrice(clampedPrice);
console.log('更新价格:', { console.log('更新价格:', {
inputPrice: newPrice, inputPrice: newPrice,
@ -1018,8 +1034,10 @@ Page({
if (!isNaN(price)) { if (!isNaN(price)) {
const clampedPrice = Math.max(minPrice, Math.min(maxPrice, price)); const clampedPrice = Math.max(minPrice, Math.min(maxPrice, price));
const formattedPrice = formatPrice(clampedPrice);
console.log('计算后的价格:', clampedPrice); console.log('计算后的价格:', clampedPrice);
this.updatePrice(clampedPrice); console.log('格式化后的价格:', formattedPrice);
this.updatePrice(formattedPrice);
} }
this.setData({ this.setData({
@ -1056,11 +1074,7 @@ Page({
const priceRange = maxPrice - minPrice; const priceRange = maxPrice - minPrice;
let price = minPrice + (progress / 100) * priceRange; let price = minPrice + (progress / 100) * priceRange;
if (bargainPrice < 20) { price = formatPrice(price);
price = Math.round(price * 10) / 10;
} else {
price = Math.round(price);
}
const clampedPrice = Math.max(minPrice, Math.min(maxPrice, price)); const clampedPrice = Math.max(minPrice, Math.min(maxPrice, price));

4
pages/goods-detail/goods-detail.wxml

@ -73,7 +73,7 @@
<!-- 商品基本信息 --> <!-- 商品基本信息 -->
<view class="goods-info"> <view class="goods-info">
<view style="display: flex; flex-direction: column; margin-bottom: 10rpx;"> <view style="display: flex; flex-direction: column; margin-bottom: 10rpx;">
<view style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 8rpx;"> <view style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 0rpx;">
<view style="display: flex; align-items: center; flex: 1;"> <view style="display: flex; align-items: center; flex: 1;">
<view wx:if="{{goodsDetail.status === 'sold_out'}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: linear-gradient(135deg, #8c8c8c 0%, #a6a6a6 100%); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -20rpx;">售空</view> <view wx:if="{{goodsDetail.status === 'sold_out'}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: linear-gradient(135deg, #8c8c8c 0%, #a6a6a6 100%); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -20rpx;">售空</view>
<view wx:elif="{{goodsDetail.supplyStatus}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: rgba(218, 165, 32, 0.8); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -20rpx;">{{goodsDetail.supplyStatus}}</view> <view wx:elif="{{goodsDetail.supplyStatus}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: rgba(218, 165, 32, 0.8); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -20rpx;">{{goodsDetail.supplyStatus}}</view>
@ -86,7 +86,7 @@
</view> </view>
</view> </view>
</view> </view>
<view class="source-description" style="padding: 0rpx 16rpx; background: #f5f5f5; border-radius: 8rpx; font-size: 28rpx; color: #333; margin-top: 4rpx; height: 68rpx; display: block; box-sizing: border-box"> <view class="source-description" style="background: #f5f5f5; font-size: 28rpx; color: #333; height: 43rpx; display: block; box-sizing: border-box">
{{goodsDetail.description || '暂无描述'}} {{goodsDetail.description || '暂无描述'}}
</view> </view>
</view> </view>

16
pages/goods-detail/goods-detail.wxss

@ -250,7 +250,7 @@ video.slider-media .wx-video-volume-icon {
/* 商品详细信息网格 */ /* 商品详细信息网格 */
.info-grid { .info-grid {
background-color: #ffffff; background-color: #ffffff;
margin: 8px 0; margin: 8rpx 16rpx;
padding: 12px; padding: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
} }
@ -313,9 +313,9 @@ video.slider-media .wx-video-volume-icon {
/* 联系信息 */ /* 联系信息 */
.contact-info { .contact-info {
margin: 8px 16px; /* 减小外边距 */ margin: 8rpx 16rpx;
padding: 12px; /* 减小内边距 */ padding: 12px;
border-radius: 10px; /* 减小圆角 */ border-radius: 10px;
background: #ffffff; background: #ffffff;
border: 1px solid #d6e4ff; border: 1px solid #d6e4ff;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08); box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
@ -724,8 +724,8 @@ video.slider-media .wx-video-volume-icon {
/* 净重件数对应信息样式 */ /* 净重件数对应信息样式 */
.weight-quantity-info { .weight-quantity-info {
background-color: #ffffff; background-color: #ffffff;
margin: 16rpx; margin: 8rpx 16rpx;
padding: 24rpx; padding: 16rpx;
border-radius: 12rpx; border-radius: 12rpx;
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08); box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
border: 1rpx solid #f0f0f0; border: 1rpx solid #f0f0f0;
@ -1141,7 +1141,7 @@ video.slider-media .wx-video-volume-icon {
/* 内部信息样式 */ /* 内部信息样式 */
.internal-info { .internal-info {
margin: 8px 16px; margin: 8rpx 16rpx;
padding: 16px; padding: 16px;
border-radius: 10px; border-radius: 10px;
background: #f0f5ff; background: #f0f5ff;
@ -2263,7 +2263,7 @@ video.slider-media .wx-video-volume-icon {
/* 内部信息样式 */ /* 内部信息样式 */
.internal-info { .internal-info {
margin: 8px 16px; margin: 8rpx 16rpx;
padding: 16px; padding: 16px;
border-radius: 10px; border-radius: 10px;
background: #f0f5ff; background: #f0f5ff;

Loading…
Cancel
Save