修改购物车逻辑

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

@ -256,7 +256,10 @@
// 添加到购物车(默认商品数量+1,可以让后端连表查询商品数量字段) // 添加到购物车(默认商品数量+1,可以让后端连表查询商品数量字段)
async addCartFun(item) { async addCartFun(item) {
console.log("🚀 ~ addCartFun ~ item:", item) uni.navigateTo({
url: `/pages/shop/shop?id=${item.id}&showPopup=true`
})
return
// 限购逻辑 // 限购逻辑
if (item.first_category_id === 1 || item.first_category_id === 2) { if (item.first_category_id === 1 || item.first_category_id === 2) {
@ -346,7 +349,7 @@
// 去结算 // 去结算
goSettle() { goSettle() {
console.log(this.cartLists) console.log(this.cartLists)
this.showCart = false this.showCart = false
const goods = this.cartLists.map(item => { const goods = this.cartLists.map(item => {
return { return {

View File

@ -36,10 +36,11 @@
<view class="row u-row-center mt20 u-p-b-20" :class="specValueText.indexOf('请选择') != -1 || checkedGoods.stock == 0 ? 'disabled' : ''"> <view class="row u-row-center mt20 u-p-b-20" :class="specValueText.indexOf('请选择') != -1 || checkedGoods.stock == 0 ? 'disabled' : ''">
<view class="w-40 mr10"> <view class="w-40 mr10">
<u-button @click="close" hover-class="none" :customStyle="{color: themeColor, border: '1px solid ' + themeColor, padding: '16rpx 0'}" :plain="true" :hair-line="false" shape="circle">取消</u-button> <!-- <u-button @click="close" hover-class="none" :customStyle="{color: themeColor, border: '1px solid ' + themeColor, padding: '16rpx 0'}" :plain="true" :hair-line="false" shape="circle">取消</u-button> -->
<u-button @click="confirm('cart')" hover-class="none" :customStyle="{color: themeColor, border: '1px solid ' + themeColor, padding: '16rpx 0'}" :plain="true" :hair-line="false" shape="circle">加入购物车</u-button>
</view> </view>
<view class="w-40 ml10"> <view class="w-40 ml10">
<u-button @click="confirm" hover-class="none" :customStyle="{backgroundColor: themeColor, color: '#fff', border: 'none', padding: '16rpx 0'}" :hair-line="false" shape="circle">确定</u-button> <u-button @click="confirm('buy')" hover-class="none" :customStyle="{backgroundColor: themeColor, color: '#fff', border: 'none', padding: '16rpx 0'}" :hair-line="false" shape="circle">确定</u-button>
</view> </view>
</view> </view>
</view> </view>
@ -198,7 +199,7 @@
// }, // },
// 确认选择的规格 // 确认选择的规格
confirm() { confirm(type) {
let { checkedGoods, goodsNum } = this; let { checkedGoods, goodsNum } = this;
if (this.specValueText.indexOf("请选择") != -1) if (this.specValueText.indexOf("请选择") != -1)
return this.$toast({ return this.$toast({
@ -211,6 +212,7 @@
checkedGoods.goodsNum = goodsNum; checkedGoods.goodsNum = goodsNum;
this.$emit('buynow', { this.$emit('buynow', {
detail: checkedGoods, detail: checkedGoods,
type
}); });
} }
}, },

View File

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