Browse Source

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

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

71
pages/index/index.js

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

Loading…
Cancel
Save