Browse Source

修复下拉刷新清除本地缓存并重新加载数据的问题

pull/11/head
徐飞洋 2 months ago
parent
commit
d55fec346f
  1. 71
      pages/index/index.js
  2. 5
      pages/index/index.json

71
pages/index/index.js

@ -436,8 +436,6 @@ Page({
return
}
const existingGoods = [...this.data.goods]
this.setData({
isRefreshing: true,
page: 1,
@ -481,7 +479,7 @@ Page({
this.setData({ isRefreshing: false })
if (res.success && res.products) {
this.processRefreshData(res.products, existingGoods)
this.processRefreshData(res.products)
wx.showToast({
title: '刷新成功',
icon: 'success',
@ -506,7 +504,7 @@ Page({
this.setData({ isRefreshing: false });
if (soldOutRes.success && soldOutRes.products && soldOutRes.products.length > 0) {
this.processRefreshData(soldOutRes.products, existingGoods);
this.processRefreshData(soldOutRes.products);
wx.showToast({
title: '刷新成功',
icon: 'success',
@ -797,7 +795,7 @@ Page({
},
// 处理刷新数据 - 查重并保持原有商品
processRefreshData: function (newProducts, existingGoods) {
processRefreshData: function (newProducts) {
let newGoods = newProducts.map(product => {
const imageUrls = product.imageUrls || product.images || [];
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls];
@ -813,6 +811,44 @@ Page({
};
});
// 计算库存总数 - 支持逗号分隔的数字字符串
const calculateTotalStock = (value) => {
if (!value) return 0;
if (typeof value === 'string') {
// 支持逗号分隔的数字字符串,如 "23,34,24"
return value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0).reduce((sum, num) => sum + num, 0);
}
return parseInt(value) || 0;
};
// 优化库存计算 - 尝试多个字段
const minOrder = calculateTotalStock(product.minOrder);
const quantity = calculateTotalStock(product.quantity);
const stock = calculateTotalStock(product.stock);
const inventory = calculateTotalStock(product.inventory);
const availableStock = calculateTotalStock(product.availableStock);
const totalAvailable = calculateTotalStock(product.totalAvailable);
// 优先使用最具体的库存字段
const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable;
const totalStock = primaryStock;
// 智能库存显示 - 库存>=10000显示"库存充足",库存=0显示"暂无",其他显示具体数字
let displayStock;
if (totalStock >= 10000) {
// 库存>=10000时显示"库存充足"
displayStock = '充足';
} else if (totalStock === 0) {
// 库存=0时显示"暂无"
displayStock = '暂无';
} else {
// 其他情况显示具体数字
displayStock = totalStock;
}
// 格式化规格显示
const formattedSpec = this.formatSpecification(product.specification || product.spec || '', product.yolk || '');
return {
...product,
id: productId, // 统一使用id字段
@ -837,7 +873,11 @@ Page({
currentImageIndex: 0,
imageUrls: formattedImageUrls,
// 添加预处理后的媒体数据,包含类型信息
mediaItems: mediaItems
mediaItems: mediaItems,
totalStock: displayStock, // 使用优化后的库存显示值
originalTotalStock: totalStock, // 保留原始计算值用于调试
displaySpecification: formattedSpec.displaySpec, // 格式化后的规格
displayYolk: formattedSpec.displayYolk // 格式化后的蛋黄
}
})
@ -850,18 +890,11 @@ Page({
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 combinedGoods = [...existingGoods, ...uniqueNewGoods]
// 下拉刷新时完全使用新获取的商品数据,不再与旧数据合并
const newGoodsOnly = Array.from(newGoodsMap.values());
// 对合并后的完整数据进行过滤和排序
const filteredGoods = this.applyFilters(combinedGoods, true)
// 对新数据进行过滤和排序
const filteredGoods = this.applyFilters(newGoodsOnly, true)
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods);
const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory;
@ -869,7 +902,7 @@ Page({
const cacheKey = `${currentCategory}_${currentKeyword}`;
this.setData({
goods: combinedGoods,
goods: newGoodsOnly,
filteredGoods: filteredGoods,
groupedGoods: groupedGoods,
loadingMore: false,
@ -881,7 +914,7 @@ Page({
// 更新分类查询缓存
const newCategoryQueryCache = { ...this.data.categoryQueryCache };
newCategoryQueryCache[cacheKey] = combinedGoods;
newCategoryQueryCache[cacheKey] = newGoodsOnly;
this.setData({
categoryQueryCache: newCategoryQueryCache
})

5
pages/index/index.json

@ -1,5 +1,8 @@
{
"usingComponents": {
"navigation-bar": "/components/navigation-bar/navigation-bar"
}
},
"enablePullDownRefresh": true,
"backgroundTextStyle": "dark",
"backgroundColor": "#f8f8f8"
}
Loading…
Cancel
Save