From 86ca61a9a3e5f8282aa8efbd0f53100415479033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Wed, 7 Jan 2026 19:07:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=95=86=E5=93=81=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=B1=95=E7=A4=BA=E9=97=AE=E9=A2=98=EF=BC=9A1.=20?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E5=85=88=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=89=80?= =?UTF-8?q?=E6=9C=89published=E5=95=86=E5=93=81=E5=86=8D=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?sold=5Fout=E5=95=86=E5=93=81=202.=20=E4=BF=AE=E5=A4=8D=E5=BD=93?= =?UTF-8?q?published=E5=95=86=E5=93=81=E5=8F=AA=E6=9C=891=E9=A1=B5?= =?UTF-8?q?=E6=97=B6=E6=97=A0=E6=B3=95=E6=98=BE=E7=A4=BAsold=5Fout?= =?UTF-8?q?=E5=95=86=E5=93=81=E7=9A=84=E9=97=AE=E9=A2=98=203.=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96sold=5Fout=E5=95=86=E5=93=81=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.js | 71 +++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index be56fae..49738f2 100644 --- a/pages/index/index.js +++ b/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; - - // 如果published商品数量不足pageSize,标记需要查询sold_out - const shouldQuerySoldOut = res.products.length < pageSize; + // 只需要判断是否还有下一页,不需要检查当前页数据量是否等于pageSize + const publishedHasMore = page < totalPages; + + // 当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);