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,