From 9559d6d1aaeee140f8f452c5c0aaefd1200a9a5c 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: Sat, 24 Jan 2026 14:55:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=B7=E6=A0=BC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E9=80=BB=E8=BE=91=E5=92=8C=E5=95=86=E5=93=81=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/evaluate1/index.js | 114 ++++++++++++++++---------------- pages/evaluate1/product-list.js | 29 +++++++- 2 files changed, 84 insertions(+), 59 deletions(-) diff --git a/pages/evaluate1/index.js b/pages/evaluate1/index.js index 4bd1b80..c83e6a5 100644 --- a/pages/evaluate1/index.js +++ b/pages/evaluate1/index.js @@ -42,7 +42,9 @@ Page({ } else { // 如果本地没有数据,再请求服务器 const api = require('../../utils/api'); - api.getProducts().then(products => { + api.getProducts(1, 1000).then(result => { + // 从返回对象中提取products数组 + const products = result.products || []; this.processSpecifications(productName, products); }).catch(err => { console.error('获取规格失败:', err); @@ -114,6 +116,12 @@ Page({ console.log(`处理第${productIndex + 1}个商品: 规格字符串='${specStr}', 价格字符串='${price}'`); + // 价格为空的不参与计算 + if (!price || price.trim() === '') { + console.log(`商品价格为空,跳过处理`); + return; + } + if (specStr.length > 0) { // 处理逗号分隔的多个规格,确保每个规格都被正确分割 // 首先按逗号分割 @@ -135,11 +143,17 @@ Page({ specs = processedSpecs; // 处理逗号分隔的多个价格 - const prices = (price || '').split(',').map(p => p.trim()); + const prices = (price || '').split(',').map(p => p.trim()).filter(p => p && p.trim() !== ''); console.log(`规格数组:`, specs); console.log(`价格数组:`, prices); + // 价格为空的不参与计算 + if (prices.length === 0) { + console.log(`价格数组为空,跳过处理`); + return; + } + // 将规格和价格配对 specs.forEach((spec, index) => { if (spec.length > 0) { @@ -149,7 +163,7 @@ Page({ console.log(`规格'${spec}' 配对价格: '${matchedPrice}'`); // 只有当价格不为空时才添加该规格 - if (matchedPrice) { + if (matchedPrice && matchedPrice.trim() !== '') { // 收集相同规格的所有价格,以便计算平均值 if (!specPriceMap[spec]) { specPriceMap[spec] = []; @@ -163,69 +177,57 @@ 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); - - // 检查specAvgPriceMap是否为空 - if (Object.keys(specAvgPriceMap).length === 0) { - console.error('未提取到商品名称为"' + productName + '"的有效规格'); - this.setData({ - error: '未提取到商品名称为"' + productName + '"的有效规格', - loading: false - }); - return; - } - // 转换为规格对象数组 - const specifications = Object.keys(specAvgPriceMap).map(spec => { - const price = specAvgPriceMap[spec]; - console.log(`最终规格'${spec}' 对应平均价格: '${price}'`); + const specifications = Object.keys(specPriceMap).map(spec => { + const prices = specPriceMap[spec]; + console.log(`规格'${spec}' 的所有原始价格:`, prices); // 解析规格 const specInfo = this.parseSpecification(spec); - let finalPrice = parseFloat(price) || 0; - let finalPriceText = price; - // 根据规格类型和价格水平计算最终价格 - if (specInfo && parseFloat(price) < 10) { - if (specInfo.type === '净重') { - // 净重:规格平均值 × 价格 - finalPrice = specInfo.avg * parseFloat(price); - finalPriceText = finalPrice.toFixed(2); - console.log(`规格'${spec}' 是净重,计算最终价格: ${finalPriceText}`); - } else if (specInfo.type === '毛重') { - // 毛重:(规格平均值 - 5) × 价格 - finalPrice = (specInfo.avg - 5) * parseFloat(price); - finalPriceText = finalPrice.toFixed(2); - console.log(`规格'${spec}' 是毛重,计算最终价格: ${finalPriceText}`); + // 处理每个价格 + const processedPrices = prices.map(price => { + if (!price || price.trim() === '' || isNaN(parseFloat(price))) { + return 0; } + + const priceValue = parseFloat(price); + console.log(`处理价格: ${priceValue}`); + + // 价格<10的需要按照公式计算 + if (priceValue < 10 && specInfo) { + console.log(`价格 ${priceValue} < 10,按照公式计算`); + if (specInfo.type === '净重') { + // 净重:规格平均值 × 价格 + return specInfo.avg * priceValue; + } else if (specInfo.type === '毛重') { + // 毛重:(规格平均值 - 5) × 价格 + return (specInfo.avg - 5) * priceValue; + } + } + // 价格>=10的直接使用 + return priceValue; + }).filter(price => price > 0); // 过滤掉0值 + + console.log(`规格'${spec}' 处理后的价格:`, processedPrices); + + // 计算处理后价格的平均值 + let finalPrice = 0; + let finalPriceText = ''; + + if (processedPrices.length > 0) { + const sum = processedPrices.reduce((acc, price) => acc + price, 0); + finalPrice = sum / processedPrices.length; + finalPriceText = finalPrice.toFixed(2); + console.log(`规格'${spec}' 处理后价格的平均值: ${finalPriceText} (基于 ${processedPrices.length} 个价格)`); + } else { + console.log(`规格'${spec}' 没有有效价格`); } const specObj = { name: spec, - price: price, - priceText: price, + price: finalPriceText, + priceText: finalPriceText, finalPrice: finalPrice, finalPriceText: finalPriceText }; diff --git a/pages/evaluate1/product-list.js b/pages/evaluate1/product-list.js index 335e439..9405738 100644 --- a/pages/evaluate1/product-list.js +++ b/pages/evaluate1/product-list.js @@ -12,9 +12,32 @@ Page({ this.setData({ loading: true, error: '' }); const api = require('../../utils/api'); - api.getProducts().then(products => { - // 提取唯一的productName - const uniqueProductNames = [...new Set(products.map(product => product.productName).filter(Boolean))]; + // 使用较大的pageSize来获取更多商品数据 + api.getProducts(1, 1000).then(result => { + console.log('API返回结果:', result); + // 从返回对象中提取products数组,如果不存在则使用空数组 + const products = result.products || []; + console.log('提取的products数组:', products); + console.log('products数组长度:', products.length); + + // 过滤出price不为空的商品 + const productsWithPrice = products.filter(product => product.price); + console.log('price不为空的商品:', productsWithPrice); + console.log('price不为空的商品数量:', productsWithPrice.length); + + // 提取这些商品的productName + const productNames = productsWithPrice.map(product => product.productName); + console.log('提取的productName数组:', productNames); + + // 过滤掉空的productName + const nonEmptyProductNames = productNames.filter(Boolean); + console.log('非空的productName数组:', nonEmptyProductNames); + + // 去重 + const uniqueProductNames = [...new Set(nonEmptyProductNames)]; + console.log('去重后的productName数组:', uniqueProductNames); + console.log('去重后的productName数量:', uniqueProductNames.length); + this.setData({ productNames: uniqueProductNames, loading: false