易购商品搜索部分添加库存校验

This commit is contained in:
赵毅 2025-10-09 15:44:37 +08:00
parent 41c389743e
commit 9be3eecd0f

View File

@ -78,7 +78,7 @@
{{ getPriceRange(item.commodity_goods_info_list) }} {{ getPriceRange(item.commodity_goods_info_list) }}
</view> </view>
<view class="CateInfo_Item_Money_right" v-if="!(item.commodity_goods_info_list.length > 1)"> <view class="CateInfo_Item_Money_right" v-if="!(item.commodity_goods_info_list.length > 1)">
<u-number-box :min="0" v-model="item.commodity_goods_info_list[0].quantity" <u-number-box :value="item.commodity_goods_info_list[0].quantity || 0" :min="0"
@change="(value) => handleQuantityChange(value, item)"> @change="(value) => handleQuantityChange(value, item)">
<view slot="minus" class="minus"> <view slot="minus" class="minus">
<u-icon name="minus" size="20"></u-icon> <u-icon name="minus" size="20"></u-icon>
@ -128,7 +128,7 @@
<span>¥</span>{{ ite.sales_price }} <span>¥</span>{{ ite.sales_price }}
</view> </view>
<view class="GGItem_Con_Msg_right"> <view class="GGItem_Con_Msg_right">
<u-number-box v-model="ite.quantity" :min="0" <u-number-box :value="ite.quantity || 0" :min="0"
@change="(value) => handleQuantityChange(value, ite)"> @change="(value) => handleQuantityChange(value, ite)">
<view slot="minus" class="minus"> <view slot="minus" class="minus">
<u-icon name="minus" size="20"></u-icon> <u-icon name="minus" size="20"></u-icon>
@ -293,25 +293,49 @@ export default {
}, },
// 处理商品数量变化 // 处理商品数量变化
handleQuantityChange(val, item) { handleQuantityChange(val, item) {
// 先在前端直接更新数量,确保页面显示及时变化
// 注意:这里的val可能是直接的数值,也可能是包含value属性的对象
const quantity = typeof val === 'object' && val !== null && 'value' in val ? val.value : val; 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) // 对于有规格的主商品(绑定到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; this.goodsId = item.commodity_goods_info_list[0].id;
// 使用$set确保响应式更新 currentQuantity = item.commodity_goods_info_list[0].quantity || 0;
this.$set(item.commodity_goods_info_list[0], 'quantity', quantity); stockQuantity = item.commodity_goods_info_list[0].stock_quantity || 0;
goodsToUpdate = item.commodity_goods_info_list[0];
} }
// 对于规格列表中的商品(绑定到ite.quantity) // 对于规格列表中的商品(绑定到ite.quantity)
else { else {
this.goodsId = item.id; this.goodsId = item.id;
// 使用$set确保响应式更新 currentQuantity = item.quantity || 0;
this.$set(item, 'quantity', quantity); 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 = { const params = {
user_id: uni.getStorageSync('userId'), user_id: uni.getStorageSync("userId"),
goods_id_and_count: [ goods_id_and_count: [
{ {
goods_id: this.goodsId, goods_id: this.goodsId,
@ -320,23 +344,25 @@ export default {
], ],
}; };
request(apiArr.updateCar, 'POST', params).then((res) => { // 发送请求更新后端数据
request(apiArr.updateCar, "POST", params).then((res) => {
console.log(res); console.log(res);
// 更新购物车数据 // 先更新购物车数据
this.getShopCarList(); this.getShopCarList();
// 延迟时间,确保goodsDetail已经更新 // 延迟一小段时间,确保goodsDetail已经更新
setTimeout(() => { setTimeout(() => {
// 重新同步商品列表中的数量 // 重新同步商品列表中的数量
this.syncGoodsQuantities(); this.syncGoodsQuantities();
}, 100); }, 100);
uni.showToast({ uni.showToast({
title: '操作成功!', title: "操作成功!",
success() { }, success() { },
}); });
}); });
}, },
// 同步商品列表中的数量与购物车数据 // 同步商品列表中的数量与购物车数据
syncGoodsQuantities() { syncGoodsQuantities() {
// 遍历所有商品,同步数量 // 遍历所有商品,同步数量