From 7d03d464a9ae96d0b9eba41a0a43a38171af3a29 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: Mon, 26 Jan 2026 16:20:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A6=96=E9=A1=B5=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E8=AE=A1=E7=AE=97=EF=BC=8C=E6=8E=92=E9=99=A4=E4=B8=8B?= =?UTF-8?q?=E6=9E=B6=E8=A7=84=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.js | 82 +++++++++++++++++++++++++--------- server-example/server-mysql.js | 3 +- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index 689ed7c..f3c4868 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -1014,23 +1014,44 @@ Page({ }; }); - // 计算库存总数 - 支持逗号分隔的数字字符串 - const calculateTotalStock = (value) => { + // 计算库存总数 - 支持逗号分隔的数字字符串,同时考虑规格状态 + const calculateTotalStock = (value, specStatusString) => { 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); + const quantityArray = value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0); + + // 检查是否有规格状态字符串 + if (specStatusString && typeof specStatusString === 'string') { + const specStatusArray = specStatusString.split(/[,,、]/).map(item => item.trim()); + + // 只计算未下架的规格库存(specStatus !== '1') + // 确保规格状态数组与数量数组长度匹配 + return quantityArray.reduce((sum, num, index) => { + // 如果规格状态数组长度不足,默认为未下架状态 + const specStatus = index < specStatusArray.length ? specStatusArray[index] : '0'; + if (specStatus !== '1') { + return sum + num; + } + return sum; + }, 0); + } + + // 如果没有规格状态,计算所有库存 + return quantityArray.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 specStatusString = product.specStatusString || product.specStatus || product.statusString || product.spec_status || ''; + const minOrder = calculateTotalStock(product.minOrder, specStatusString); + const quantity = calculateTotalStock(product.quantity, specStatusString); + const stock = calculateTotalStock(product.stock, specStatusString); + const inventory = calculateTotalStock(product.inventory, specStatusString); + const availableStock = calculateTotalStock(product.availableStock, specStatusString); + const totalAvailable = calculateTotalStock(product.totalAvailable, specStatusString); // 优先使用最具体的库存字段 const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; @@ -1199,23 +1220,44 @@ Page({ }; }); - // 计算库存总数 - 支持逗号分隔的数字字符串 - const calculateTotalStock = (value) => { + // 计算库存总数 - 支持逗号分隔的数字字符串,同时考虑规格状态 + const calculateTotalStock = (value, specStatusString) => { 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); + const quantityArray = value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0); + + // 检查是否有规格状态字符串 + if (specStatusString && typeof specStatusString === 'string') { + const specStatusArray = specStatusString.split(/[,,、]/).map(item => item.trim()); + + // 只计算未下架的规格库存(specStatus !== '1') + // 确保规格状态数组与数量数组长度匹配 + return quantityArray.reduce((sum, num, index) => { + // 如果规格状态数组长度不足,默认为未下架状态 + const specStatus = index < specStatusArray.length ? specStatusArray[index] : '0'; + if (specStatus !== '1') { + return sum + num; + } + return sum; + }, 0); + } + + // 如果没有规格状态,计算所有库存 + return quantityArray.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 specStatusString = product.specStatusString || product.specStatus || product.statusString || product.spec_status || ''; + const minOrder = calculateTotalStock(product.minOrder, specStatusString); + const quantity = calculateTotalStock(product.quantity, specStatusString); + const stock = calculateTotalStock(product.stock, specStatusString); + const inventory = calculateTotalStock(product.inventory, specStatusString); + const availableStock = calculateTotalStock(product.availableStock, specStatusString); + const totalAvailable = calculateTotalStock(product.totalAvailable, specStatusString); // 优先使用最具体的库存字段 const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index d708505..61ecd3e 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -2261,7 +2261,8 @@ app.post('/api/product/list', async (req, res) => { 'category', 'producting', 'description', - 'frequency' + 'frequency', + 'spec_status' ], order: [['created_at', 'DESC']], limit: pageSize,