From 1dbb125acbed88e6cb4d5de99ffcb6948b10a1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=89=E9=B9=8F?= Date: Sun, 7 Jun 2020 13:31:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6bug=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/cart/cart.vue | 99 +++++++++++++++++------------------------------ src/store/modules/cart.js | 6 ++- 2 files changed, 41 insertions(+), 64 deletions(-) diff --git a/src/pages/cart/cart.vue b/src/pages/cart/cart.vue index 084cf60..4d82a33 100644 --- a/src/pages/cart/cart.vue +++ b/src/pages/cart/cart.vue @@ -25,8 +25,8 @@ @longpress="delCart(item.cart_id,index)" > @@ -110,84 +110,57 @@ export default { sk_id:String, mp_id:String, isShowBottom : false, //底部弹窗开关 - // totalPrice: 0, - pIsoPen: Boolean, - childIsOpenArr:[], cart_id:Number, maxCount: 20, - cartIndex:Number + cartIndex:Number, + cartList:[] } }, computed: { - + pIsoPen (){ + if (this.cartList.length > 0){ + return this.cartList.find(item => !item.isChecked) ? false : true; + } + return false + }, goodInfo () { return this.$store.state.read.goodInfo }, - cartList() { - return this.$store.state.cart.cartList - }, - totalPrice() { - const itemPriceArr = [] - this.cartList.map(item =>{ - itemPriceArr.push(item.num*item.nowPrice) + totalPrice() { + let totalPrice = 0 + this.cartList.forEach((item)=>{ + if(item.isChecked){ + totalPrice += item.nowPrice * item.num; + } }) - let total = 0 - if(this.pIsoPen){ - itemPriceArr.map(item =>{ - total += item - }) - }else{ - this.childIsOpenArr.map((item,index) =>{ - if(item){ - total += itemPriceArr[index] - } - }) - } - return total - }, - - }, - onShow() { + return totalPrice + } }, - onLoad: function() { - //全选置否 - this.pIsoPen = false - store.dispatch('cart/getCartList', { + onLoad: async function() { + await this.$store.dispatch('cart/getCartList', { uid: this.$store.state.user.userInfo.uid, // 用户id - }) - .then((res)=>{ - //单选置否 - const temp = [] - temp.length = this.$store.state.cart.cartList.length - for (let i = 0; i < temp.length; i++) { - temp[i] = false - } - this.childIsOpenArr = temp + }) + + this.cartList = this.$store.state.cart.cartList; + this.cartList.forEach((item)=>{ + item.isChecked = false }) }, methods: { //全选按钮 - pClick(){ - if(this.pIsoPen){ - this.pIsoPen = false - this.childIsOpenArr.map((item,index)=> { - this.childIsOpenArr[index] = false - }) - }else{ - this.pIsoPen = true - this.childIsOpenArr.map((item,index)=> { - this.childIsOpenArr[index] = true - }) - } + pClick(){ + let pStatus = this.cartList.find(item => !item.isChecked) ? false : true + let oldList = this.cartList; + oldList.forEach((item, index)=>{ + item.isChecked = !pStatus + this.cartList.splice(index,1, item) + }) }, //单选按钮 - childClick(index){ - this.childIsOpenArr[index] = !this.childIsOpenArr[index] - if(this.childIsOpenArr.find(item => item == false)==undefined){ - this.pIsoPen = true - }else{ - this.pIsoPen = false - } + childClick(type,index){ + this.cartList[index].isChecked = !this.cartList[index].isChecked + //vue没有办法监听数组内部值的变化,所以需要通过这个方法去触发 + this.cartList.splice(index,1, this.cartList[index]) }, //修改购物车 chooseCartModi(mp_id,sk_id,price,pid,num,cart_id,index){ diff --git a/src/store/modules/cart.js b/src/store/modules/cart.js index 4644c82..5667687 100644 --- a/src/store/modules/cart.js +++ b/src/store/modules/cart.js @@ -30,7 +30,11 @@ const actions = { url: cartList, data: param, success: (res) => { - console.log('cart===>接口数据', res.data.data) + let test = { + isChecked: false, + itemNum:1, + price:0 + } commit('INIT', res.data.data) resolve(res.data.data) }, -- 2.0.0