|
|
@ -130,7 +130,9 @@ Page({ |
|
|
currentGoods: null, |
|
|
currentGoods: null, |
|
|
currentSwiperIndex: 0, |
|
|
currentSwiperIndex: 0, |
|
|
currentPage: 1, |
|
|
currentPage: 1, |
|
|
hasMore: true |
|
|
pageSize: 10, // 每页展示10条数据
|
|
|
|
|
|
hasMore: true, |
|
|
|
|
|
allGoods: [] // 存储所有符合条件的商品
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onLoad: function (options) { |
|
|
onLoad: function (options) { |
|
|
@ -490,350 +492,283 @@ Page({ |
|
|
loadingMore: page > 1 ? true : false |
|
|
loadingMore: page > 1 ? true : false |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
API.getProducts(page, 20, 'all', '') |
|
|
// 如果是第一页,获取所有符合条件的商品
|
|
|
.then(res => { |
|
|
if (page === 1) { |
|
|
console.log('获取商品列表成功:', res); |
|
|
// 使用新的API接口获取相同规格的商品数据
|
|
|
console.log('选择的种类:', selectedCategory); |
|
|
API.getProductsBySpecification(specWeight) |
|
|
console.log('选择的规格:', specWeight); |
|
|
.then(comparisonProducts => { |
|
|
|
|
|
console.log('获取到的对比商品数据:', comparisonProducts); |
|
|
let filteredGoods = []; |
|
|
console.log('选择的种类:', selectedCategory); |
|
|
if (res && res.products) { |
|
|
console.log('选择的规格:', specWeight); |
|
|
console.log('原始商品数量:', res.products.length); |
|
|
|
|
|
// 获取当前商品的唯一标识符
|
|
|
let filteredGoods = []; |
|
|
const currentGoodsId = goodsData.productId || goodsData.id || goodsData._id; |
|
|
if (comparisonProducts && Array.isArray(comparisonProducts)) { |
|
|
console.log('当前商品ID:', currentGoodsId); |
|
|
console.log('原始商品数量:', comparisonProducts.length); |
|
|
|
|
|
// 获取当前商品的唯一标识符
|
|
|
// 过滤商品
|
|
|
const currentGoodsId = goodsData.productId || goodsData.id || goodsData._id; |
|
|
filteredGoods = res.products.filter(item => { |
|
|
console.log('当前商品ID:', currentGoodsId); |
|
|
// 1. 排除当前商品
|
|
|
|
|
|
const itemId = item.productId || item.id || item._id; |
|
|
// 过滤商品
|
|
|
if (currentGoodsId && itemId && currentGoodsId === itemId) { |
|
|
filteredGoods = comparisonProducts.filter(item => { |
|
|
console.log('排除当前商品:', itemId); |
|
|
// 1. 排除当前商品
|
|
|
return false; |
|
|
const itemId = item.productId || item.id || item._id; |
|
|
} |
|
|
if (currentGoodsId && itemId && currentGoodsId === itemId) { |
|
|
|
|
|
console.log('排除当前商品:', itemId); |
|
|
// 2. 检查商品状态和价格
|
|
|
return false; |
|
|
if ((item.status !== 'published' && item.status !== 'sold_out') || item.price === null || item.price === undefined) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 3. 过滤种类
|
|
|
|
|
|
if (selectedCategory && selectedCategory !== '全部' && item.category !== selectedCategory) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 4. 过滤规格
|
|
|
|
|
|
if (specWeight) { |
|
|
|
|
|
// 检查多个可能存储重量信息的字段
|
|
|
|
|
|
const fieldsToCheck = [ |
|
|
|
|
|
item.specification, |
|
|
|
|
|
item.grossWeight, |
|
|
|
|
|
item.weightQuantityData, |
|
|
|
|
|
item.spec // 检查spec字段
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
let hasMatchingSpec = false; |
|
|
|
|
|
for (const field of fieldsToCheck) { |
|
|
|
|
|
if (!field) continue; |
|
|
|
|
|
|
|
|
|
|
|
if (typeof field === 'string') { |
|
|
|
|
|
// 处理字符串格式的规格数据
|
|
|
|
|
|
console.log('检查字符串规格:', field, '是否包含', specWeight); |
|
|
|
|
|
// 处理逗号分隔的规格字符串
|
|
|
|
|
|
const specs = field.split(/[,,、]/).map(s => s.trim()); |
|
|
|
|
|
if (specs.some(spec => spec.includes(specWeight))) { |
|
|
|
|
|
hasMatchingSpec = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} else if (Array.isArray(field)) { |
|
|
|
|
|
// 处理数组格式的规格数据
|
|
|
|
|
|
console.log('检查数组规格:', field); |
|
|
|
|
|
if (field.some(spec => { |
|
|
|
|
|
if (typeof spec === 'string') { |
|
|
|
|
|
console.log('检查数组元素(字符串):', spec, '是否包含', specWeight); |
|
|
|
|
|
return spec.includes(specWeight); |
|
|
|
|
|
} else if (typeof spec === 'object') { |
|
|
|
|
|
// 检查对象格式的规格数据
|
|
|
|
|
|
const specStr = spec.weightSpec || spec.display || spec.spec || ''; |
|
|
|
|
|
console.log('检查数组元素(对象):', specStr, '是否包含', specWeight); |
|
|
|
|
|
return specStr.includes(specWeight); |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
})) { |
|
|
|
|
|
hasMatchingSpec = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} else if (typeof field === 'object') { |
|
|
|
|
|
// 处理对象格式的规格数据
|
|
|
|
|
|
console.log('检查对象规格:', field); |
|
|
|
|
|
const specStr = field.weightSpec || field.display || field.spec || ''; |
|
|
|
|
|
console.log('检查对象规格值:', specStr, '是否包含', specWeight); |
|
|
|
|
|
if (specStr.includes(specWeight)) { |
|
|
|
|
|
hasMatchingSpec = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果没有找到匹配的规格,尝试进行更宽松的匹配
|
|
|
// 2. 检查商品状态和价格
|
|
|
if (!hasMatchingSpec) { |
|
|
if ((item.status !== 'published' && item.status !== 'sold_out') || item.price === null || item.price === undefined) { |
|
|
console.log('尝试更宽松的匹配...'); |
|
|
return false; |
|
|
// 提取规格中的数字部分进行匹配
|
|
|
|
|
|
const weightNum = specWeight.replace(/[^0-9-]/g, ''); |
|
|
|
|
|
console.log('提取的重量数字:', weightNum); |
|
|
|
|
|
|
|
|
|
|
|
for (const field of fieldsToCheck) { |
|
|
|
|
|
if (!field) continue; |
|
|
|
|
|
|
|
|
|
|
|
if (typeof field === 'string') { |
|
|
|
|
|
console.log('检查字符串字段:', field, '是否包含', weightNum); |
|
|
|
|
|
if (field.includes(weightNum)) { |
|
|
|
|
|
hasMatchingSpec = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} else if (Array.isArray(field)) { |
|
|
|
|
|
console.log('检查数组字段:', field); |
|
|
|
|
|
if (field.some(spec => { |
|
|
|
|
|
const specStr = typeof spec === 'string' ? spec : (spec.weightSpec || spec.display || spec.spec || ''); |
|
|
|
|
|
console.log('检查数组元素:', specStr, '是否包含', weightNum); |
|
|
|
|
|
return specStr.includes(weightNum); |
|
|
|
|
|
})) { |
|
|
|
|
|
hasMatchingSpec = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} else if (typeof field === 'object') { |
|
|
|
|
|
console.log('检查对象字段:', field); |
|
|
|
|
|
const specStr = field.weightSpec || field.display || field.spec || ''; |
|
|
|
|
|
console.log('检查对象字段值:', specStr, '是否包含', weightNum); |
|
|
|
|
|
if (specStr.includes(weightNum)) { |
|
|
|
|
|
hasMatchingSpec = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!hasMatchingSpec) { |
|
|
// 3. 过滤种类
|
|
|
|
|
|
if (selectedCategory && selectedCategory !== '全部' && item.category !== selectedCategory) { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
return true; |
|
|
}); |
|
|
|
|
|
console.log('过滤后的商品数量:', filteredGoods.length); |
|
|
|
|
|
console.log('过滤后的商品:', filteredGoods); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理商品数据
|
|
|
|
|
|
const processedGoods = filteredGoods.map(item => { |
|
|
|
|
|
// 首先清理 imageUrls 字段(如果存在)
|
|
|
|
|
|
if (item.imageUrls && Array.isArray(item.imageUrls)) { |
|
|
|
|
|
item.imageUrls = item.imageUrls.map(url => { |
|
|
|
|
|
return url.trim().replace(/[`]/g, ''); |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
console.log('过滤后的商品数量:', filteredGoods.length); |
|
|
|
|
|
console.log('过滤后的商品:', filteredGoods); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 使用processMediaUrls函数处理媒体数据
|
|
|
// 处理商品数据
|
|
|
item.mediaItems = processMediaUrls(item.imageUrls); |
|
|
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函数处理媒体数据
|
|
|
if (item.mediaItems && item.mediaItems.length > 1) { |
|
|
item.mediaItems = processMediaUrls(item.imageUrls); |
|
|
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)) { |
|
|
if (item.mediaItems && item.mediaItems.length > 1) { |
|
|
item.mediaItems = item.mediaItems.map(media => { |
|
|
const imageItems = item.mediaItems.filter(media => media.type === 'image'); |
|
|
if (media.url) { |
|
|
const videoItems = item.mediaItems.filter(media => media.type === 'video'); |
|
|
// 去除 URL 中的反引号和空格
|
|
|
item.mediaItems = [...imageItems, ...videoItems]; |
|
|
media.url = media.url.trim().replace(/[`]/g, ''); |
|
|
|
|
|
// 确保媒体类型正确
|
|
|
|
|
|
if (!media.type) { |
|
|
|
|
|
media.type = isVideoUrl(media.url) ? 'video' : 'image'; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
return media; |
|
|
} |
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理商品价格和库存,使用选中规格的价格和库存
|
|
|
// 清理 mediaItems 中的 URL
|
|
|
if (specWeight) { |
|
|
if (item.mediaItems && Array.isArray(item.mediaItems)) { |
|
|
if (item.weightQuantityData && Array.isArray(item.weightQuantityData)) { |
|
|
item.mediaItems = item.mediaItems.map(media => { |
|
|
// 尝试从weightQuantityData中找到对应规格的价格和库存
|
|
|
if (media.url) { |
|
|
const matchingSpec = item.weightQuantityData.find(spec => { |
|
|
// 去除 URL 中的反引号和空格
|
|
|
if (spec.weightSpec) { |
|
|
media.url = media.url.trim().replace(/[`]/g, ''); |
|
|
return spec.weightSpec.trim() === specWeight; |
|
|
// 确保媒体类型正确
|
|
|
|
|
|
if (!media.type) { |
|
|
|
|
|
media.type = isVideoUrl(media.url) ? 'video' : 'image'; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return media; |
|
|
}); |
|
|
}); |
|
|
if (matchingSpec) { |
|
|
} |
|
|
// 确保价格是一个单一的值,而不是多个价格的组合
|
|
|
|
|
|
if (matchingSpec.price) { |
|
|
// 处理商品价格和库存,使用选中规格的价格和库存
|
|
|
item.price = matchingSpec.price; |
|
|
if (specWeight) { |
|
|
} |
|
|
if (item.weightQuantityData && Array.isArray(item.weightQuantityData)) { |
|
|
// 确保库存是对应规格的库存
|
|
|
// 尝试从weightQuantityData中找到对应规格的价格和库存
|
|
|
if (matchingSpec.quantity) { |
|
|
const matchingSpec = item.weightQuantityData.find(spec => { |
|
|
item.specStock = matchingSpec.quantity; |
|
|
if (spec.weightSpec) { |
|
|
|
|
|
return spec.weightSpec.trim() === specWeight; |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
}); |
|
|
|
|
|
if (matchingSpec) { |
|
|
|
|
|
// 确保价格是一个单一的值,而不是多个价格的组合
|
|
|
|
|
|
if (matchingSpec.price) { |
|
|
|
|
|
item.price = matchingSpec.price; |
|
|
|
|
|
} |
|
|
|
|
|
// 确保库存是对应规格的库存
|
|
|
|
|
|
if (matchingSpec.quantity) { |
|
|
|
|
|
item.specStock = matchingSpec.quantity; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 清理价格字段,确保只显示单一价格
|
|
|
// 清理价格字段,确保只显示单一价格
|
|
|
if (item.price) { |
|
|
if (item.price) { |
|
|
// 如果价格是字符串且包含逗号,只取第一个价格
|
|
|
// 如果价格是字符串且包含逗号,只取第一个价格
|
|
|
if (typeof item.price === 'string' && item.price.includes(',')) { |
|
|
if (typeof item.price === 'string' && item.price.includes(',')) { |
|
|
item.price = item.price.split(',')[0].trim(); |
|
|
item.price = item.price.split(',')[0].trim(); |
|
|
} |
|
|
} |
|
|
// 如果价格是数组,只取第一个价格
|
|
|
// 如果价格是数组,只取第一个价格
|
|
|
if (Array.isArray(item.price)) { |
|
|
if (Array.isArray(item.price)) { |
|
|
item.price = item.price[0]; |
|
|
item.price = item.price[0]; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理规格信息,将多个净重信息分割成数组
|
|
|
// 处理规格信息,将多个净重信息分割成数组
|
|
|
item.processedSpecs = []; |
|
|
item.processedSpecs = []; |
|
|
if (item.specification) { |
|
|
if (item.specification) { |
|
|
item.processedSpecs = processSpecifications(item.specification); |
|
|
item.processedSpecs = processSpecifications(item.specification); |
|
|
} else if (item.weightSpec) { |
|
|
} else if (item.weightSpec) { |
|
|
item.processedSpecs = processSpecifications(item.weightSpec); |
|
|
item.processedSpecs = processSpecifications(item.weightSpec); |
|
|
} else if (item.grossWeight) { |
|
|
} else if (item.grossWeight) { |
|
|
item.processedSpecs = processSpecifications(item.grossWeight); |
|
|
item.processedSpecs = processSpecifications(item.grossWeight); |
|
|
} else if (selectedSpec) { |
|
|
} else if (selectedSpec) { |
|
|
item.processedSpecs = processSpecifications(selectedSpec); |
|
|
item.processedSpecs = processSpecifications(selectedSpec); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理库存显示逻辑
|
|
|
|
|
|
// 优先使用对应规格的库存,如果没有则使用默认库存
|
|
|
|
|
|
let quantity = item.specStock || item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; |
|
|
|
|
|
|
|
|
|
|
|
// 清理库存字段,确保只显示单一值
|
|
|
|
|
|
if (quantity) { |
|
|
|
|
|
// 如果库存是字符串且包含逗号,只取第一个库存
|
|
|
|
|
|
if (typeof quantity === 'string' && quantity.includes(',')) { |
|
|
|
|
|
quantity = quantity.split(',')[0].trim(); |
|
|
|
|
|
} |
|
|
|
|
|
// 如果库存是数组,只取第一个库存
|
|
|
|
|
|
if (Array.isArray(quantity)) { |
|
|
|
|
|
quantity = quantity[0]; |
|
|
|
|
|
} |
|
|
} |
|
|
// 转换为数字
|
|
|
|
|
|
quantity = Number(quantity); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const totalStock = quantity; |
|
|
// 处理库存显示逻辑
|
|
|
|
|
|
// 优先使用对应规格的库存,如果没有则使用默认库存
|
|
|
let displayStock; |
|
|
let quantity = item.specStock || item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; |
|
|
if (totalStock >= 10000) { |
|
|
|
|
|
// 库存>=10000时显示"库存充足"
|
|
|
|
|
|
displayStock = '充足'; |
|
|
|
|
|
} else if (totalStock === 0) { |
|
|
|
|
|
// 库存=0时显示"暂无"
|
|
|
|
|
|
displayStock = '暂无'; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 其他情况显示具体数字
|
|
|
|
|
|
displayStock = totalStock; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 更新商品的库存显示
|
|
|
// 清理库存字段,确保只显示单一值
|
|
|
item.totalStock = displayStock; |
|
|
if (quantity) { |
|
|
item.originalTotalStock = totalStock; |
|
|
// 如果库存是字符串且包含逗号,只取第一个库存
|
|
|
|
|
|
if (typeof quantity === 'string' && quantity.includes(',')) { |
|
|
|
|
|
quantity = quantity.split(',')[0].trim(); |
|
|
|
|
|
} |
|
|
|
|
|
// 如果库存是数组,只取第一个库存
|
|
|
|
|
|
if (Array.isArray(quantity)) { |
|
|
|
|
|
quantity = quantity[0]; |
|
|
|
|
|
} |
|
|
|
|
|
// 转换为数字
|
|
|
|
|
|
quantity = Number(quantity); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 添加当前对比的规格信息
|
|
|
const totalStock = quantity; |
|
|
if (specWeight) { |
|
|
|
|
|
item.currentSpec = specWeight; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理地区信息,只显示省份
|
|
|
let displayStock; |
|
|
if (item.region) { |
|
|
if (totalStock >= 10000) { |
|
|
// 提取省份信息
|
|
|
// 库存>=10000时显示"库存充足"
|
|
|
const provinceRegex = /^([^省]+省|[^自治区]+自治区|[^直辖市]+直辖市|[^特别行政区]+特别行政区)/; |
|
|
displayStock = '充足'; |
|
|
const match = item.region.match(provinceRegex); |
|
|
} else if (totalStock === 0) { |
|
|
if (match) { |
|
|
// 库存=0时显示"暂无"
|
|
|
item.region = match[1]; |
|
|
displayStock = '暂无'; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 其他情况显示具体数字
|
|
|
|
|
|
displayStock = totalStock; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 计算单位价格
|
|
|
// 更新商品的库存显示
|
|
|
item.unitPrice = calculateUnitPrice(item.price, item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : '')); |
|
|
item.totalStock = displayStock; |
|
|
|
|
|
item.originalTotalStock = totalStock; |
|
|
|
|
|
|
|
|
// 计算价格涨幅
|
|
|
// 添加当前对比的规格信息
|
|
|
if (goodsData.price) { |
|
|
if (specWeight) { |
|
|
// 获取当前商品的价格(使用单位价格如果低于10元)
|
|
|
item.currentSpec = specWeight; |
|
|
let currentPrice = parseFloat(goodsData.price); |
|
|
|
|
|
if (goodsData.unitPrice) { |
|
|
|
|
|
currentPrice = parseFloat(goodsData.unitPrice); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 获取对比商品的价格(根据规则使用合适的价格)
|
|
|
// 处理地区信息,只显示省份
|
|
|
let itemPrice = parseFloat(item.price); |
|
|
if (item.region) { |
|
|
if (currentPrice >= 10 && item.unitPrice) { |
|
|
// 提取省份信息
|
|
|
// 如果当前商品价格高于10元,对比商品使用单位价格
|
|
|
const provinceRegex = /^([^省]+省|[^自治区]+自治区|[^直辖市]+直辖市|[^特别行政区]+特别行政区)/; |
|
|
itemPrice = parseFloat(item.unitPrice); |
|
|
const match = item.region.match(provinceRegex); |
|
|
} else if (currentPrice < 10 && itemPrice < 10) { |
|
|
if (match) { |
|
|
// 如果两者都低于10元,使用原始价格
|
|
|
item.region = match[1]; |
|
|
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; |
|
|
item.unitPrice = calculateUnitPrice(item.price, item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : '')); |
|
|
const pricePercent = ((priceDiff / currentPrice) * 100).toFixed(1); |
|
|
|
|
|
item.priceDiff = priceDiff; |
|
|
|
|
|
item.pricePercent = pricePercent; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return item; |
|
|
// 计算价格涨幅
|
|
|
}); |
|
|
if (goodsData.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); |
|
|
|
|
|
item.priceDiff = priceDiff; |
|
|
|
|
|
item.pricePercent = pricePercent; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 合并商品数据
|
|
|
return item; |
|
|
const newGoods = page === 1 ? processedGoods : [...this.data.goods, ...processedGoods]; |
|
|
}); |
|
|
|
|
|
|
|
|
// 检查是否还有更多数据
|
|
|
// 存储所有符合条件的商品
|
|
|
const hasMore = res && res.products && res.products.length >= 20; |
|
|
this.setData({ |
|
|
|
|
|
allGoods: processedGoods |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
// 设置商品数据
|
|
|
// 计算当前页应该显示的商品
|
|
|
this.setData({ |
|
|
const { pageSize } = this.data; |
|
|
goods: newGoods, |
|
|
const startIndex = (page - 1) * pageSize; |
|
|
loading: false, |
|
|
const endIndex = startIndex + pageSize; |
|
|
loadingMore: false, |
|
|
const currentPageGoods = processedGoods.slice(startIndex, endIndex); |
|
|
currentPage: page, |
|
|
|
|
|
hasMore: hasMore, |
|
|
// 检查是否还有更多数据
|
|
|
selectedOption: selectedCategory || '对比价格', |
|
|
const hasMore = endIndex < processedGoods.length; |
|
|
showTips: false |
|
|
|
|
|
}); |
|
|
// 设置商品数据
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
goods: currentPageGoods, |
|
|
|
|
|
loading: false, |
|
|
|
|
|
loadingMore: false, |
|
|
|
|
|
currentPage: page, |
|
|
|
|
|
hasMore: hasMore, |
|
|
|
|
|
selectedOption: selectedCategory || '对比价格', |
|
|
|
|
|
showTips: false |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
console.log('对比价格数据加载完成:', newGoods); |
|
|
console.log('对比价格数据加载完成:', currentPageGoods); |
|
|
|
|
|
|
|
|
// 如果符合条件的数据少于5条,自动加载更多数据
|
|
|
|
|
|
if (newGoods.length < 5 && hasMore) { |
|
|
|
|
|
console.log('符合条件的数据少于5条,自动加载更多数据'); |
|
|
|
|
|
this.loadGoodsData(page + 1, selectedCategory, specWeight, goodsData); |
|
|
|
|
|
} else if (page === 1) { |
|
|
|
|
|
// 显示提示信息
|
|
|
// 显示提示信息
|
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
title: `找到${newGoods.length}个符合条件的商品`, |
|
|
title: `找到${processedGoods.length}个符合条件的商品`, |
|
|
icon: 'success', |
|
|
icon: 'success', |
|
|
duration: 2000 |
|
|
duration: 2000 |
|
|
}); |
|
|
}); |
|
|
} |
|
|
}) |
|
|
}) |
|
|
.catch(err => { |
|
|
.catch(err => { |
|
|
console.error('获取商品列表失败:', err); |
|
|
console.error('获取商品列表失败:', err); |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
loading: false, |
|
|
loading: false, |
|
|
loadingMore: false |
|
|
loadingMore: false |
|
|
}); |
|
|
}); |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
title: '获取商品失败,请稍后重试', |
|
|
title: '获取商品失败,请稍后重试', |
|
|
icon: 'none' |
|
|
icon: 'none' |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果不是第一页,从allGoods中获取数据
|
|
|
|
|
|
const { allGoods, pageSize } = this.data; |
|
|
|
|
|
const startIndex = (page - 1) * pageSize; |
|
|
|
|
|
const endIndex = startIndex + pageSize; |
|
|
|
|
|
const currentPageGoods = allGoods.slice(startIndex, endIndex); |
|
|
|
|
|
|
|
|
|
|
|
// 检查是否还有更多数据
|
|
|
|
|
|
const hasMore = endIndex < allGoods.length; |
|
|
|
|
|
|
|
|
|
|
|
// 合并商品数据
|
|
|
|
|
|
const newGoods = [...this.data.goods, ...currentPageGoods]; |
|
|
|
|
|
|
|
|
|
|
|
// 设置商品数据
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
goods: newGoods, |
|
|
|
|
|
loading: false, |
|
|
|
|
|
loadingMore: false, |
|
|
|
|
|
currentPage: page, |
|
|
|
|
|
hasMore: hasMore |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
console.log('加载更多数据完成:', currentPageGoods); |
|
|
|
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 加载更多数据
|
|
|
// 加载更多数据
|
|
|
|