diff --git a/packages/shop/goods/index.vue b/packages/shop/goods/index.vue index da5955ee..3907f099 100644 --- a/packages/shop/goods/index.vue +++ b/packages/shop/goods/index.vue @@ -515,20 +515,35 @@ export default { addCar() { let that = this; + // 获取当前商品 + const currentGoods = this.info.commodity_goods_info_list[this.currentGGIndex]; + + // 获取当前购物车数量和库存数量 + const currentQuantity = currentGoods.cart_count ? currentGoods.cart_count.count : 0; + const stockQuantity = currentGoods.stock_quantity || 0; + + // 检查库存是否充足 + if (currentQuantity >= stockQuantity) { + uni.showToast({ + title: "库存不足", + icon: "none", + duration: 2000 + }); + return; + } + //如果没有当前商品 直接添加一个 let goods_id_and_count = []; this.info.commodity_goods_info_list[this.currentGGIndex].cart_count = { - count: 1, + count: currentQuantity + 1, }; this.info.commodity_goods_info_list.forEach((item) => { - console.log(item.cart_count); goods_id_and_count.push({ goods_id: item.id, count: item.cart_count ? item.cart_count.count : 0, }); }); - console.log(goods_id_and_count); //因为是当前商品没有 调用update就是增加商品。增加商品之后再获取购物车数量 request(apiArr.updateCar, "POST", { goods_id_and_count, diff --git a/packages/shop/groupPurchaseSubmit/index.vue b/packages/shop/groupPurchaseSubmit/index.vue index 442c2b4a..3c6dcbd9 100644 --- a/packages/shop/groupPurchaseSubmit/index.vue +++ b/packages/shop/groupPurchaseSubmit/index.vue @@ -168,8 +168,8 @@ - 运费 ¥{{ - item.commodity_goods_info.freight }} + - @@ -420,14 +420,24 @@ export default { } }, increaseQuantity(item) { + console.log("🚀 ~ increaseQuantity ~ item:", item) const currentTime = new Date().getTime(); const startTime = new Date(item.commodity_goods_info.group_buy_activity_info?.start_time).getTime(); const endTime = new Date(item.commodity_goods_info.group_buy_activity_info?.end_time).getTime(); + + if (item.count >= item.commodity_goods_info.stock_quantity) { + uni.showToast({ + title: '库存不足', + icon: 'none' + }); + return + } + if (currentTime >= startTime && currentTime <= endTime) { if (item.count == 0) { item.count += item.commodity_goods_info.min_order_quantity } else { - if (item.count == item.commodity_goods_info.total_stock) { + if (item.count >= item.commodity_goods_info.stock_quantity) { uni.showToast({ title: '库存不足', icon: 'none' @@ -474,9 +484,11 @@ export default { const startTime = new Date(goods.commodity_goods_info.group_buy_activity_info?.start_time).getTime(); const endTime = new Date(goods.commodity_goods_info.group_buy_activity_info?.end_time).getTime(); if (currentTime >= startTime && currentTime <= endTime) { - total += goods.commodity_goods_info.group_buy_price * goods.count + goods.commodity_goods_info.freight; + // total += goods.commodity_goods_info.group_buy_price * goods.count + goods.commodity_goods_info.freight; + total += goods.commodity_goods_info.group_buy_price * goods.count; } else { - total += goods.commodity_goods_info.sales_price * goods.count + goods.commodity_goods_info.freight; + // total += goods.commodity_goods_info.sales_price * goods.count + goods.commodity_goods_info.freight; + total += goods.commodity_goods_info.sales_price * goods.count; } }); // 加运费 @@ -488,9 +500,11 @@ export default { const startTime = new Date(goods.commodity_goods_info.group_buy_activity_info?.start_time).getTime(); const endTime = new Date(goods.commodity_goods_info.group_buy_activity_info?.end_time).getTime(); if (currentTime >= startTime && currentTime <= endTime) { - total += goods.commodity_goods_info.group_buy_price * goods.count + goods.commodity_goods_info.freight; + // total += goods.commodity_goods_info.group_buy_price * goods.count + goods.commodity_goods_info.freight; + total += goods.commodity_goods_info.group_buy_price * goods.count; } else { - total += goods.commodity_goods_info.sales_price * goods.count + goods.commodity_goods_info.freight; + // total += goods.commodity_goods_info.sales_price * goods.count + goods.commodity_goods_info.freight; + total += goods.commodity_goods_info.sales_price * goods.count; } }); // 加运费 diff --git a/packages/shop/index/index.vue b/packages/shop/index/index.vue index 063e37b8..9d31b27a 100644 --- a/packages/shop/index/index.vue +++ b/packages/shop/index/index.vue @@ -437,26 +437,42 @@ 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 ) { 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 > currentQuantity && currentQuantity >= stockQuantity) { + uni.showToast({ + title: "库存不足", + icon: 'none' + }); + return; + } + + // 使用$set确保响应式更新 + this.$set(goodsToUpdate, 'quantity', quantity); const params = { user_id: uni.getStorageSync("userId"), diff --git a/packages/shop/shopCar/index.vue b/packages/shop/shopCar/index.vue index 8051dfe6..9e6ca891 100644 --- a/packages/shop/shopCar/index.vue +++ b/packages/shop/shopCar/index.vue @@ -617,6 +617,13 @@ export default { this.shopCarTotal++; } } else { + if (carItem.count >= carItem.commodity_goods_info.stock_quantity) { + uni.showToast({ + title: '库存不足', + icon: 'none' + }); + return + } carItem.count++; this.shopCarTotal++; }