|
|
@ -447,8 +447,13 @@ Page({ |
|
|
const imageUrls = product.imageUrls || product.images || []; |
|
|
const imageUrls = product.imageUrls || product.images || []; |
|
|
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
|
|
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
|
|
|
|
|
|
|
|
|
|
|
// 确保商品ID的一致性
|
|
|
|
|
|
const productId = product.productId || product.id; |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
...product, |
|
|
...product, |
|
|
|
|
|
id: productId, // 统一使用id字段
|
|
|
|
|
|
productId: productId, // 同时保留productId字段
|
|
|
category: product.category || '', |
|
|
category: product.category || '', |
|
|
fullRegion: product.region || '', |
|
|
fullRegion: product.region || '', |
|
|
region: product.region ? this.extractProvince(product.region) : '', |
|
|
region: product.region ? this.extractProvince(product.region) : '', |
|
|
@ -471,12 +476,23 @@ Page({ |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// 过滤隐藏状态的商品
|
|
|
newGoods = newGoods.filter(item => (item.status || '').toLowerCase() !== 'hidden') |
|
|
newGoods = newGoods.filter(item => (item.status || '').toLowerCase() !== 'hidden') |
|
|
|
|
|
|
|
|
|
|
|
// 先对新商品进行内部查重
|
|
|
|
|
|
const uniqueNewGoodsMap = new Map(); |
|
|
|
|
|
newGoods.forEach(item => { |
|
|
|
|
|
if (!uniqueNewGoodsMap.has(item.id)) { |
|
|
|
|
|
uniqueNewGoodsMap.set(item.id, item); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
newGoods = Array.from(uniqueNewGoodsMap.values()); |
|
|
|
|
|
|
|
|
// 更新商品的收藏状态
|
|
|
// 更新商品的收藏状态
|
|
|
this.updateGoodsFavoriteStatus(newGoods, isLoadMore) |
|
|
this.updateGoodsFavoriteStatus(newGoods, isLoadMore) |
|
|
|
|
|
|
|
|
// 只在第一页或刷新时在商品列表最前面插入广告
|
|
|
// 只在第一页或刷新时在商品列表最前面插入广告,且只插入一次
|
|
|
|
|
|
let adSlotsAdded = false; |
|
|
if ((!isLoadMore || this.data.page === 1) && newGoods.length > 0) { |
|
|
if ((!isLoadMore || this.data.page === 1) && newGoods.length > 0) { |
|
|
// 确保广告位在最前面
|
|
|
// 确保广告位在最前面
|
|
|
const adSlots = [ |
|
|
const adSlots = [ |
|
|
@ -499,14 +515,17 @@ Page({ |
|
|
]; |
|
|
]; |
|
|
|
|
|
|
|
|
newGoods = [...adSlots, ...newGoods]; |
|
|
newGoods = [...adSlots, ...newGoods]; |
|
|
|
|
|
adSlotsAdded = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let updatedGoods = [] |
|
|
let updatedGoods = [] |
|
|
if (isLoadMore) { |
|
|
if (isLoadMore) { |
|
|
|
|
|
// 加载更多时,去重处理
|
|
|
const existingIds = new Set(this.data.goods.map(item => item.id)); |
|
|
const existingIds = new Set(this.data.goods.map(item => item.id)); |
|
|
const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); |
|
|
const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); |
|
|
updatedGoods = [...this.data.goods, ...uniqueNewGoods]; |
|
|
updatedGoods = [...this.data.goods, ...uniqueNewGoods]; |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
// 首次加载或切换分类时,直接使用去重后的新商品
|
|
|
updatedGoods = newGoods |
|
|
updatedGoods = newGoods |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -537,8 +556,13 @@ Page({ |
|
|
const imageUrls = product.imageUrls || product.images || []; |
|
|
const imageUrls = product.imageUrls || product.images || []; |
|
|
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
|
|
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
|
|
|
|
|
|
|
|
|
|
|
// 确保商品ID的一致性
|
|
|
|
|
|
const productId = product.productId || product.id; |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
...product, |
|
|
...product, |
|
|
|
|
|
id: productId, // 统一使用id字段
|
|
|
|
|
|
productId: productId, // 同时保留productId字段
|
|
|
category: product.category || '', |
|
|
category: product.category || '', |
|
|
fullRegion: product.region || '', |
|
|
fullRegion: product.region || '', |
|
|
region: product.region ? this.extractProvince(product.region) : '', |
|
|
region: product.region ? this.extractProvince(product.region) : '', |
|
|
@ -563,9 +587,20 @@ Page({ |
|
|
|
|
|
|
|
|
newGoods = newGoods.filter(item => (item.status || '').toLowerCase() !== 'hidden') |
|
|
newGoods = newGoods.filter(item => (item.status || '').toLowerCase() !== 'hidden') |
|
|
|
|
|
|
|
|
const existingIds = new Set(existingGoods.filter(item => !item.isAd).map(item => item.id)); |
|
|
// 对新商品进行内部查重
|
|
|
|
|
|
const newGoodsMap = new Map(); |
|
|
|
|
|
newGoods.forEach(item => { |
|
|
|
|
|
if (!newGoodsMap.has(item.id)) { |
|
|
|
|
|
newGoodsMap.set(item.id, item); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
newGoods = Array.from(newGoodsMap.values()); |
|
|
|
|
|
|
|
|
|
|
|
// 移除与现有商品重复的新商品(包括广告和普通商品)
|
|
|
|
|
|
const existingIds = new Set(existingGoods.map(item => item.id)); |
|
|
const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); |
|
|
const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); |
|
|
|
|
|
|
|
|
|
|
|
// 合并现有商品和去重后的新商品
|
|
|
const updatedGoods = [...existingGoods, ...uniqueNewGoods] |
|
|
const updatedGoods = [...existingGoods, ...uniqueNewGoods] |
|
|
|
|
|
|
|
|
const filteredGoods = this.applyFilters(updatedGoods, false) |
|
|
const filteredGoods = this.applyFilters(updatedGoods, false) |
|
|
@ -754,13 +789,22 @@ Page({ |
|
|
processCachedGoods: function(cachedGoods, isLoadMore) { |
|
|
processCachedGoods: function(cachedGoods, isLoadMore) { |
|
|
console.log('processCachedGoods 被调用, isLoadMore:', isLoadMore); |
|
|
console.log('processCachedGoods 被调用, isLoadMore:', isLoadMore); |
|
|
|
|
|
|
|
|
// 处理商品数据格式
|
|
|
// 处理商品数据格式并去重
|
|
|
const processedGoods = cachedGoods.map(product => { |
|
|
const goodsMap = new Map(); |
|
|
|
|
|
|
|
|
|
|
|
cachedGoods.forEach(product => { |
|
|
const imageUrls = product.imageUrls || product.images || []; |
|
|
const imageUrls = product.imageUrls || product.images || []; |
|
|
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
|
|
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
|
|
|
|
|
|
|
|
return { |
|
|
// 确保商品ID的一致性
|
|
|
|
|
|
const productId = product.productId || product.id; |
|
|
|
|
|
|
|
|
|
|
|
// 只有当商品ID不存在时才添加,避免重复
|
|
|
|
|
|
if (!goodsMap.has(productId)) { |
|
|
|
|
|
const processedProduct = { |
|
|
...product, |
|
|
...product, |
|
|
|
|
|
id: productId, // 统一使用id字段
|
|
|
|
|
|
productId: productId, // 同时保留productId字段
|
|
|
category: product.category || '', |
|
|
category: product.category || '', |
|
|
fullRegion: product.region || '', |
|
|
fullRegion: product.region || '', |
|
|
region: product.region ? this.extractProvince(product.region) : '', |
|
|
region: product.region ? this.extractProvince(product.region) : '', |
|
|
@ -780,9 +824,15 @@ Page({ |
|
|
isFavorite: false, |
|
|
isFavorite: false, |
|
|
currentImageIndex: 0, |
|
|
currentImageIndex: 0, |
|
|
imageUrls: formattedImageUrls |
|
|
imageUrls: formattedImageUrls |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
goodsMap.set(productId, processedProduct); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 转换为数组
|
|
|
|
|
|
const processedGoods = Array.from(goodsMap.values()); |
|
|
|
|
|
|
|
|
// 更新收藏状态
|
|
|
// 更新收藏状态
|
|
|
this.updateGoodsFavoriteStatus(processedGoods, false); |
|
|
this.updateGoodsFavoriteStatus(processedGoods, false); |
|
|
|
|
|
|
|
|
@ -862,6 +912,21 @@ Page({ |
|
|
filtered = filtered.filter(item => item.isAd || (item.region && item.region.includes(selectedRegion))) |
|
|
filtered = filtered.filter(item => item.isAd || (item.region && item.region.includes(selectedRegion))) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 去重处理 - 确保返回的商品列表中没有重复的商品
|
|
|
|
|
|
const uniqueGoodsMap = new Map(); |
|
|
|
|
|
filtered.forEach(item => { |
|
|
|
|
|
// 使用id作为唯一标识,如果id不存在则使用productId
|
|
|
|
|
|
const uniqueId = item.id || item.productId; |
|
|
|
|
|
if (uniqueId && !uniqueGoodsMap.has(uniqueId)) { |
|
|
|
|
|
uniqueGoodsMap.set(uniqueId, item); |
|
|
|
|
|
} |
|
|
|
|
|
// 对于广告位,使用它们的唯一id
|
|
|
|
|
|
else if (item.isAd && !uniqueGoodsMap.has(item.id)) { |
|
|
|
|
|
uniqueGoodsMap.set(item.id, item); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
filtered = Array.from(uniqueGoodsMap.values()); |
|
|
|
|
|
|
|
|
if (shouldSort) { |
|
|
if (shouldSort) { |
|
|
filtered.sort((a, b) => { |
|
|
filtered.sort((a, b) => { |
|
|
const reservedCountA = a.reservedCount || 0 |
|
|
const reservedCountA = a.reservedCount || 0 |
|
|
|