From cdf36d46f900933716dbb70a91dee44abb7f5d78 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: Fri, 9 Jan 2026 09:19:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DupdatedGoods=E6=9C=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=94=99=E8=AF=AF=E5=B9=B6=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=8C=89createdAt=E9=99=8D=E5=BA=8F=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index 2f2e6e8..c5e1890 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -443,7 +443,7 @@ Page({ page: 1, soldOutPage: 1, hasMoreData: true, - goods: existingGoods, + goods: [], // 下拉刷新时清空商品数组,避免旧数据影响排序 filteredGoods: [], // 清除缓存以确保获取最新数据 categoryQueryCache: {}, @@ -829,10 +829,12 @@ Page({ const existingIds = new Set(existingGoods.map(item => item.id)); const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); - // 合并现有商品和去重后的新商品 - const updatedGoods = [...existingGoods, ...uniqueNewGoods] - - const filteredGoods = this.applyFilters(updatedGoods, false) + // 先将现有商品和新商品合并,然后重新处理整个数据集 + // 这样可以确保所有商品都经过相同的排序规则处理 + const combinedGoods = [...existingGoods, ...uniqueNewGoods] + + // 对合并后的完整数据进行过滤和排序 + const filteredGoods = this.applyFilters(combinedGoods, false) const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods); const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory; @@ -840,7 +842,7 @@ Page({ const cacheKey = `${currentCategory}_${currentKeyword}`; this.setData({ - goods: updatedGoods, + goods: combinedGoods, filteredGoods: filteredGoods, groupedGoods: groupedGoods, loadingMore: false, @@ -852,7 +854,7 @@ Page({ // 更新分类查询缓存 const newCategoryQueryCache = { ...this.data.categoryQueryCache }; - newCategoryQueryCache[cacheKey] = updatedGoods; + newCategoryQueryCache[cacheKey] = combinedGoods; this.setData({ categoryQueryCache: newCategoryQueryCache }) @@ -1382,24 +1384,16 @@ Page({ } }) - // Define the sorting function for existing criteria - const sortByExistingCriteria = (a, b) => { - const reservedCountA = a.reservedCount || 0 - const reservedCountB = b.reservedCount || 0 - if (reservedCountB !== reservedCountA) return reservedCountB - reservedCountA - - const priceA = parseFloat(a.price || 0) - const priceB = parseFloat(b.price || 0) - if (!isNaN(priceB) && !isNaN(priceA) && priceA !== priceB) return priceA - priceB - + // Define the sorting function: only by createdAt descending order + const sortByCreatedAtDesc = (a, b) => { const createdAtA = new Date(a.createdAt || 0).getTime() const createdAtB = new Date(b.createdAt || 0).getTime() return createdAtB - createdAtA } - // Sort each group by existing criteria - publishedItems.sort(sortByExistingCriteria) - soldOutItems.sort(sortByExistingCriteria) + // Sort each group by createdAt descending + publishedItems.sort(sortByCreatedAtDesc) + soldOutItems.sort(sortByCreatedAtDesc) // Combine groups: all published first, then all sold_out filtered = [...publishedItems, ...soldOutItems]