From d55fec346faebb9dd5d88f5c4f5ebd44c1d625ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Sat, 10 Jan 2026 10:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E6=8B=89=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E6=B8=85=E9=99=A4=E6=9C=AC=E5=9C=B0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=B9=B6=E9=87=8D=E6=96=B0=E5=8A=A0=E8=BD=BD=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.js | 71 +++++++++++++++++++++++++++++++----------- pages/index/index.json | 5 ++- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index 5c9aad5..387845d 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -436,8 +436,6 @@ Page({ return } - const existingGoods = [...this.data.goods] - this.setData({ isRefreshing: true, page: 1, @@ -481,7 +479,7 @@ Page({ this.setData({ isRefreshing: false }) if (res.success && res.products) { - this.processRefreshData(res.products, existingGoods) + this.processRefreshData(res.products) wx.showToast({ title: '刷新成功', icon: 'success', @@ -506,7 +504,7 @@ Page({ this.setData({ isRefreshing: false }); if (soldOutRes.success && soldOutRes.products && soldOutRes.products.length > 0) { - this.processRefreshData(soldOutRes.products, existingGoods); + this.processRefreshData(soldOutRes.products); wx.showToast({ title: '刷新成功', icon: 'success', @@ -797,7 +795,7 @@ Page({ }, // 处理刷新数据 - 查重并保持原有商品 - processRefreshData: function (newProducts, existingGoods) { + processRefreshData: function (newProducts) { let newGoods = newProducts.map(product => { const imageUrls = product.imageUrls || product.images || []; const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; @@ -813,6 +811,44 @@ Page({ }; }); + // 计算库存总数 - 支持逗号分隔的数字字符串 + 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 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; + + // 智能库存显示 - 库存>=10000显示"库存充足",库存=0显示"暂无",其他显示具体数字 + let displayStock; + if (totalStock >= 10000) { + // 库存>=10000时显示"库存充足" + displayStock = '充足'; + } else if (totalStock === 0) { + // 库存=0时显示"暂无" + displayStock = '暂无'; + } else { + // 其他情况显示具体数字 + displayStock = totalStock; + } + + // 格式化规格显示 + const formattedSpec = this.formatSpecification(product.specification || product.spec || '', product.yolk || ''); + return { ...product, id: productId, // 统一使用id字段 @@ -837,7 +873,11 @@ Page({ currentImageIndex: 0, imageUrls: formattedImageUrls, // 添加预处理后的媒体数据,包含类型信息 - mediaItems: mediaItems + mediaItems: mediaItems, + totalStock: displayStock, // 使用优化后的库存显示值 + originalTotalStock: totalStock, // 保留原始计算值用于调试 + displaySpecification: formattedSpec.displaySpec, // 格式化后的规格 + displayYolk: formattedSpec.displayYolk // 格式化后的蛋黄 } }) @@ -850,18 +890,11 @@ Page({ newGoodsMap.set(item.id, item); } }); - newGoods = Array.from(newGoodsMap.values()); - - // 移除与现有商品重复的新商品(包括广告和普通商品) - const existingIds = new Set(existingGoods.map(item => item.id)); - const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); - - // 先将现有商品和新商品合并,然后重新处理整个数据集 - // 这样可以确保所有商品都经过相同的排序规则处理 - const combinedGoods = [...existingGoods, ...uniqueNewGoods] + // 下拉刷新时完全使用新获取的商品数据,不再与旧数据合并 + const newGoodsOnly = Array.from(newGoodsMap.values()); - // 对合并后的完整数据进行过滤和排序 - const filteredGoods = this.applyFilters(combinedGoods, true) + // 对新数据进行过滤和排序 + const filteredGoods = this.applyFilters(newGoodsOnly, true) const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods); const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory; @@ -869,7 +902,7 @@ Page({ const cacheKey = `${currentCategory}_${currentKeyword}`; this.setData({ - goods: combinedGoods, + goods: newGoodsOnly, filteredGoods: filteredGoods, groupedGoods: groupedGoods, loadingMore: false, @@ -881,7 +914,7 @@ Page({ // 更新分类查询缓存 const newCategoryQueryCache = { ...this.data.categoryQueryCache }; - newCategoryQueryCache[cacheKey] = combinedGoods; + newCategoryQueryCache[cacheKey] = newGoodsOnly; this.setData({ categoryQueryCache: newCategoryQueryCache }) diff --git a/pages/index/index.json b/pages/index/index.json index aa3f1b0..23ea9ee 100644 --- a/pages/index/index.json +++ b/pages/index/index.json @@ -1,5 +1,8 @@ { "usingComponents": { "navigation-bar": "/components/navigation-bar/navigation-bar" - } + }, + "enablePullDownRefresh": true, + "backgroundTextStyle": "dark", + "backgroundColor": "#f8f8f8" } \ No newline at end of file