Browse Source

修复库存显示问题:扩展库存字段检查范围,优化显示逻辑

pull/3/head
徐飞洋 2 months ago
parent
commit
392316bf33
  1. 127
      pages/index/index.js
  2. 6
      pages/index/index.wxml

127
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);
// 优先使用最具体的库存字段
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 = '充足';
}
// 如果库存为0但有其他库存信息,显示"充足"或基于其他字段
const displayStock = totalStock > 0 ? totalStock :
(product.stock > 0 ? product.stock :
(product.inventory > 0 ? product.inventory : '充足'));
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;
// 如果库存为0但有其他库存信息,显示"充足"或基于其他字段
const displayStock = totalStock > 0 ? totalStock :
(product.stock > 0 ? product.stock :
(product.inventory > 0 ? product.inventory : '充足'));
const stock = calculateTotalStock(product.stock);
const inventory = calculateTotalStock(product.inventory);
const availableStock = calculateTotalStock(product.availableStock);
const totalAvailable = calculateTotalStock(product.totalAvailable);
// 优先使用最具体的库存字段
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
})
searchKeyword: '', // 清除搜索关键词,筛选框优先级更高
loadingMore: false
});
const currentCategory = category === '全部' ? '' : category;
const currentKeyword = this.data.searchKeyword;
const cacheKey = `${currentCategory}_${currentKeyword}`;
const now = new Date().getTime();
if (hasOriginalGoods) {
// 使用现有商品数据进行筛选
console.log('使用现有商品数据进行分类筛选,原始商品数量:', originalGoods.length);
const filteredGoods = this.applyFilters(originalGoods, false);
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods);
// 检查缓存(首次加载时使用缓存,加载更多时不用)
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);
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);
}
}
},

6
pages/index/index.wxml

@ -160,11 +160,11 @@
</view>
<view class="product-info">
<view class="product-title">{{item.name}}</view>
<view class="product-spec">{{item.specification || '无'}}<text wx:if="{{item.yolk && item.yolk !== '无'}}"> | {{item.yolk}}</text></view>
<view class="product-status-row" style="width: 325rpx; display: block; box-sizing: border-box">
<view class="product-spec" style="width: 325rpx; height: 68rpx; display: block; box-sizing: border-box">{{item.specification || '无'}}<text wx:if="{{item.yolk && item.yolk !== '无'}}"> | {{item.yolk}}</text></view>
<view class="product-status-row" style="width: 325rpx; display: block; box-sizing: border-box; height: 93rpx">
<view class="status-tag source-{{item.sourceType === '三方认证' ? 'third' : (item.sourceType === '平台货源' ? 'platform' : 'unverified')}}">{{item.sourceType || ''}}</view>
<view class="status-tag negotiate-{{item.negotiateStatus === '可议价' ? 'yes' : 'no'}}" style="width: 70rpx; display: inline-block; box-sizing: border-box">{{item.negotiateStatus}}</view>
<view class="status-tag item-count">库存:{{item.totalStock > 0 ? item.totalStock + '件' : '暂无'}}</view>
<view class="status-tag item-count">库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}}</view>
</view>
<view class="product-meta">
<text class="sales-count">已有{{item.reservedCount || 0}}人收藏</text>

Loading…
Cancel
Save