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]