diff --git a/images/background.png b/images/background.png deleted file mode 100644 index 04267d4..0000000 Binary files a/images/background.png and /dev/null differ diff --git a/pages/compare_price/index.js b/pages/compare_price/index.js index bc26a7d..4bea7e8 100644 --- a/pages/compare_price/index.js +++ b/pages/compare_price/index.js @@ -43,6 +43,22 @@ function processSpecifications(spec) { 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({ data: { // 页面数据 @@ -111,6 +127,22 @@ Page({ if (Array.isArray(goodsData.price)) { 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); } // 处理当前商品的地区信息,只显示省份 @@ -255,124 +287,102 @@ Page({ } // 处理商品数据 - const processedGoods = filteredGoods.map(item => { - // 首先清理 imageUrls 字段(如果存在) - if (item.imageUrls && Array.isArray(item.imageUrls)) { - item.imageUrls = item.imageUrls.map(url => { - return url.trim().replace(/[`]/g, ''); - }); - - // 使用processMediaUrls函数处理媒体数据 - item.mediaItems = processMediaUrls(item.imageUrls); - - // 确保图片优先显示:将图片类型的媒体项移到数组前面 - if (item.mediaItems && item.mediaItems.length > 1) { - const imageItems = item.mediaItems.filter(media => media.type === 'image'); - const videoItems = item.mediaItems.filter(media => media.type === 'video'); - item.mediaItems = [...imageItems, ...videoItems]; - } - } - - // 清理 mediaItems 中的 URL - if (item.mediaItems && Array.isArray(item.mediaItems)) { - item.mediaItems = item.mediaItems.map(media => { - if (media.url) { - // 去除 URL 中的反引号和空格 - media.url = media.url.trim().replace(/[`]/g, ''); - // 确保媒体类型正确 - if (!media.type) { - media.type = isVideoUrl(media.url) ? 'video' : 'image'; - } - } - return media; - }); - } - - // 处理商品价格,使用选中规格的价格 - if (specWeight) { - if (item.weightQuantityData && Array.isArray(item.weightQuantityData)) { - // 尝试从weightQuantityData中找到对应规格的价格 - const matchingSpec = item.weightQuantityData.find(spec => { - if (spec.weightSpec) { - return spec.weightSpec.trim() === specWeight; - } - return false; - }); - if (matchingSpec && matchingSpec.price) { - // 确保价格是一个单一的值,而不是多个价格的组合 - item.price = matchingSpec.price; - } - } - } - - // 清理价格字段,确保只显示单一价格 - 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.processedSpecs = []; - if (item.specification) { - item.processedSpecs = processSpecifications(item.specification); - } else if (item.weightSpec) { - item.processedSpecs = processSpecifications(item.weightSpec); - } else if (item.grossWeight) { - item.processedSpecs = processSpecifications(item.grossWeight); - } else if (selectedSpec) { - item.processedSpecs = processSpecifications(selectedSpec); - } - - // 处理库存显示逻辑 - const quantity = item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; - const totalStock = quantity; - - let displayStock; - if (totalStock >= 10000) { - // 库存>=10000时显示"库存充足" - displayStock = '充足'; - } else if (totalStock === 0) { - // 库存=0时显示"暂无" - displayStock = '暂无'; - } else { - // 其他情况显示具体数字 - displayStock = totalStock; - } - - // 更新商品的库存显示 - item.totalStock = displayStock; - 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; - }); + const processedGoods = filteredGoods.map(item => { + // 首先清理 imageUrls 字段(如果存在) + if (item.imageUrls && Array.isArray(item.imageUrls)) { + item.imageUrls = item.imageUrls.map(url => { + return url.trim().replace(/[`]/g, ''); + }); + + // 使用processMediaUrls函数处理媒体数据 + item.mediaItems = processMediaUrls(item.imageUrls); + + // 确保图片优先显示:将图片类型的媒体项移到数组前面 + if (item.mediaItems && item.mediaItems.length > 1) { + const imageItems = item.mediaItems.filter(media => media.type === 'image'); + const videoItems = item.mediaItems.filter(media => media.type === 'video'); + item.mediaItems = [...imageItems, ...videoItems]; + } + } + + // 清理 mediaItems 中的 URL + if (item.mediaItems && Array.isArray(item.mediaItems)) { + item.mediaItems = item.mediaItems.map(media => { + if (media.url) { + // 去除 URL 中的反引号和空格 + media.url = media.url.trim().replace(/[`]/g, ''); + // 确保媒体类型正确 + if (!media.type) { + media.type = isVideoUrl(media.url) ? 'video' : 'image'; + } + } + return media; + }); + } + + // 处理商品价格,使用选中规格的价格 + if (specWeight) { + if (item.weightQuantityData && Array.isArray(item.weightQuantityData)) { + // 尝试从weightQuantityData中找到对应规格的价格 + const matchingSpec = item.weightQuantityData.find(spec => { + if (spec.weightSpec) { + return spec.weightSpec.trim() === specWeight; + } + return false; + }); + if (matchingSpec && matchingSpec.price) { + // 确保价格是一个单一的值,而不是多个价格的组合 + item.price = matchingSpec.price; + } + } + } + + // 清理价格字段,确保只显示单一价格 + 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.processedSpecs = []; + if (item.specification) { + item.processedSpecs = processSpecifications(item.specification); + } else if (item.weightSpec) { + item.processedSpecs = processSpecifications(item.weightSpec); + } else if (item.grossWeight) { + item.processedSpecs = processSpecifications(item.grossWeight); + } else if (selectedSpec) { + item.processedSpecs = processSpecifications(selectedSpec); + } + + // 处理库存显示逻辑 + const quantity = item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; + const totalStock = quantity; + + let displayStock; + if (totalStock >= 10000) { + // 库存>=10000时显示"库存充足" + displayStock = '充足'; + } else if (totalStock === 0) { + // 库存=0时显示"暂无" + displayStock = '暂无'; + } else { + // 其他情况显示具体数字 + displayStock = totalStock; + } + + // 更新商品的库存显示 + item.totalStock = displayStock; + item.originalTotalStock = totalStock; + + return item; + }); // 显示提示信息 wx.showToast({ @@ -524,73 +534,101 @@ Page({ ); // 清理 mediaItems 中的 URL,去除反引号和空格 - // 同时处理 imageUrls 字段,将其转换为 mediaItems 格式 - // 处理库存显示逻辑 - const cleanedGoods = filteredGoods.map(item => { - // 首先清理 imageUrls 字段(如果存在) - if (item.imageUrls && Array.isArray(item.imageUrls)) { - item.imageUrls = item.imageUrls.map(url => { - return url.trim().replace(/[`]/g, ''); - }); + // 同时处理 imageUrls 字段,将其转换为 mediaItems 格式 + // 处理库存显示逻辑 + const cleanedGoods = filteredGoods.map(item => { + // 首先清理 imageUrls 字段(如果存在) + if (item.imageUrls && Array.isArray(item.imageUrls)) { + item.imageUrls = item.imageUrls.map(url => { + return url.trim().replace(/[`]/g, ''); + }); + + // 使用processMediaUrls函数处理媒体数据 + item.mediaItems = processMediaUrls(item.imageUrls); + + // 确保图片优先显示:将图片类型的媒体项移到数组前面 + if (item.mediaItems && item.mediaItems.length > 1) { + const imageItems = item.mediaItems.filter(media => media.type === 'image'); + const videoItems = item.mediaItems.filter(media => media.type === 'video'); + item.mediaItems = [...imageItems, ...videoItems]; + } + } - // 使用processMediaUrls函数处理媒体数据 - item.mediaItems = processMediaUrls(item.imageUrls); + // 清理 mediaItems 中的 URL + if (item.mediaItems && Array.isArray(item.mediaItems)) { + item.mediaItems = item.mediaItems.map(media => { + if (media.url) { + // 去除 URL 中的反引号和空格 + media.url = media.url.trim().replace(/[`]/g, ''); + // 确保媒体类型正确 + if (!media.type) { + media.type = isVideoUrl(media.url) ? 'video' : 'image'; + } + } + return media; + }); + } - // 确保图片优先显示:将图片类型的媒体项移到数组前面 - if (item.mediaItems && item.mediaItems.length > 1) { - const imageItems = item.mediaItems.filter(media => media.type === 'image'); - const videoItems = item.mediaItems.filter(media => media.type === 'video'); - item.mediaItems = [...imageItems, ...videoItems]; + // 清理价格字段,确保只显示单一价格 + 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); } - } - - // 清理 mediaItems 中的 URL - if (item.mediaItems && Array.isArray(item.mediaItems)) { - item.mediaItems = item.mediaItems.map(media => { - if (media.url) { - // 去除 URL 中的反引号和空格 - media.url = media.url.trim().replace(/[`]/g, ''); - // 确保媒体类型正确 - if (!media.type) { - media.type = isVideoUrl(media.url) ? 'video' : 'image'; - } + + // 处理原始价格 + if (item.originalPrice) { + // 如果原始价格是字符串且包含逗号,只取第一个价格 + if (typeof item.originalPrice === 'string' && item.originalPrice.includes(',')) { + item.originalPrice = item.originalPrice.split(',')[0].trim(); } - return media; - }); - } - - // 处理规格信息,将多个净重信息分割成数组 - item.processedSpecs = []; - if (item.specification) { - item.processedSpecs = processSpecifications(item.specification); - } else if (item.weightSpec) { - item.processedSpecs = processSpecifications(item.weightSpec); - } else if (item.grossWeight) { - item.processedSpecs = processSpecifications(item.grossWeight); - } - - // 处理库存显示逻辑(参考首页的处理方式) - const quantity = item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; - const totalStock = quantity; - - let displayStock; - if (totalStock >= 10000) { - // 库存>=10000时显示"库存充足" - displayStock = '充足'; - } else if (totalStock === 0) { - // 库存=0时显示"暂无" - displayStock = '暂无'; - } else { - // 其他情况显示具体数字 - displayStock = totalStock; - } - - // 更新商品的库存显示 - item.totalStock = displayStock; - item.originalTotalStock = totalStock; - - return item; - }); + // 如果原始价格是数组,只取第一个价格 + if (Array.isArray(item.originalPrice)) { + item.originalPrice = item.originalPrice[0]; + } + // 格式化原始价格,最多显示一位小数 + item.originalPrice = formatPrice(item.originalPrice); + } + + // 处理规格信息,将多个净重信息分割成数组 + item.processedSpecs = []; + if (item.specification) { + item.processedSpecs = processSpecifications(item.specification); + } else if (item.weightSpec) { + item.processedSpecs = processSpecifications(item.weightSpec); + } else if (item.grossWeight) { + item.processedSpecs = processSpecifications(item.grossWeight); + } + + // 处理库存显示逻辑(参考首页的处理方式) + const quantity = item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; + const totalStock = quantity; + + let displayStock; + if (totalStock >= 10000) { + // 库存>=10000时显示"库存充足" + displayStock = '充足'; + } else if (totalStock === 0) { + // 库存=0时显示"暂无" + displayStock = '暂无'; + } else { + // 其他情况显示具体数字 + displayStock = totalStock; + } + + // 更新商品的库存显示 + item.totalStock = displayStock; + item.originalTotalStock = totalStock; + + return item; + }); console.log('过滤后的商品列表:', cleanedGoods); // 检查商品数据结构 diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index fad1ac8..b3a7724 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -338,6 +338,22 @@ function formatDateTime(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) { if (!dateString) return '未知时间'; @@ -793,9 +809,9 @@ Page({ } const priceRange = middlePrice < 20 ? 1 : 5; - const minPrice = middlePrice - priceRange; - const maxPrice = middlePrice + priceRange; - const defaultPrice = parseFloat(middlePrice.toFixed(2)); + const minPrice = formatPrice(middlePrice - priceRange); + const maxPrice = formatPrice(middlePrice + priceRange); + const defaultPrice = formatPrice(middlePrice); const priceThreshold = middlePrice < 20 ? 0.1 : 2; console.log('计算后的价格数据:', { @@ -855,9 +871,9 @@ Page({ } const priceRange = middlePrice < 20 ? 1 : 5; - const minPrice = middlePrice - priceRange; - const maxPrice = middlePrice + priceRange; - const currentPrice = parseFloat(middlePrice.toFixed(2)); + const minPrice = formatPrice(middlePrice - priceRange); + const maxPrice = formatPrice(middlePrice + priceRange); + const currentPrice = formatPrice(middlePrice); const priceThreshold = middlePrice < 20 ? 0.3 : 2; console.log('选择规格 - 价格数据:', { @@ -926,7 +942,7 @@ Page({ if (bargainPrice > minPrice) { const step = bargainPrice < 20 ? 0.1 : 1; - const newPrice = parseFloat((bargainPrice - step).toFixed(2)); + const newPrice = formatPrice(bargainPrice - step); console.log('计算新价格:', newPrice); this.updatePrice(newPrice); } @@ -943,7 +959,7 @@ Page({ if (bargainPrice < maxPrice) { const step = bargainPrice < 20 ? 0.1 : 1; - const newPrice = parseFloat((bargainPrice + step).toFixed(2)); + const newPrice = formatPrice(bargainPrice + step); console.log('计算新价格:', newPrice); this.updatePrice(newPrice); } @@ -954,7 +970,7 @@ Page({ const { minPrice, maxPrice } = this.data; const clampedPrice = Math.max(minPrice, Math.min(maxPrice, newPrice)); const progress = ((clampedPrice - minPrice) / (maxPrice - minPrice)) * 100; - const finalPrice = parseFloat(clampedPrice.toFixed(2)); + const finalPrice = formatPrice(clampedPrice); console.log('更新价格:', { inputPrice: newPrice, @@ -1018,8 +1034,10 @@ Page({ if (!isNaN(price)) { const clampedPrice = Math.max(minPrice, Math.min(maxPrice, price)); + const formattedPrice = formatPrice(clampedPrice); console.log('计算后的价格:', clampedPrice); - this.updatePrice(clampedPrice); + console.log('格式化后的价格:', formattedPrice); + this.updatePrice(formattedPrice); } this.setData({ @@ -1056,11 +1074,7 @@ Page({ const priceRange = maxPrice - minPrice; let price = minPrice + (progress / 100) * priceRange; - if (bargainPrice < 20) { - price = Math.round(price * 10) / 10; - } else { - price = Math.round(price); - } + price = formatPrice(price); const clampedPrice = Math.max(minPrice, Math.min(maxPrice, price)); diff --git a/pages/goods-detail/goods-detail.wxml b/pages/goods-detail/goods-detail.wxml index cc60b51..8ef0915 100644 --- a/pages/goods-detail/goods-detail.wxml +++ b/pages/goods-detail/goods-detail.wxml @@ -73,7 +73,7 @@ - + 售空 {{goodsDetail.supplyStatus}} @@ -86,7 +86,7 @@ - + {{goodsDetail.description || '暂无描述'}} diff --git a/pages/goods-detail/goods-detail.wxss b/pages/goods-detail/goods-detail.wxss index 723c559..d7c7b71 100644 --- a/pages/goods-detail/goods-detail.wxss +++ b/pages/goods-detail/goods-detail.wxss @@ -250,7 +250,7 @@ video.slider-media .wx-video-volume-icon { /* 商品详细信息网格 */ .info-grid { background-color: #ffffff; - margin: 8px 0; + margin: 8rpx 16rpx; padding: 12px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); } @@ -313,9 +313,9 @@ video.slider-media .wx-video-volume-icon { /* 联系信息 */ .contact-info { - margin: 8px 16px; /* 减小外边距 */ - padding: 12px; /* 减小内边距 */ - border-radius: 10px; /* 减小圆角 */ + margin: 8rpx 16rpx; + padding: 12px; + border-radius: 10px; background: #ffffff; border: 1px solid #d6e4ff; 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 { background-color: #ffffff; - margin: 16rpx; - padding: 24rpx; + margin: 8rpx 16rpx; + padding: 16rpx; border-radius: 12rpx; box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08); border: 1rpx solid #f0f0f0; @@ -1141,7 +1141,7 @@ video.slider-media .wx-video-volume-icon { /* 内部信息样式 */ .internal-info { - margin: 8px 16px; + margin: 8rpx 16rpx; padding: 16px; border-radius: 10px; background: #f0f5ff; @@ -2263,7 +2263,7 @@ video.slider-media .wx-video-volume-icon { /* 内部信息样式 */ .internal-info { - margin: 8px 16px; + margin: 8rpx 16rpx; padding: 16px; border-radius: 10px; background: #f0f5ff;