From 3f844a2c43a331642c7a99a58c084905b024f95b Mon Sep 17 00:00:00 2001 From: Trae AI Date: Tue, 3 Mar 2026 14:39:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=AF=94=E4=BB=B7=E6=A0=BCui=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/compare_price/index.js | 88 +++++++++++++++++++++++++++- pages/compare_price/index.wxml | 11 +++- pages/compare_price/index.wxss | 19 +++++- pages/goods-detail/goods-detail.js | 13 ++-- pages/goods-detail/goods-detail.wxml | 1 - 5 files changed, 121 insertions(+), 11 deletions(-) diff --git a/pages/compare_price/index.js b/pages/compare_price/index.js index 4b50681..e41e51a 100644 --- a/pages/compare_price/index.js +++ b/pages/compare_price/index.js @@ -59,6 +59,64 @@ function formatPrice(price) { return numPrice.toFixed(1); } +// 计算单位价格 +function calculateUnitPrice(price, spec) { + if (!price || !spec) { + return null; + } + + // 转换价格为数字 + const numPrice = parseFloat(price); + if (isNaN(numPrice)) { + return null; + } + + // 检查是否需要计算单位价格(价格低于10元) + if (numPrice >= 10) { + return null; + } + + // 提取重量范围 + const weightMatch = spec.match(/(\d+)-(\d+)/); + if (!weightMatch) { + return null; + } + + const minWeight = parseFloat(weightMatch[1]); + const maxWeight = parseFloat(weightMatch[2]); + if (isNaN(minWeight) || isNaN(maxWeight)) { + return null; + } + + // 计算平均重量 + const avgWeight = (minWeight + maxWeight) / 2; + + // 检查是否是毛重 + const isGrossWeight = spec.includes('毛重'); + + // 计算单位价格 + let unitPrice; + if (isGrossWeight) { + unitPrice = (avgWeight - 5) * numPrice; + } else { + unitPrice = avgWeight * numPrice; + } + + return unitPrice.toFixed(1); +} + +// 获取商品状态信息 +function getStatusInfo(status) { + switch (status) { + case 'published': + return { text: '在售', color: '#10B981' }; + case 'sold_out': + return { text: '售罄', color: '#666666' }; + default: + return { text: '未知', color: '#666666' }; + } +} + Page({ data: { // 页面数据 @@ -142,6 +200,9 @@ Page({ } } + // 计算当前商品的单位价格 + goodsData.unitPrice = calculateUnitPrice(goodsData.price, specWeight || goodsData.specification); + // 保存选择的种类、规格和当前商品数据到页面数据 this.setData({ selectedCategory: selectedCategory, @@ -695,10 +756,30 @@ Page({ } } + // 计算单位价格 + item.unitPrice = calculateUnitPrice(item.price, item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : '')); + // 计算价格涨幅 if (goodsData.price) { - const currentPrice = parseFloat(goodsData.price); - const itemPrice = parseFloat(item.price); + // 获取当前商品的价格(使用单位价格如果低于10元) + let currentPrice = parseFloat(goodsData.price); + if (goodsData.unitPrice) { + currentPrice = parseFloat(goodsData.unitPrice); + } + + // 获取对比商品的价格(根据规则使用合适的价格) + let itemPrice = parseFloat(item.price); + if (currentPrice >= 10 && item.unitPrice) { + // 如果当前商品价格高于10元,对比商品使用单位价格 + itemPrice = parseFloat(item.unitPrice); + } else if (currentPrice < 10 && itemPrice < 10) { + // 如果两者都低于10元,使用原始价格 + itemPrice = parseFloat(item.price); + } else if (currentPrice < 10 && itemPrice >= 10) { + // 如果当前商品低于10元,对比商品高于10元,当前商品使用单位价格 + currentPrice = parseFloat(goodsData.unitPrice || goodsData.price); + } + if (!isNaN(currentPrice) && !isNaN(itemPrice)) { const priceDiff = itemPrice - currentPrice; const pricePercent = ((priceDiff / currentPrice) * 100).toFixed(1); @@ -823,6 +904,9 @@ Page({ } } + // 计算当前商品的单位价格 + currentGoods.unitPrice = calculateUnitPrice(currentGoods.price, specWeight || currentGoods.specification); + // 更新currentGoods this.setData({ currentGoods: currentGoods diff --git a/pages/compare_price/index.wxml b/pages/compare_price/index.wxml index 5d272e7..42bd054 100644 --- a/pages/compare_price/index.wxml +++ b/pages/compare_price/index.wxml @@ -46,14 +46,16 @@ - 品牌 + {{currentGoods.category || '品种'}} {{currentGoods.productName || currentGoods.name || '商品'}} + {{currentGoods.status === 'published' ? '在售' : currentGoods.status === 'sold_out' ? '售罄' : '未知'}} ¥ {{currentGoods.price}} + 件价: ¥{{currentGoods.unitPrice}} - {{currentGoods.specification || selectedSpec || '净重43-44g'}} | {{currentGoods.yolk || '绿壳'}} | 库存:{{currentGoods.totalStock || '450'}} + {{currentGoods.specification || selectedSpec || '净重43-44g'}} | {{currentGoods.category || '绿壳'}} | 库存:{{currentGoods.totalStock || '450'}} 🏪 @@ -86,13 +88,16 @@ bindtap="navigateToGoodsDetail" > + {{item.category || '品种'}} {{item.productName || item.name}} + {{item.status === 'published' ? '在售' : item.status === 'sold_out' ? '售罄' : '未知'}} ¥ {{item.price}} + 件价: ¥{{item.unitPrice}} - {{item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : '净重:45-50g')}} | {{item.category || '绿壳'}} | {{item.yolk || '无'}} | 库存:{{item.totalStock || '450'}} + {{item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : '净重:45-50g')}} | {{item.category || '品种'}} | {{item.yolk || '无'}} | 库存:{{item.totalStock || '450'}} 🏪 {{item.sourceType || '生态农场'}} diff --git a/pages/compare_price/index.wxss b/pages/compare_price/index.wxss index 3ebd079..ca727c7 100644 --- a/pages/compare_price/index.wxss +++ b/pages/compare_price/index.wxss @@ -327,6 +327,15 @@ flex-shrink: 0; } +.status-badge { + background-color: #F3F4F6; + border-radius: 9999px; + padding: 2px 8px; + font-size: 12px; + flex-shrink: 0; + font-weight: 500; +} + .goods-name { font-size: 16px; color: #1A1A1A; @@ -351,6 +360,13 @@ font-weight: bold; } +.unit-price { + font-size: 12px; + color: #666666; + margin-left: 8px; + white-space: nowrap; +} + .spec-info { font-size: 14px; color: #666666; @@ -416,8 +432,8 @@ .compare-item-header { display: flex; align-items: center; - justify-content: space-between; gap: 12px; + flex-wrap: wrap; } .compare-item-name { @@ -425,6 +441,7 @@ color: #1A1A1A; font-weight: 500; flex: 1; + min-width: 0; white-space: normal; word-break: break-word; line-height: 1.4; diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index 265edac..7b63b11 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -3830,8 +3830,12 @@ Page({ const currentGoods = this.data.goodsDetail; const currentSpecifications = currentGoods.weightQuantityData || []; - // 品种默认选择全部品种 - const selectedCategory = '全部'; + // 品种默认选择当前商品的品种 + const selectedCategory = currentGoods.category || '全部'; + + // 找到当前商品品种在选项中的索引 + const categoryIndex = this.data.categoryOptions.indexOf(selectedCategory); + const selectedCategoryIndex = categoryIndex !== -1 ? categoryIndex : 0; // 检查规格数量 if (currentSpecifications.length === 1) { @@ -3879,6 +3883,7 @@ Page({ showCompareModal: true, compareStep: 2, // 直接显示规格选择步骤 selectedCategory: selectedCategory, + selectedCategoryIndex: selectedCategoryIndex, selectedSpecIndex: 0, selectedSpec: currentSpecifications.length > 0 ? currentSpecifications[0] : null, currentSpecifications: currentSpecifications @@ -3972,8 +3977,8 @@ Page({ } const currentGoods = this.data.goodsDetail; - // 品种默认选择全部品种 - const selectedCategory = '全部'; + // 使用已选择的品种 + const selectedCategory = this.data.selectedCategory || currentGoods.category || '全部'; const selectedSpec = this.data.selectedSpec; // 显示提示信息 diff --git a/pages/goods-detail/goods-detail.wxml b/pages/goods-detail/goods-detail.wxml index 7310d0f..3516c2d 100644 --- a/pages/goods-detail/goods-detail.wxml +++ b/pages/goods-detail/goods-detail.wxml @@ -103,7 +103,6 @@ style="display: flex; align-items: left; margin-right: 20rpx; padding: 8rpx 16rpx; border: 2rpx solid #4a90e2; border-radius: 20rpx; position: relative; left: 68rpx; top: 0rpx" > 对比价格 - >