|
|
@ -2083,18 +2083,86 @@ Page({ |
|
|
loadHomeGoods: function () { |
|
|
loadHomeGoods: function () { |
|
|
this.setData({ loadingHome: true }); |
|
|
this.setData({ loadingHome: true }); |
|
|
|
|
|
|
|
|
|
|
|
// 获取当前商品的规格信息
|
|
|
|
|
|
const currentGoods = this.data.goodsDetail; |
|
|
|
|
|
const currentSpecifications = currentGoods.weightQuantityData || []; |
|
|
|
|
|
// 提取当前商品的净重规格(如"净重41-42")
|
|
|
|
|
|
const currentWeightSpecs = currentSpecifications.map(item => item.weightSpec.trim()) |
|
|
|
|
|
.filter(spec => spec && (spec.includes('净重') || spec.includes('毛重'))); |
|
|
|
|
|
|
|
|
// 调用API获取首页商品列表
|
|
|
// 调用API获取首页商品列表
|
|
|
API.getProducts() |
|
|
API.getProducts() |
|
|
.then(res => { |
|
|
.then(res => { |
|
|
console.log('获取首页商品数据成功:', res); |
|
|
console.log('获取首页商品数据成功:', res); |
|
|
// API.getProducts()直接返回商品数组,而不是包含data字段的对象
|
|
|
// API.getProducts()直接返回商品数组,而不是包含data字段的对象
|
|
|
if (Array.isArray(res)) { |
|
|
if (Array.isArray(res)) { |
|
|
// 为每个商品添加mediaItems字段
|
|
|
// 为每个商品添加mediaItems字段和weightQuantityData字段,并过滤出与当前商品规格匹配的商品
|
|
|
const processedGoods = res.map(goods => { |
|
|
const processedGoods = res.map(goods => { |
|
|
|
|
|
// 处理每个首页商品的规格数据
|
|
|
|
|
|
let weightSpecString = ''; |
|
|
|
|
|
let quantityString = ''; |
|
|
|
|
|
|
|
|
|
|
|
// 检查规格字段是否包含净重信息
|
|
|
|
|
|
if (goods.spec && typeof goods.spec === 'string' && (goods.spec.includes('净重') || goods.spec.includes('毛重'))) { |
|
|
|
|
|
weightSpecString = goods.spec; |
|
|
|
|
|
} else if (goods.specification && typeof goods.specification === 'string' && (goods.specification.includes('净重') || goods.specification.includes('毛重'))) { |
|
|
|
|
|
weightSpecString = goods.specification; |
|
|
|
|
|
} else if (goods.grossWeight) { |
|
|
|
|
|
weightSpecString = String(goods.grossWeight); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 处理件数数据
|
|
|
|
|
|
if (goods.minOrder) { |
|
|
|
|
|
quantityString = String(goods.minOrder); |
|
|
|
|
|
} else if (goods.quantity) { |
|
|
|
|
|
quantityString = String(goods.quantity); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 调用processWeightAndQuantityData处理规格数据
|
|
|
|
|
|
const weightQuantityData = processWeightAndQuantityData(weightSpecString, quantityString, ''); |
|
|
|
|
|
|
|
|
|
|
|
// 提取省份信息
|
|
|
|
|
|
const province = extractProvince(goods.region || ''); |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
...goods, |
|
|
...goods, |
|
|
mediaItems: processMediaUrls(goods.imageUrls) |
|
|
mediaItems: processMediaUrls(goods.imageUrls), |
|
|
|
|
|
weightQuantityData: weightQuantityData, |
|
|
|
|
|
province: province // 添加省份字段
|
|
|
}; |
|
|
}; |
|
|
|
|
|
}).filter(goods => { |
|
|
|
|
|
// 只有当当前商品有明确的规格时才进行筛选
|
|
|
|
|
|
if (currentWeightSpecs.length === 0) { |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取该商品的规格信息
|
|
|
|
|
|
const goodsSpecifications = goods.weightQuantityData || []; |
|
|
|
|
|
// 提取该商品的净重规格
|
|
|
|
|
|
const goodsWeightSpecs = goodsSpecifications.map(item => item.weightSpec.trim()) |
|
|
|
|
|
.filter(spec => spec && (spec.includes('净重') || spec.includes('毛重'))); |
|
|
|
|
|
|
|
|
|
|
|
// 辅助函数:获取规格的匹配版本(处理44净重=49毛重的转换规则)
|
|
|
|
|
|
function getMatchingSpecs(spec) { |
|
|
|
|
|
const specs = [spec]; |
|
|
|
|
|
|
|
|
|
|
|
// 添加转换规则:44净重=49毛重
|
|
|
|
|
|
if (spec === '44净重') { |
|
|
|
|
|
specs.push('49毛重'); |
|
|
|
|
|
} else if (spec === '49毛重') { |
|
|
|
|
|
specs.push('44净重'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return specs; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 检查是否有匹配的规格(考虑转换规则)
|
|
|
|
|
|
return goodsWeightSpecs.some(goodsSpec => |
|
|
|
|
|
currentWeightSpecs.some(currentSpec => { |
|
|
|
|
|
const matchingSpecs = getMatchingSpecs(currentSpec); |
|
|
|
|
|
return matchingSpecs.includes(goodsSpec); |
|
|
|
|
|
}) |
|
|
|
|
|
); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
@ -2232,13 +2300,19 @@ Page({ |
|
|
// 等待所有商品详情获取完成
|
|
|
// 等待所有商品详情获取完成
|
|
|
Promise.all(productPromises) |
|
|
Promise.all(productPromises) |
|
|
.then(products => { |
|
|
.then(products => { |
|
|
// 过滤掉获取失败的商品,并为每个商品添加mediaItems字段
|
|
|
// 过滤掉获取失败的商品,并为每个商品添加mediaItems字段和省份信息
|
|
|
const validProducts = products |
|
|
const validProducts = products |
|
|
.filter(product => product !== null) |
|
|
.filter(product => product !== null) |
|
|
.map(product => ({ |
|
|
.map(product => { |
|
|
...product, |
|
|
// 提取省份信息
|
|
|
mediaItems: processMediaUrls(product.imageUrls) |
|
|
const province = extractProvince(product.region || ''); |
|
|
})); |
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
...product, |
|
|
|
|
|
mediaItems: processMediaUrls(product.imageUrls), |
|
|
|
|
|
province: province // 添加省份字段
|
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
console.log('所有商品详情获取完成,有效商品数量:', validProducts.length); |
|
|
console.log('所有商品详情获取完成,有效商品数量:', validProducts.length); |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
@ -2254,11 +2328,17 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} else { |
|
|
} else { |
|
|
// 收藏列表已经包含完整商品信息,为每个商品添加mediaItems字段
|
|
|
// 收藏列表已经包含完整商品信息,为每个商品添加mediaItems字段和省份信息
|
|
|
const processedFavorites = favoritesList.map(product => ({ |
|
|
const processedFavorites = favoritesList.map(product => { |
|
|
...product, |
|
|
// 提取省份信息
|
|
|
mediaItems: processMediaUrls(product.imageUrls) |
|
|
const province = extractProvince(product.region || ''); |
|
|
})); |
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
...product, |
|
|
|
|
|
mediaItems: processMediaUrls(product.imageUrls), |
|
|
|
|
|
province: province // 添加省份字段
|
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
favoriteGoods: processedFavorites, |
|
|
favoriteGoods: processedFavorites, |
|
|
|