From 9be3eecd0f0041483e14f8a47dab2d046cc98829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=AF=85?= <1335909236@qq.com> Date: Thu, 9 Oct 2025 15:44:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=93=E8=B4=AD=E5=95=86=E5=93=81=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E9=83=A8=E5=88=86=E6=B7=BB=E5=8A=A0=E5=BA=93=E5=AD=98?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/shop/search/index.vue | 64 ++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/packages/shop/search/index.vue b/packages/shop/search/index.vue index c06be7eb..68840013 100644 --- a/packages/shop/search/index.vue +++ b/packages/shop/search/index.vue @@ -78,7 +78,7 @@ {{ getPriceRange(item.commodity_goods_info_list) }} - @@ -128,7 +128,7 @@ {{ ite.sales_price }} - @@ -293,25 +293,49 @@ export default { }, // 处理商品数量变化 handleQuantityChange(val, item) { - // 先在前端直接更新数量,确保页面显示及时变化 - // 注意:这里的val可能是直接的数值,也可能是包含value属性的对象 const quantity = typeof val === 'object' && val !== null && 'value' in val ? val.value : val; - + + // 检查库存数量 + let currentQuantity = 0; + let stockQuantity = 0; + let goodsToUpdate = null; + // 对于有规格的主商品(绑定到items.commodity_goods_info_list[0].quantity) - if (item.commodity_goods_info_list && item.commodity_goods_info_list.length) { + if ( + item.commodity_goods_info_list && + item.commodity_goods_info_list.length + ) { this.goodsId = item.commodity_goods_info_list[0].id; - // 使用$set确保响应式更新 - this.$set(item.commodity_goods_info_list[0], 'quantity', quantity); + currentQuantity = item.commodity_goods_info_list[0].quantity || 0; + stockQuantity = item.commodity_goods_info_list[0].stock_quantity || 0; + goodsToUpdate = item.commodity_goods_info_list[0]; } // 对于规格列表中的商品(绑定到ite.quantity) else { this.goodsId = item.id; - // 使用$set确保响应式更新 - this.$set(item, 'quantity', quantity); + currentQuantity = item.quantity || 0; + stockQuantity = item.stock_quantity || 0; + goodsToUpdate = item; } - + + // 直接检查新数量是否超过库存,如果超过则不允许修改 + if (quantity > stockQuantity) { + uni.showToast({ + title: "库存不足", + icon: 'none' + }); + // 强制重置数量为当前值或库存值,确保UI上显示的数量不会超过库存 + this.$nextTick(() => { + this.$set(goodsToUpdate, 'quantity', Math.min(currentQuantity, stockQuantity)); + }); + return; + } + + // 使用$set确保响应式更新 + this.$set(goodsToUpdate, 'quantity', quantity); + const params = { - user_id: uni.getStorageSync('userId'), + user_id: uni.getStorageSync("userId"), goods_id_and_count: [ { goods_id: this.goodsId, @@ -319,24 +343,26 @@ export default { }, ], }; - - request(apiArr.updateCar, 'POST', params).then((res) => { + + // 发送请求更新后端数据 + request(apiArr.updateCar, "POST", params).then((res) => { console.log(res); - // 更新购物车数据 + // 先更新购物车数据 this.getShopCarList(); - - // 延迟时间,确保goodsDetail已经更新 + + // 延迟一小段时间,确保goodsDetail已经更新 setTimeout(() => { // 重新同步商品列表中的数量 this.syncGoodsQuantities(); }, 100); - + uni.showToast({ - title: '操作成功!', + title: "操作成功!", success() { }, }); }); }, + // 同步商品列表中的数量与购物车数据 syncGoodsQuantities() { // 遍历所有商品,同步数量