Browse Source

修复商品列表展示问题:1. 确保先加载完所有published商品再加载sold_out商品 2. 修复当published商品只有1页时无法显示sold_out商品的问题 3. 优化sold_out商品分页加载逻辑

pull/11/head
徐飞洋 2 months ago
parent
commit
86ca61a9a3
  1. 69
      pages/index/index.js

69
pages/index/index.js

@ -94,6 +94,7 @@ Page({
hasMoreData: true,
page: 1,
pageSize: 8,
soldOutPage: 1,
// 商品数据缓存
goodsCache: [],
@ -440,6 +441,7 @@ Page({
this.setData({
isRefreshing: true,
page: 1,
soldOutPage: 1,
hasMoreData: true,
goods: existingGoods,
filteredGoods: [],
@ -462,14 +464,18 @@ Page({
});
// 优先查询published状态的商品
API.getProductList(['published'], {
const apiParams = {
timestamp: timestamp,
viewMode: 'shopping',
page: 1,
pageSize: pageSize,
keyword: currentKeyword,
category: currentCategory
})
keyword: currentKeyword
}
// 只有非全部分类时才传递category参数
if (currentCategory) {
apiParams.category = currentCategory
}
API.getProductList(['published'], apiParams)
.then(res => {
this.setData({ isRefreshing: false })
@ -483,14 +489,18 @@ Page({
} else if (res.products.length === 0) {
// 如果published状态没有商品,则查询sold_out状态
console.log('没有published状态的商品,查询sold_out状态的商品');
API.getProductList(['sold_out'], {
const apiParams = {
timestamp: timestamp,
viewMode: 'shopping',
page: 1,
pageSize: pageSize,
keyword: currentKeyword,
category: currentCategory
})
keyword: currentKeyword
}
// 只有非全部分类时才传递category参数
if (currentCategory) {
apiParams.category = currentCategory
}
API.getProductList(['sold_out'], apiParams)
.then(soldOutRes => {
this.setData({ isRefreshing: false });
@ -953,14 +963,18 @@ Page({
const pageSize = this.data.pageSize;
// 优先查询published状态的商品
API.getProductList(['published'], {
const apiParams = {
timestamp: timestamp,
viewMode: 'shopping',
page: page,
pageSize: pageSize,
keyword: currentKeyword,
category: currentCategory
})
keyword: currentKeyword
}
// 只有非全部分类时才传递category参数
if (currentCategory) {
apiParams.category = currentCategory
}
API.getProductList(['published'], apiParams)
.then(res => {
wx.hideLoading();
@ -975,10 +989,11 @@ Page({
console.log('有published商品,处理商品数据');
const totalGoods = res.total || 0;
const totalPages = res.totalPages || Math.ceil(totalGoods / pageSize);
const publishedHasMore = page < totalPages && res.products.length >= pageSize;
// 只需要判断是否还有下一页,不需要检查当前页数据量是否等于pageSize
const publishedHasMore = page < totalPages;
// 如果published商品数量不足pageSize,标记需要查询sold_out
const shouldQuerySoldOut = res.products.length < pageSize;
// 当published商品没有更多数据时,查询sold_out商品
const shouldQuerySoldOut = !publishedHasMore;
// 更新缓存(加载更多时追加数据)
const updatedCache = { ...this.data.categoryQueryCache };
@ -995,18 +1010,14 @@ Page({
// 计算sold_out起始页码和每页数量
let soldOutPageNum = 1;
let soldOutPageSize = pageSize;
if (shouldQuerySoldOut) {
// 如果published不足一页,从第一页开始查sold_out补齐
soldOutPageNum = 1;
soldOutPageSize = pageSize - res.products.length;
} else if (!publishedHasMore && isLoadMore) {
// 如果published已无更多数据,加载更多时查询sold_out下一页
soldOutPageNum = this.data.soldOutPage + 1;
if (shouldQuerySoldOut || (!publishedHasMore && isLoadMore)) {
// 如果published已无更多数据,查询sold_out商品
soldOutPageNum = isLoadMore ? this.data.soldOutPage + 1 : 1;
soldOutPageSize = pageSize;
}
this.setData({
hasMoreData: shouldQuerySoldOut || publishedHasMore,
hasMoreData: publishedHasMore || (shouldQuerySoldOut || this.data.isQueryingSoldOut),
isQueryingSoldOut: shouldQuerySoldOut || (!publishedHasMore && isLoadMore),
publishedHasMore: publishedHasMore,
soldOutPage: soldOutPageNum,
@ -1055,14 +1066,18 @@ Page({
console.log('加载sold_out数据 - page:', soldOutPageNum, 'pageSize:', pageSize);
console.log('查询参数 - page:', soldOutPageNum, 'pageSize:', pageSize, 'keyword:', currentKeyword, 'category:', currentCategory);
API.getProductList(['sold_out'], {
const apiParams = {
timestamp: timestamp,
viewMode: 'shopping',
page: soldOutPageNum,
pageSize: pageSize,
keyword: currentKeyword,
category: currentCategory
})
keyword: currentKeyword
}
// 只有非全部分类时才传递category参数
if (currentCategory) {
apiParams.category = currentCategory
}
API.getProductList(['sold_out'], apiParams)
.then(soldOutRes => {
console.log('===== sold_out状态查询结果 =====');
console.log('soldOutRes.success:', soldOutRes.success);

Loading…
Cancel
Save