diff --git a/pages/index/index.js b/pages/index/index.js index 557ff8a..e00bee1 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -460,14 +460,40 @@ Page({ return parseInt(value) || 0; }; + // 优化库存计算 - 尝试多个字段 const minOrder = calculateTotalStock(product.minOrder); const quantity = calculateTotalStock(product.quantity); - const totalStock = minOrder + quantity; + const stock = calculateTotalStock(product.stock); + const inventory = calculateTotalStock(product.inventory); + const availableStock = calculateTotalStock(product.availableStock); + const totalAvailable = calculateTotalStock(product.totalAvailable); - // 如果库存为0但有其他库存信息,显示"充足"或基于其他字段 - const displayStock = totalStock > 0 ? totalStock : - (product.stock > 0 ? product.stock : - (product.inventory > 0 ? product.inventory : '充足')); + // 优先使用最具体的库存字段 + const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; + const totalStock = primaryStock; + + // 智能库存显示 - 有数据时显示具体数字,没有数据时显示"充足" + let displayStock; + if (totalStock > 0) { + displayStock = totalStock; + } else if (stock > 0 || inventory > 0 || availableStock > 0 || totalAvailable > 0) { + displayStock = stock || inventory || availableStock || totalAvailable; + } else { + // 如果有商品数据但没有库存信息,显示"充足" + displayStock = '充足'; + } + + console.log('库存计算详情:', { + productId: product.productId || product.id, + minOrder: product.minOrder, + quantity: product.quantity, + stock: product.stock, + inventory: product.inventory, + availableStock: product.availableStock, + totalAvailable: product.totalAvailable, + calculatedTotal: totalStock, + displayStock: displayStock + }); return { ...product, @@ -832,14 +858,28 @@ Page({ return parseInt(value) || 0; }; + // 优化库存计算 - 尝试多个字段 const minOrder = calculateTotalStock(product.minOrder); const quantity = calculateTotalStock(product.quantity); - const totalStock = minOrder + quantity; + const stock = calculateTotalStock(product.stock); + const inventory = calculateTotalStock(product.inventory); + const availableStock = calculateTotalStock(product.availableStock); + const totalAvailable = calculateTotalStock(product.totalAvailable); - // 如果库存为0但有其他库存信息,显示"充足"或基于其他字段 - const displayStock = totalStock > 0 ? totalStock : - (product.stock > 0 ? product.stock : - (product.inventory > 0 ? product.inventory : '充足')); + // 优先使用最具体的库存字段 + const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; + const totalStock = primaryStock; + + // 智能库存显示 - 有数据时显示具体数字,没有数据时显示"充足" + let displayStock; + if (totalStock > 0) { + displayStock = totalStock; + } else if (stock > 0 || inventory > 0 || availableStock > 0 || totalAvailable > 0) { + displayStock = stock || inventory || availableStock || totalAvailable; + } else { + // 如果有商品数据但没有库存信息,显示"充足" + displayStock = '充足'; + } const processedProduct = { ...product, @@ -1188,6 +1228,7 @@ Page({ // 立即关闭弹窗 this.setData({ selectedRegion: region, + searchKeyword: '', // 清除搜索关键词,筛选框优先级更高 showRegionPicker: false }) @@ -1230,32 +1271,54 @@ Page({ return // 如果选择的分类和当前相同,不重复加载 } + // 如果有现有的商品数据,先基于现有数据进行筛选,而不是清空 + const originalGoods = this.data.goods; + const hasOriginalGoods = originalGoods && originalGoods.length > 0; + this.setData({ selectedCategory: category, - page: 1, - hasMoreData: true, - goods: [], - filteredGoods: [], - loadingMore: false, - isLoading: true - }) - - const currentCategory = category === '全部' ? '' : category; - const currentKeyword = this.data.searchKeyword; - const cacheKey = `${currentCategory}_${currentKeyword}`; - const now = new Date().getTime(); + searchKeyword: '', // 清除搜索关键词,筛选框优先级更高 + loadingMore: false + }); - // 检查缓存(首次加载时使用缓存,加载更多时不用) - if (this.data.categoryQueryCache[cacheKey] && - (now - this.data.lastDataTimestamp) < this.data.cacheValidDuration) { - console.log('selectCategory使用缓存数据,cacheKey:', cacheKey); - const cachedGoods = this.data.categoryQueryCache[cacheKey]; - // 更新timestamp以确保缓存有效 - this.setData({ lastDataTimestamp: now }); - this.processCachedGoods(cachedGoods, false); + if (hasOriginalGoods) { + // 使用现有商品数据进行筛选 + console.log('使用现有商品数据进行分类筛选,原始商品数量:', originalGoods.length); + const filteredGoods = this.applyFilters(originalGoods, false); + const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods); + + this.setData({ + filteredGoods: filteredGoods, + groupedGoods: groupedGoods, + isLoading: false + }); } else { - // 重新从服务器加载数据 - this.loadGoods(false) + // 如果没有现有数据,清空并重新加载 + this.setData({ + page: 1, + hasMoreData: true, + goods: [], + filteredGoods: [], + isLoading: true + }); + + const currentCategory = category === '全部' ? '' : category; + const currentKeyword = this.data.searchKeyword; + const cacheKey = `${currentCategory}_${currentKeyword}`; + const now = new Date().getTime(); + + // 检查缓存(首次加载时使用缓存,加载更多时不用) + if (this.data.categoryQueryCache[cacheKey] && + (now - this.data.lastDataTimestamp) < this.data.cacheValidDuration) { + console.log('selectCategory使用缓存数据,cacheKey:', cacheKey); + const cachedGoods = this.data.categoryQueryCache[cacheKey]; + // 更新timestamp以确保缓存有效 + this.setData({ lastDataTimestamp: now }); + this.processCachedGoods(cachedGoods, false); + } else { + // 重新从服务器加载数据 + this.loadGoods(false); + } } }, diff --git a/pages/index/index.wxml b/pages/index/index.wxml index 6c54b29..9233547 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -160,11 +160,11 @@ {{item.name}} - {{item.specification || '无'}} | {{item.yolk}} - + {{item.specification || '无'}} | {{item.yolk}} + {{item.sourceType || ''}} {{item.negotiateStatus}} - 库存:{{item.totalStock > 0 ? item.totalStock + '件' : '暂无'}} + 库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}} 已有{{item.reservedCount || 0}}人收藏