|
|
@ -42,7 +42,9 @@ Page({ |
|
|
} else { |
|
|
} else { |
|
|
// 如果本地没有数据,再请求服务器
|
|
|
// 如果本地没有数据,再请求服务器
|
|
|
const api = require('../../utils/api'); |
|
|
const api = require('../../utils/api'); |
|
|
api.getProducts().then(products => { |
|
|
api.getProducts(1, 1000).then(result => { |
|
|
|
|
|
// 从返回对象中提取products数组
|
|
|
|
|
|
const products = result.products || []; |
|
|
this.processSpecifications(productName, products); |
|
|
this.processSpecifications(productName, products); |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
console.error('获取规格失败:', err); |
|
|
console.error('获取规格失败:', err); |
|
|
@ -114,6 +116,12 @@ Page({ |
|
|
|
|
|
|
|
|
console.log(`处理第${productIndex + 1}个商品: 规格字符串='${specStr}', 价格字符串='${price}'`); |
|
|
console.log(`处理第${productIndex + 1}个商品: 规格字符串='${specStr}', 价格字符串='${price}'`); |
|
|
|
|
|
|
|
|
|
|
|
// 价格为空的不参与计算
|
|
|
|
|
|
if (!price || price.trim() === '') { |
|
|
|
|
|
console.log(`商品价格为空,跳过处理`); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (specStr.length > 0) { |
|
|
if (specStr.length > 0) { |
|
|
// 处理逗号分隔的多个规格,确保每个规格都被正确分割
|
|
|
// 处理逗号分隔的多个规格,确保每个规格都被正确分割
|
|
|
// 首先按逗号分割
|
|
|
// 首先按逗号分割
|
|
|
@ -135,11 +143,17 @@ Page({ |
|
|
specs = processedSpecs; |
|
|
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(`规格数组:`, specs); |
|
|
console.log(`价格数组:`, prices); |
|
|
console.log(`价格数组:`, prices); |
|
|
|
|
|
|
|
|
|
|
|
// 价格为空的不参与计算
|
|
|
|
|
|
if (prices.length === 0) { |
|
|
|
|
|
console.log(`价格数组为空,跳过处理`); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 将规格和价格配对
|
|
|
// 将规格和价格配对
|
|
|
specs.forEach((spec, index) => { |
|
|
specs.forEach((spec, index) => { |
|
|
if (spec.length > 0) { |
|
|
if (spec.length > 0) { |
|
|
@ -149,7 +163,7 @@ Page({ |
|
|
console.log(`规格'${spec}' 配对价格: '${matchedPrice}'`); |
|
|
console.log(`规格'${spec}' 配对价格: '${matchedPrice}'`); |
|
|
|
|
|
|
|
|
// 只有当价格不为空时才添加该规格
|
|
|
// 只有当价格不为空时才添加该规格
|
|
|
if (matchedPrice) { |
|
|
if (matchedPrice && matchedPrice.trim() !== '') { |
|
|
// 收集相同规格的所有价格,以便计算平均值
|
|
|
// 收集相同规格的所有价格,以便计算平均值
|
|
|
if (!specPriceMap[spec]) { |
|
|
if (!specPriceMap[spec]) { |
|
|
specPriceMap[spec] = []; |
|
|
specPriceMap[spec] = []; |
|
|
@ -163,69 +177,57 @@ Page({ |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 计算每个规格的平均价格
|
|
|
// 转换为规格对象数组
|
|
|
const specAvgPriceMap = {}; |
|
|
const specifications = Object.keys(specPriceMap).map(spec => { |
|
|
Object.keys(specPriceMap).forEach(spec => { |
|
|
|
|
|
const prices = specPriceMap[spec]; |
|
|
const prices = specPriceMap[spec]; |
|
|
// 过滤掉非数字价格
|
|
|
console.log(`规格'${spec}' 的所有原始价格:`, prices); |
|
|
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); |
|
|
// 解析规格
|
|
|
|
|
|
const specInfo = this.parseSpecification(spec); |
|
|
console.log('规格价格映射:', specPriceMap); |
|
|
|
|
|
|
|
|
|
|
|
// 检查specAvgPriceMap是否为空
|
|
|
// 处理每个价格
|
|
|
if (Object.keys(specAvgPriceMap).length === 0) { |
|
|
const processedPrices = prices.map(price => { |
|
|
console.error('未提取到商品名称为"' + productName + '"的有效规格'); |
|
|
if (!price || price.trim() === '' || isNaN(parseFloat(price))) { |
|
|
this.setData({ |
|
|
return 0; |
|
|
error: '未提取到商品名称为"' + productName + '"的有效规格', |
|
|
|
|
|
loading: false |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 转换为规格对象数组
|
|
|
const priceValue = parseFloat(price); |
|
|
const specifications = Object.keys(specAvgPriceMap).map(spec => { |
|
|
console.log(`处理价格: ${priceValue}`); |
|
|
const price = specAvgPriceMap[spec]; |
|
|
|
|
|
console.log(`最终规格'${spec}' 对应平均价格: '${price}'`); |
|
|
|
|
|
|
|
|
|
|
|
// 解析规格
|
|
|
// 价格<10的需要按照公式计算
|
|
|
const specInfo = this.parseSpecification(spec); |
|
|
if (priceValue < 10 && specInfo) { |
|
|
let finalPrice = parseFloat(price) || 0; |
|
|
console.log(`价格 ${priceValue} < 10,按照公式计算`); |
|
|
let finalPriceText = price; |
|
|
|
|
|
|
|
|
|
|
|
// 根据规格类型和价格水平计算最终价格
|
|
|
|
|
|
if (specInfo && parseFloat(price) < 10) { |
|
|
|
|
|
if (specInfo.type === '净重') { |
|
|
if (specInfo.type === '净重') { |
|
|
// 净重:规格平均值 × 价格
|
|
|
// 净重:规格平均值 × 价格
|
|
|
finalPrice = specInfo.avg * parseFloat(price); |
|
|
return specInfo.avg * priceValue; |
|
|
finalPriceText = finalPrice.toFixed(2); |
|
|
|
|
|
console.log(`规格'${spec}' 是净重,计算最终价格: ${finalPriceText}`); |
|
|
|
|
|
} else if (specInfo.type === '毛重') { |
|
|
} else if (specInfo.type === '毛重') { |
|
|
// 毛重:(规格平均值 - 5) × 价格
|
|
|
// 毛重:(规格平均值 - 5) × 价格
|
|
|
finalPrice = (specInfo.avg - 5) * parseFloat(price); |
|
|
return (specInfo.avg - 5) * priceValue; |
|
|
finalPriceText = finalPrice.toFixed(2); |
|
|
} |
|
|
console.log(`规格'${spec}' 是毛重,计算最终价格: ${finalPriceText}`); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
// 价格>=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 = { |
|
|
const specObj = { |
|
|
name: spec, |
|
|
name: spec, |
|
|
price: price, |
|
|
price: finalPriceText, |
|
|
priceText: price, |
|
|
priceText: finalPriceText, |
|
|
finalPrice: finalPrice, |
|
|
finalPrice: finalPrice, |
|
|
finalPriceText: finalPriceText |
|
|
finalPriceText: finalPriceText |
|
|
}; |
|
|
}; |
|
|
|