diff --git a/src/pages/shop/components/product-car.vue b/src/pages/shop/components/product-car.vue index 847f001..d75f2fb 100644 --- a/src/pages/shop/components/product-car.vue +++ b/src/pages/shop/components/product-car.vue @@ -13,6 +13,7 @@ v-for="v in carList" :key="v.id" :productId="v.productId" + :spuId="v.spuId" :merchantId="v.merchantId" :picUrl="filePath(v.productPic)" :productName="v.productName" diff --git a/src/pages/shop/components/product-row.vue b/src/pages/shop/components/product-row.vue index a29f49d..17866d2 100644 --- a/src/pages/shop/components/product-row.vue +++ b/src/pages/shop/components/product-row.vue @@ -55,6 +55,7 @@ const emit = defineEmits(["onClickAddToCar", "add", "remove"]) export type Props = { mode?: "default" | "car" productId: string + spuId?: string merchantId: string picUrl: string productName: string @@ -69,6 +70,7 @@ export type Props = { const props = withDefaults(defineProps(), { mode: "default", productId: "", + spuId: "", merchantId: "", picUrl: "", productName: "", @@ -90,14 +92,16 @@ const add = () => { if (props.productCount + 1 >= 99) { return } - emit("add", { productId: props.productId, productCount: props.productCount + 1 }) + // emit("add", { productId: props.productId, productCount: props.productCount + 1 }) + emit("add", { productId: props.productId, productCount: props.productCount, spuId: props.spuId }) } const remove = () => { if (props.productCount - 1 < 0) { return } - emit("remove", { productId: props.productId, productCount: props.productCount - 1 }) + // emit("remove", { productId: props.productId, productCount: props.productCount - 1 }) + emit("remove", { productId: props.productId, productCount: props.productCount, spuId: props.spuId }) } diff --git a/src/pages/shop/components/product-sku.vue b/src/pages/shop/components/product-sku.vue index db6885d..b990164 100644 --- a/src/pages/shop/components/product-sku.vue +++ b/src/pages/shop/components/product-sku.vue @@ -191,12 +191,14 @@ function onSelectSku(skuItem: any) { // changeDisabled(isChecked, propertyId, valueId); } +// 加数量 const handleNumAdd = () => { if (state.goodsCount >= state.goodsDetail.stock) { return } state.goodsCount += 1 } +// 减数量 const handleNumRemove = () => { if (state.goodsCount <= 1) { return @@ -205,18 +207,39 @@ const handleNumRemove = () => { } // 点击加入购物车 -const handleClickAddToCar = () => { - if (state.goodsDetail.stock === 0) { +const handleClickAddToCar = async () => { + // 检查规格选择 + if (!state.selectedSku.id) { uni.showToast({ - title: "库存不足", + title: "请选择规格", icon: "none", duration: 1000, }) return } - if (!state.selectedSku.id) { + // 检查库存 + try { + uni.showLoading({ title: "加载中", mask: true }) + const { code }: any = await shopApi.checkSpuStock({ + spuId: state.goodsDetail.id, + incrCount: state.goodsCount, + }) + if (code != 0) { + return uni.showToast({ + title: "库存不足", + icon: "none", + duration: 1000, + }) + } + } catch (e) { + console.error(e) + } finally { + uni.hideLoading() + } + // 检查库存 + if (state.goodsDetail.stock === 0) { uni.showToast({ - title: "请选择规格", + title: "库存不足", icon: "none", duration: 1000, }) diff --git a/src/pages/shop/detail.vue b/src/pages/shop/detail.vue index b715463..26ccaf9 100644 --- a/src/pages/shop/detail.vue +++ b/src/pages/shop/detail.vue @@ -237,19 +237,38 @@ const onAddToCar = (cartGoodsInfo: any) => { showSkuDialog.value = false } // 同步购物车中的数据 -const chentProductCount = async (item: any) => { - const target = state.carList.find((v) => v.productId == item.productId) - const targetIndex = state.carList.findIndex((v) => v.productId == item.productId) - if (target) { - state.carList[targetIndex].productCount = item.productCount - state.carList[targetIndex].productId = item.productId - console.log("1", state.carList[targetIndex]) - } else { - state.carList.push(item) - console.log("2", item) - } +const chentProductCount = async (item: any, type?: "add" | "remove") => { + try { + console.log("ccccccc", item) - nextTick(() => updateShopCar()) // 保存购物车数据 + uni.showLoading({ title: "加载中", mask: true }) + let count = item.productCount + if (type === "add") { + count += 1 + const { code }: any = await shopApi.checkSpuStock({ spuId: item.spuId, incrCount: count }) + if (code != 0) throw new Error() + } else if (type === "remove") { + count -= 1 + } + + const target = state.carList.find((v) => v.productId == item.productId) + const targetIndex = state.carList.findIndex((v) => v.productId == item.productId) + if (target) { + state.carList[targetIndex].productCount = count + state.carList[targetIndex].productId = item.productId + state.carList[targetIndex].spuId = item.spuId + console.log("1", state.carList[targetIndex]) + } else { + state.carList.push(item) + console.log("2", item) + } + + nextTick(async () => await updateShopCar()) // 保存购物车数据 + } catch (e) { + console.error(e) + } finally { + uni.hideLoading() + } } // 保存购物车数据 const updateShopCar = async () => { @@ -279,10 +298,10 @@ const doVisibleCar = async () => { } const doAdd = async (item: any) => { - chentProductCount(item) + chentProductCount(item, "add") } const doRemove = async (item: any) => { - chentProductCount(item) + chentProductCount(item, "remove") } // 点击提交订单