增加购物车商品数量变化时的库存检查
This commit is contained in:
1 parent
6546f12fe2
commit
ee7f4a6903
4 files changed
+68
-21
No files matched your search
@@ -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"
|
||||
|
||||
@@ -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<Props>(), {
|
||||
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 })
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
+33
-14
@@ -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")
|
||||
}
|
||||
|
||||
// 点击提交订单
|
||||
|
||||
Reference in new issue
Block a user