Browse Source

fix: 修复下滑加载更多数据缓存问题

- 加载更多时追加数据到缓存而非覆盖
- 去重处理避免重复数据
- 确保缓存保存所有页数据
pull/1/head
徐飞洋 2 months ago
parent
commit
d2e63ea81b
  1. 14
      pages/index/index.js

14
pages/index/index.js

@ -738,15 +738,23 @@ Page({
const totalPages = res.totalPages || Math.ceil(totalGoods / this.data.pageSize); const totalPages = res.totalPages || Math.ceil(totalGoods / this.data.pageSize);
const hasMoreData = page < totalPages && res.products.length > 0; const hasMoreData = page < totalPages && res.products.length > 0;
// 更新缓存 // 更新缓存(加载更多时追加数据)
const updatedCache = { ...this.data.categoryQueryCache }; const updatedCache = { ...this.data.categoryQueryCache };
updatedCache[cacheKey] = res.products; if (isLoadMore && updatedCache[cacheKey]) {
// 追加新数据到缓存
const existingIds = new Set(updatedCache[cacheKey].map(item => item.id));
const newProducts = res.products.filter(item => !existingIds.has(item.id));
updatedCache[cacheKey] = [...updatedCache[cacheKey], ...newProducts];
} else {
// 首次加载或刷新时替换缓存
updatedCache[cacheKey] = res.products;
}
this.setData({ this.setData({
hasMoreData, hasMoreData,
categoryQueryCache: updatedCache, categoryQueryCache: updatedCache,
lastDataTimestamp: now, lastDataTimestamp: now,
goodsCache: res.products goodsCache: updatedCache[cacheKey]
}) })
this.processGoodsData(res.products, isLoadMore) this.processGoodsData(res.products, isLoadMore)

Loading…
Cancel
Save