Browse Source

修复分类预加载问题,首次加载全部数据

pull/1/head
徐飞洋 2 months ago
parent
commit
56aaaa12fa
  1. 28
      pages/index/index.js

28
pages/index/index.js

@ -532,18 +532,36 @@ Page({
const categories = this.data.categories;
const selectedCategory = this.data.selectedCategory;
// 如果是首次加载全部数据,需要把返回的所有数据按实际分类存储
if (selectedCategory === '全部' && !isLoadMore) {
categories.forEach(cat => {
if (cat === '全部') {
categorizedGoods[cat] = updatedGoods;
} else {
categorizedGoods[cat] = updatedGoods.filter(item => item.isAd || item.category === cat);
}
});
} else {
// 只更新当前选中的分类
categories.forEach(cat => {
if (cat === '全部') {
categorizedGoods[cat] = updatedGoods;
} else if (cat === selectedCategory) {
categorizedGoods[cat] = updatedGoods.filter(item => item.isAd || !item.category || item.category === cat);
} else {
categorizedGoods[cat] = [];
}
});
}
// 应用地区筛选后的分类数据
const filteredByRegion = this.applyRegionFilter(updatedGoods);
if (selectedCategory === '全部' && !isLoadMore) {
categories.forEach(cat => {
if (cat !== '全部') {
categorizedGoods[cat + '_filtered'] = filteredByRegion.filter(item => item.isAd || item.category === cat);
}
});
} else {
categories.forEach(cat => {
if (cat !== '全部') {
if (cat === selectedCategory) {
@ -553,6 +571,7 @@ Page({
}
}
});
}
// 根据当前选中的分类和地区筛选获取商品
let filteredGoods;
@ -719,13 +738,16 @@ Page({
const timestamp = new Date().getTime();
const currentPage = isLoadMore ? this.data.page : 1
// 首次加载时获取全部数据,在本地进行分类
const shouldLoadAll = !isLoadMore && this.data.selectedCategory === '全部';
API.getProductList('published', {
timestamp: timestamp,
viewMode: 'shopping',
page: currentPage,
pageSize: this.data.pageSize,
keyword: this.data.searchKeyword,
category: this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory
category: shouldLoadAll ? '' : this.data.selectedCategory
})
.then(res => {
wx.hideLoading();

Loading…
Cancel
Save