|
|
|
@ -130,7 +130,9 @@ Page({ |
|
|
|
currentGoods: null, |
|
|
|
currentSwiperIndex: 0, |
|
|
|
currentPage: 1, |
|
|
|
hasMore: true |
|
|
|
pageSize: 10, // 每页展示10条数据
|
|
|
|
hasMore: true, |
|
|
|
allGoods: [] // 存储所有符合条件的商品
|
|
|
|
}, |
|
|
|
|
|
|
|
onLoad: function (options) { |
|
|
|
@ -490,21 +492,24 @@ Page({ |
|
|
|
loadingMore: page > 1 ? true : false |
|
|
|
}); |
|
|
|
|
|
|
|
API.getProducts(page, 20, 'all', '') |
|
|
|
.then(res => { |
|
|
|
console.log('获取商品列表成功:', res); |
|
|
|
// 如果是第一页,获取所有符合条件的商品
|
|
|
|
if (page === 1) { |
|
|
|
// 使用新的API接口获取相同规格的商品数据
|
|
|
|
API.getProductsBySpecification(specWeight) |
|
|
|
.then(comparisonProducts => { |
|
|
|
console.log('获取到的对比商品数据:', comparisonProducts); |
|
|
|
console.log('选择的种类:', selectedCategory); |
|
|
|
console.log('选择的规格:', specWeight); |
|
|
|
|
|
|
|
let filteredGoods = []; |
|
|
|
if (res && res.products) { |
|
|
|
console.log('原始商品数量:', res.products.length); |
|
|
|
if (comparisonProducts && Array.isArray(comparisonProducts)) { |
|
|
|
console.log('原始商品数量:', comparisonProducts.length); |
|
|
|
// 获取当前商品的唯一标识符
|
|
|
|
const currentGoodsId = goodsData.productId || goodsData.id || goodsData._id; |
|
|
|
console.log('当前商品ID:', currentGoodsId); |
|
|
|
|
|
|
|
// 过滤商品
|
|
|
|
filteredGoods = res.products.filter(item => { |
|
|
|
filteredGoods = comparisonProducts.filter(item => { |
|
|
|
// 1. 排除当前商品
|
|
|
|
const itemId = item.productId || item.id || item._id; |
|
|
|
if (currentGoodsId && itemId && currentGoodsId === itemId) { |
|
|
|
@ -522,102 +527,6 @@ Page({ |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 如果没有找到匹配的规格,尝试进行更宽松的匹配
|
|
|
|
if (!hasMatchingSpec) { |
|
|
|
console.log('尝试更宽松的匹配...'); |
|
|
|
// 提取规格中的数字部分进行匹配
|
|
|
|
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) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
}); |
|
|
|
console.log('过滤后的商品数量:', filteredGoods.length); |
|
|
|
@ -791,15 +700,23 @@ Page({ |
|
|
|
return item; |
|
|
|
}); |
|
|
|
|
|
|
|
// 合并商品数据
|
|
|
|
const newGoods = page === 1 ? processedGoods : [...this.data.goods, ...processedGoods]; |
|
|
|
// 存储所有符合条件的商品
|
|
|
|
this.setData({ |
|
|
|
allGoods: processedGoods |
|
|
|
}); |
|
|
|
|
|
|
|
// 计算当前页应该显示的商品
|
|
|
|
const { pageSize } = this.data; |
|
|
|
const startIndex = (page - 1) * pageSize; |
|
|
|
const endIndex = startIndex + pageSize; |
|
|
|
const currentPageGoods = processedGoods.slice(startIndex, endIndex); |
|
|
|
|
|
|
|
// 检查是否还有更多数据
|
|
|
|
const hasMore = res && res.products && res.products.length >= 20; |
|
|
|
const hasMore = endIndex < processedGoods.length; |
|
|
|
|
|
|
|
// 设置商品数据
|
|
|
|
this.setData({ |
|
|
|
goods: newGoods, |
|
|
|
goods: currentPageGoods, |
|
|
|
loading: false, |
|
|
|
loadingMore: false, |
|
|
|
currentPage: page, |
|
|
|
@ -808,20 +725,14 @@ Page({ |
|
|
|
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({ |
|
|
|
title: `找到${newGoods.length}个符合条件的商品`, |
|
|
|
title: `找到${processedGoods.length}个符合条件的商品`, |
|
|
|
icon: 'success', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('获取商品列表失败:', err); |
|
|
|
@ -834,6 +745,30 @@ Page({ |
|
|
|
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); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
// 加载更多数据
|
|
|
|
|