diff --git a/pages/index/index.js b/pages/index/index.js index d06468b..8f18351 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -463,6 +463,11 @@ Page({ const minOrder = calculateTotalStock(product.minOrder); const quantity = calculateTotalStock(product.quantity); const totalStock = minOrder + quantity; + + // 如果库存为0但有其他库存信息,显示"充足"或基于其他字段 + const displayStock = totalStock > 0 ? totalStock : + (product.stock > 0 ? product.stock : + (product.inventory > 0 ? product.inventory : '充足')); return { ...product, @@ -487,7 +492,8 @@ Page({ isFavorite: false, currentImageIndex: 0, imageUrls: formattedImageUrls, - totalStock: totalStock // 合并后的库存总数 + totalStock: displayStock, // 使用优化后的库存显示值 + originalTotalStock: totalStock // 保留原始计算值用于调试 } }) @@ -816,6 +822,25 @@ Page({ // 只有当商品ID不存在时才添加,避免重复 if (!goodsMap.has(productId)) { + // 计算库存总数 - 支持逗号分隔的数字字符串 + const calculateTotalStock = (value) => { + if (!value) return 0; + if (typeof value === 'string') { + // 支持逗号分隔的数字字符串,如 "23,34,24" + return value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0).reduce((sum, num) => sum + num, 0); + } + return parseInt(value) || 0; + }; + + const minOrder = calculateTotalStock(product.minOrder); + const quantity = calculateTotalStock(product.quantity); + const totalStock = minOrder + quantity; + + // 如果库存为0但有其他库存信息,显示"充足"或基于其他字段 + const displayStock = totalStock > 0 ? totalStock : + (product.stock > 0 ? product.stock : + (product.inventory > 0 ? product.inventory : '充足')); + const processedProduct = { ...product, id: productId, // 统一使用id字段 @@ -838,7 +863,9 @@ Page({ isReserved: false, isFavorite: false, currentImageIndex: 0, - imageUrls: formattedImageUrls + imageUrls: formattedImageUrls, + totalStock: displayStock, // 使用优化后的库存显示值 + originalTotalStock: totalStock // 保留原始计算值用于调试 }; goodsMap.set(productId, processedProduct); diff --git a/pages/index/index.wxml b/pages/index/index.wxml index a3d0c09..fc5c592 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -10,7 +10,7 @@ - {{selectedRegion || '全国'}} ▼ + {{selectedRegion || '全国'}} ➤ 🔍 @@ -164,7 +164,7 @@ {{item.sourceType || ''}} {{item.negotiateStatus}} - 库存:{{item.totalStock || '暂无'}} + 库存:{{item.totalStock > 0 ? item.totalStock + '件' : '暂无'}} 已有{{item.reservedCount || 0}}人收藏