修改购物车逻辑

This commit is contained in:
2025-08-11 15:48:44 +08:00
parent 9c0be00fbd
commit ff5e4af2d4
3 changed files with 21 additions and 16 deletions

View File

@ -82,7 +82,7 @@
</view>
</view>
<shop-spec v-model="showSpec" :name="goods.name" :goods="goods" @close="closePopup" @confirm="confirmSpec" @buynow="onBuy"></shop-spec>
<shop-spec v-model="showSpec" :name="goods.name" :goods="goods" @confirm="confirmSpec" @buynow="onBuy"></shop-spec>
</view>
</template>
@ -113,11 +113,15 @@
source: '',
cartLists: [],
totalPrice: 0,
showPopup: false,
}
},
onLoad(options) {
console.log("🚀 ~ onLoad ~ options:", options)
this.id = options.id
this.showPopup = options.showPopup === 'true' ? true : false
this.showSpec = this.showPopup
},
onShow() {
@ -174,7 +178,6 @@
// 将当前的商品添加到购物车里面
addCartFun() {
this.source = 'cart'
this.showSpec = true
},
@ -188,7 +191,6 @@
console.log("data>>>", data.spec);
this.spec = data.spec
this.showSpec = false
this.source = ''
},
onChangeGoods(e) {
@ -202,16 +204,13 @@
this.showSpec = true;
},
closePopup() {
this.source = ''
},
// 购买商品
onBuy(e) {
if (this.source === 'cart') {
if (e.type === 'cart') {
// 限购逻辑:套餐和单品内的商品列表,每个都是只能购买两个
console.log("🚀 ~ onBuy ~ this.goods:", this.goods)
if (this.goods.first_category_id == 1 || this.goods.first_category_id == 2) {
console.log("🚀 ~ onBuy ~ this.cartLists:", this.cartLists)
const totalNum = this.cartLists
.filter(i => i.first_category_id === this.goods.first_category_id)
.reduce((sum, i) => sum + (i.goods_num || 0), 0);
@ -220,8 +219,9 @@
this.$toast({ title: '该类商品每人限购2件' });
return;
} else {
let {id} = e.detail
addCart({
item_id: this.goods.id,
item_id: id,
goods_num: 1
}).then(res => {
this.getCartListFun()