From 3ccade40d372d757f017d87eeb13cb8294b05731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Fri, 23 Jan 2026 16:35:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E7=9B=B8=E5=90=8C?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E8=AE=A1=E7=AE=97=E5=B9=B3=E5=9D=87=E4=BB=B7?= =?UTF-8?q?=E6=A0=BC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/evaluate1/index.js | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/pages/evaluate1/index.js b/pages/evaluate1/index.js index b3bf6e9..3e1a908 100644 --- a/pages/evaluate1/index.js +++ b/pages/evaluate1/index.js @@ -150,8 +150,11 @@ Page({ // 只有当价格不为空时才添加该规格 if (matchedPrice) { - // 直接使用商品的原始价格,不做任何处理 - specPriceMap[spec] = matchedPrice; + // 收集相同规格的所有价格,以便计算平均值 + if (!specPriceMap[spec]) { + specPriceMap[spec] = []; + } + specPriceMap[spec].push(matchedPrice); } else { console.log(`规格'${spec}' 价格为空,不添加`); } @@ -160,10 +163,32 @@ Page({ } }); + // 计算每个规格的平均价格 + const specAvgPriceMap = {}; + Object.keys(specPriceMap).forEach(spec => { + const prices = specPriceMap[spec]; + // 过滤掉非数字价格 + const validPrices = prices.filter(price => !isNaN(parseFloat(price))); + + if (validPrices.length > 0) { + // 计算平均价格 + const sum = validPrices.reduce((acc, price) => acc + parseFloat(price), 0); + const avgPrice = sum / validPrices.length; + specAvgPriceMap[spec] = avgPrice.toFixed(2); + console.log(`规格'${spec}' 的平均价格: ${specAvgPriceMap[spec]} (基于 ${validPrices.length} 个价格)`); + } else { + // 如果没有有效价格,使用第一个价格(可能是非数字) + specAvgPriceMap[spec] = prices[0] || ''; + console.log(`规格'${spec}' 没有有效价格,使用: ${specAvgPriceMap[spec]}`); + } + }); + + console.log('规格平均价格映射:', specAvgPriceMap); + console.log('规格价格映射:', specPriceMap); - // 检查specPriceMap是否为空 - if (Object.keys(specPriceMap).length === 0) { + // 检查specAvgPriceMap是否为空 + if (Object.keys(specAvgPriceMap).length === 0) { console.error('未提取到有效规格'); this.setData({ error: '未提取到有效规格', @@ -173,9 +198,9 @@ Page({ } // 转换为规格对象数组 - const specifications = Object.keys(specPriceMap).map(spec => { - const price = specPriceMap[spec]; - console.log(`最终规格'${spec}' 对应价格: '${price}'`); + const specifications = Object.keys(specAvgPriceMap).map(spec => { + const price = specAvgPriceMap[spec]; + console.log(`最终规格'${spec}' 对应平均价格: '${price}'`); // 解析规格 const specInfo = this.parseSpecification(spec);