diff --git a/packages/shop/goods/index copy.vue b/packages/shop/goods/index copy.vue new file mode 100644 index 00000000..208997d4 --- /dev/null +++ b/packages/shop/goods/index copy.vue @@ -0,0 +1,403 @@ + + + + + \ No newline at end of file diff --git a/packages/shop/goods/index.vue b/packages/shop/goods/index.vue index 208997d4..30d41fac 100644 --- a/packages/shop/goods/index.vue +++ b/packages/shop/goods/index.vue @@ -266,15 +266,31 @@ export default { id: this.id }).then(res => { console.log(res); - this.info = res + // 分割图片 res.commodity_goods_info_list.forEach(item => { item.goods_detail_pic = item.goods_detail_pic.split(',') item.goods_carousel = item.goods_carousel.split(',') item.commodity_pic = item.commodity_pic.split(',') }); + + + + //根据购物车列表 和 当前商品列表 来更新当前商品列表在购物车中的数量 + if (this.carOrderList) { + this.carOrderList.forEach(items => { + res.commodity_goods_info_list.forEach(item => { + if (items.goods_id == item.id) { + item.cart_count = {count:0} + item.cart_count.count = items.count + } + }) + }) + } + this.info = res this.currentGG = res.commodity_goods_info_list[0] this.currentGGIndex = 0 + }) }, @@ -314,7 +330,7 @@ export default { }) }, - //离开页面的时候直接更新购物车数量(目前接口是 传入商品id 数量。如果有当前商品就更新数量。如果没有就增加商品) + getShopCarList() { request(apiArr.getCar, 'POST', {}).then(res => { this.carOrderList = res.commodity_cart_list @@ -327,12 +343,14 @@ export default { let goods_id_and_count = [] this.info.commodity_goods_info_list[this.currentGGIndex].cart_count = { count: 1 } this.info.commodity_goods_info_list.forEach(item => { - console.log(item); + console.log(item.cart_count); goods_id_and_count.push({ goods_id: item.id, - count: 1 + count: item.cart_count ? item.cart_count.count : 0 }) }) + + console.log(goods_id_and_count); //因为是当前商品没有 调用update就是增加商品。增加商品之后再获取购物车数量 request(apiArr.updateCar, "POST", { goods_id_and_count @@ -347,14 +365,14 @@ export default { // 修改当前商品在购物车中的数量 this.info.commodity_goods_info_list[this.currentGGIndex].cart_count.count = newValue.value // 修改购物车列表中的数量 - this.carOrderList.forEach(item=>{ - if(item.commodity_goods_info.id == this.info.commodity_goods_info_list[this.currentGGIndex].id){ + this.carOrderList.forEach(item => { + if (item.commodity_goods_info.id == this.info.commodity_goods_info_list[this.currentGGIndex].id) { item.count = newValue.value } }) // 计算购物车数量 let carNum = 0 - this.carOrderList.forEach(item=>{ + this.carOrderList.forEach(item => { carNum += item.count }) this.carNum = carNum @@ -374,17 +392,18 @@ export default { }, onShow() { + this.getShopCarList() this.getGoodsInfo() this.getShopCar() - this.getShopCarList() }, - onHide() { + //离开页面的时候直接更新购物车数量(目前接口是 传入商品id 数量。如果有当前商品就更新数量。如果没有就增加商品。如果删除某个商品 count 为0 就删除) + onHide() { let goods_id_and_count = [] this.info.commodity_goods_info_list.forEach(item => { goods_id_and_count.push({ goods_id: item.id, - count: item.cart_count.count + count: item.cart_count ? item.cart_count.count : 0 }) })