Browse Source

修复updatedGoods未定义错误并实现按createdAt降序排序

pull/11/head
徐飞洋 2 months ago
parent
commit
cdf36d46f9
  1. 34
      pages/index/index.js

34
pages/index/index.js

@ -443,7 +443,7 @@ Page({
page: 1, page: 1,
soldOutPage: 1, soldOutPage: 1,
hasMoreData: true, hasMoreData: true,
goods: existingGoods, goods: [], // 下拉刷新时清空商品数组,避免旧数据影响排序
filteredGoods: [], filteredGoods: [],
// 清除缓存以确保获取最新数据 // 清除缓存以确保获取最新数据
categoryQueryCache: {}, categoryQueryCache: {},
@ -829,10 +829,12 @@ Page({
const existingIds = new Set(existingGoods.map(item => item.id)); const existingIds = new Set(existingGoods.map(item => item.id));
const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id));
// 合并现有商品和去重后的新商品 // 先将现有商品和新商品合并,然后重新处理整个数据集
const updatedGoods = [...existingGoods, ...uniqueNewGoods] // 这样可以确保所有商品都经过相同的排序规则处理
const combinedGoods = [...existingGoods, ...uniqueNewGoods]
const filteredGoods = this.applyFilters(updatedGoods, false)
// 对合并后的完整数据进行过滤和排序
const filteredGoods = this.applyFilters(combinedGoods, false)
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods); const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods);
const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory; const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory;
@ -840,7 +842,7 @@ Page({
const cacheKey = `${currentCategory}_${currentKeyword}`; const cacheKey = `${currentCategory}_${currentKeyword}`;
this.setData({ this.setData({
goods: updatedGoods, goods: combinedGoods,
filteredGoods: filteredGoods, filteredGoods: filteredGoods,
groupedGoods: groupedGoods, groupedGoods: groupedGoods,
loadingMore: false, loadingMore: false,
@ -852,7 +854,7 @@ Page({
// 更新分类查询缓存 // 更新分类查询缓存
const newCategoryQueryCache = { ...this.data.categoryQueryCache }; const newCategoryQueryCache = { ...this.data.categoryQueryCache };
newCategoryQueryCache[cacheKey] = updatedGoods; newCategoryQueryCache[cacheKey] = combinedGoods;
this.setData({ this.setData({
categoryQueryCache: newCategoryQueryCache categoryQueryCache: newCategoryQueryCache
}) })
@ -1382,24 +1384,16 @@ Page({
} }
}) })
// Define the sorting function for existing criteria // Define the sorting function: only by createdAt descending order
const sortByExistingCriteria = (a, b) => { const sortByCreatedAtDesc = (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
const createdAtA = new Date(a.createdAt || 0).getTime() const createdAtA = new Date(a.createdAt || 0).getTime()
const createdAtB = new Date(b.createdAt || 0).getTime() const createdAtB = new Date(b.createdAt || 0).getTime()
return createdAtB - createdAtA return createdAtB - createdAtA
} }
// Sort each group by existing criteria // Sort each group by createdAt descending
publishedItems.sort(sortByExistingCriteria) publishedItems.sort(sortByCreatedAtDesc)
soldOutItems.sort(sortByExistingCriteria) soldOutItems.sort(sortByCreatedAtDesc)
// Combine groups: all published first, then all sold_out // Combine groups: all published first, then all sold_out
filtered = [...publishedItems, ...soldOutItems] filtered = [...publishedItems, ...soldOutItems]

Loading…
Cancel
Save