Browse Source

修复库存显示问题:将0件改为暂无,提升用户体验

pull/3/head
徐飞洋 2 months ago
parent
commit
a7fe110537
  1. 31
      pages/index/index.js
  2. 4
      pages/index/index.wxml

31
pages/index/index.js

@ -464,6 +464,11 @@ Page({
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,
id: productId, // 统一使用id字段
@ -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);

4
pages/index/index.wxml

@ -10,7 +10,7 @@
<view class="search-input-wrapper">
<!-- 将地区选择器放在最左边 -->
<view class="region-selector" bindtap="toggleRegionPicker">
<text>{{selectedRegion || '全国'}} </text>
<text>{{selectedRegion || '全国'}} </text>
</view>
<!-- 搜索图标和输入框 -->
<text class="search-icon">🔍</text>
@ -164,7 +164,7 @@
<view class="product-status-row">
<view class="status-tag source-{{item.sourceType === '三方认证' ? 'third' : (item.sourceType === '平台货源' ? 'platform' : 'unverified')}}">{{item.sourceType || ''}}</view>
<view class="status-tag negotiate-{{item.negotiateStatus === '可议价' ? 'yes' : 'no'}}">{{item.negotiateStatus}}</view>
<view class="status-tag item-count">库存:{{item.totalStock || '暂无'}}</view>
<view class="status-tag item-count">库存:{{item.totalStock > 0 ? item.totalStock + '件' : '暂无'}}</view>
</view>
<view class="product-meta">
<text class="sales-count">已有{{item.reservedCount || 0}}人收藏</text>

Loading…
Cancel
Save