Browse Source

在首页添加库存总数显示功能,计算并展示所有商品的库存总数

pull/3/head
徐飞洋 2 months ago
parent
commit
33094e51ce
  1. 28
      pages/index/index.js
  2. 4
      pages/index/index.wxml
  3. 16
      pages/index/index.wxss

28
pages/index/index.js

@ -47,6 +47,9 @@ Page({
page: 1,
pageSize: 8,
// 库存总数
totalInventory: 0,
// 商品数据缓存
goodsCache: [],
categoryQueryCache: {},
@ -416,6 +419,19 @@ Page({
}
return "";
},
// 计算商品库存总数
calculateTotalInventory: function(goodsList) {
let total = 0;
goodsList.forEach(good => {
if (good.minOrder && !isNaN(parseInt(good.minOrder))) {
total += parseInt(good.minOrder);
} else if (good.quantity && !isNaN(parseInt(good.quantity))) {
total += parseInt(good.quantity);
}
});
return total;
},
// 提取地区中的省份信息
extractProvince: function(region) {
@ -541,12 +557,16 @@ Page({
console.log('filteredGoods[1].category:', filteredGoods[1] ? filteredGoods[1].category : 'N/A')
}
// 计算库存总数
const totalInventory = this.calculateTotalInventory(updatedGoods);
this.setData({
goods: updatedGoods,
filteredGoods: filteredGoods,
loadingMore: false,
isLoading: false,
page: this.data.page + 1
page: this.data.page + 1,
totalInventory: totalInventory
})
},
@ -605,12 +625,16 @@ Page({
const filteredGoods = this.applyFilters(updatedGoods, false)
// 计算库存总数
const totalInventory = this.calculateTotalInventory(updatedGoods);
this.setData({
goods: updatedGoods,
filteredGoods: filteredGoods,
loadingMore: false,
isLoading: false,
page: this.data.page + 1
page: this.data.page + 1,
totalInventory: totalInventory
})
},

4
pages/index/index.wxml

@ -120,6 +120,10 @@
<!-- 商品列表区域 -->
<view class="goods-section">
<!-- 库存总数显示 -->
<view wx:if="{{totalInventory > 0}}" class="total-inventory-section">
<text class="total-inventory-text">库存总数:{{totalInventory}}件</text>
</view>
<scroll-view
class="goods-list"
id="goodsScrollView"

16
pages/index/index.wxss

@ -148,7 +148,21 @@ page {
padding-bottom: calc(env(safe-area-inset-bottom) );
}
.goods-list {
/* 库存总数显示样式 */
.total-inventory-section {
background: #fff;
padding: 20rpx 30rpx;
border-bottom: 1rpx solid #eee;
text-align: center;
}
.total-inventory-text {
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.goods-list {
height: auto;
box-sizing: border-box;
background: #f5f5f5;

Loading…
Cancel
Save