Commit 15af636b407efe77a91634b3b7e4d90bfb4fbc85
1 parent
e9b87ccef5
Exists in
master
购物车逻辑
Showing
2 changed files
with
72 additions
and
81 deletions
Show diff stats
src/components/CommodityCard/CommodityCard.vue
src/pages/cart/cart.vue
... | ... | @@ -8,6 +8,7 @@ |
8 | 8 | <view class="cardHeader"> |
9 | 9 | <view |
10 | 10 | v-bind:class="pIsoPen? 'partentChecked' : 'partentCheck'" |
11 | + @click="pClick" | |
11 | 12 | > |
12 | 13 | <span class="correct"></span> |
13 | 14 | </view> |
... | ... | @@ -24,7 +25,8 @@ |
24 | 25 | @longpress="delCart(item.cart_id,index)" |
25 | 26 | > |
26 | 27 | <view |
27 | - v-bind:class="childIsOpen[index]? 'partentChecked':'partentCheck'" | |
28 | + v-bind:class="childIsOpenArr[index]? 'partentChecked':'partentCheck'" | |
29 | + @click="childClick(childIsOpenArr[index],index)" | |
28 | 30 | > |
29 | 31 | <span class="correct"></span> |
30 | 32 | </view> |
... | ... | @@ -108,46 +110,89 @@ export default { |
108 | 110 | sk_id:String, |
109 | 111 | mp_id:String, |
110 | 112 | isShowBottom : false, //底部弹窗开关 |
111 | - totalPrice: 0, | |
112 | - pIsoPen: false, | |
113 | - // childIsOpen:[], | |
113 | + // totalPrice: 0, | |
114 | + pIsoPen: Boolean, | |
115 | + childIsOpenArr:[], | |
114 | 116 | cart_id:Number, |
115 | 117 | maxCount: 20, |
116 | 118 | cartIndex:Number |
117 | 119 | } |
118 | 120 | }, |
119 | 121 | computed: { |
120 | - // skInfo(){ | |
121 | - // return filters() | |
122 | - // } | |
122 | + | |
123 | 123 | goodInfo () { |
124 | - // console.log(this.$store.state.read.goodInfo) | |
125 | 124 | return this.$store.state.read.goodInfo |
126 | 125 | }, |
127 | 126 | cartList() { |
128 | - // console.log('cart-list', this.$store.state.cart.cartList); | |
129 | - // this.totalPrice=0 | |
130 | 127 | return this.$store.state.cart.cartList |
131 | 128 | }, |
132 | - childIsOpen() { | |
133 | - const temp = [] | |
134 | - temp.length = this.$store.state.cart.cartList.length | |
135 | - for (let i = 0; i < temp.length; i++) { | |
136 | - temp[i] = false | |
137 | - } | |
138 | - // console.log('this.childisOPne===>', temp) | |
139 | - return temp | |
140 | - }, | |
129 | + totalPrice() { | |
130 | + const itemPriceArr = [] | |
131 | + this.cartList.map(item =>{ | |
132 | + itemPriceArr.push(item.num*item.nowPrice) | |
133 | + }) | |
134 | + let total = 0 | |
135 | + if(this.pIsoPen){ | |
136 | + itemPriceArr.map(item =>{ | |
137 | + total += item | |
138 | + }) | |
139 | + }else{ | |
140 | + this.childIsOpenArr.map((item,index) =>{ | |
141 | + if(item){ | |
142 | + total += itemPriceArr[index] | |
143 | + } | |
144 | + }) | |
145 | + } | |
146 | + return total | |
147 | + }, | |
148 | + | |
141 | 149 | }, |
142 | 150 | onShow() { |
143 | - this.pIsoPen = false | |
144 | 151 | }, |
145 | 152 | onLoad: function() { |
153 | + //全选置否 | |
154 | + this.pIsoPen = false | |
146 | 155 | store.dispatch('cart/getCartList', { |
147 | 156 | uid: this.$store.state.user.userInfo.uid, // 用户id |
148 | 157 | }) |
158 | + .then((res)=>{ | |
159 | + //单选置否 | |
160 | + const temp = [] | |
161 | + temp.length = this.$store.state.cart.cartList.length | |
162 | + for (let i = 0; i < temp.length; i++) { | |
163 | + temp[i] = false | |
164 | + } | |
165 | + this.childIsOpenArr = temp | |
166 | + }) | |
149 | 167 | }, |
150 | 168 | methods: { |
169 | + //全选按钮 | |
170 | + pClick(){ | |
171 | + if(this.pIsoPen){ | |
172 | + this.pIsoPen = false | |
173 | + this.childIsOpenArr.map((item,index)=> { | |
174 | + this.childIsOpenArr[index] = false | |
175 | + }) | |
176 | + }else{ | |
177 | + this.pIsoPen = true | |
178 | + this.childIsOpenArr.map((item,index)=> { | |
179 | + this.childIsOpenArr[index] = true | |
180 | + }) | |
181 | + } | |
182 | + }, | |
183 | + //单选按钮 | |
184 | + childClick(type,index){ | |
185 | + if(type){ | |
186 | + this.childIsOpenArr[index] = false | |
187 | + this.pIsoPen = false | |
188 | + }else{ | |
189 | + this.childIsOpenArr[index] = true | |
190 | + } | |
191 | + if(this.childIsOpenArr.find(item => item == false)==undefined){ | |
192 | + this.pIsoPen = true | |
193 | + } | |
194 | + console.log(this.childIsOpenArr) | |
195 | + }, | |
151 | 196 | //修改购物车 |
152 | 197 | chooseCartModi(mp_id,sk_id,price,pid,num,cart_id,index){ |
153 | 198 | // console.log('modi',mp_id,sk_id,price,pid,num,cart_id) |
... | ... | @@ -169,7 +214,7 @@ export default { |
169 | 214 | }) |
170 | 215 | |
171 | 216 | this.$forceUpdate() |
172 | - console.log('21212121212',this.cartList) | |
217 | + // console.log('21212121212',this.cartList) | |
173 | 218 | }, |
174 | 219 | //底部弹窗开关 |
175 | 220 | showBottom(isCart,pid,skId,mp_id,cart_id,index){ |
... | ... | @@ -190,8 +235,6 @@ export default { |
190 | 235 | this.isShowBottom = false; |
191 | 236 | }, |
192 | 237 | toGoods(id, sk_id) { |
193 | - console.log('cart-list', this.$store.state.cart.cartList) | |
194 | - console.log('---', '../frameDetail/frameDetail?pid=' + id + '&sk_id=' + sk_id) | |
195 | 238 | uni.navigateTo({ |
196 | 239 | url: '../frameDetail/frameDetail?pid=' + id + '&sk_id=' + sk_id, |
197 | 240 | success: res => {}, |
... | ... | @@ -205,20 +248,14 @@ export default { |
205 | 248 | }) |
206 | 249 | }, |
207 | 250 | counter(index, isadd, item) { |
208 | - // console.log('===>>counter ===>num',num) | |
209 | - // console.log('===>>counter ===>isadd',isadd) | |
210 | - console.log('item=====>', item) | |
211 | - console.log('num=====>', item.num) | |
251 | + // console.log('item=====>', item) | |
252 | + // console.log('num=====>', item.num) | |
212 | 253 | const nums = parseInt(item.num) |
213 | 254 | if (isadd) { |
214 | 255 | if (nums >= this.maxCount) { |
215 | 256 | this.addDisabled = true |
216 | 257 | } else { |
217 | 258 | this.addDisabled = true |
218 | - // 修改num | |
219 | - // if (this.childIsOpen[index]) { | |
220 | - // this.totalPrice = this.totalPrice + this.$store.state.cart.cartList[index].nowPrice | |
221 | - // } | |
222 | 259 | store.dispatch('cart/modiCart', { |
223 | 260 | uid: this.$store.state.user.userInfo.uid, |
224 | 261 | openid: this.$store.state.user.userInfo.openid, |
... | ... | @@ -240,10 +277,7 @@ export default { |
240 | 277 | this.desDisabled = true |
241 | 278 | } else { |
242 | 279 | this.desDisabled = false |
243 | - // post 请求修改相关参数 | |
244 | - // if (this.childIsOpen[index]) { | |
245 | - // this.totalPrice = this.totalPrice - this.$store.state.cart.cartList[index].nowPrice | |
246 | - // } | |
280 | + | |
247 | 281 | store.dispatch('cart/modiCart', { |
248 | 282 | uid: this.$store.state.user.userInfo.uid, |
249 | 283 | openid: this.$store.state.user.userInfo.openid, |
... | ... | @@ -263,57 +297,13 @@ export default { |
263 | 297 | } |
264 | 298 | |
265 | 299 | }, |
266 | - // Change(isopen, indexC) { | |
267 | - // // console.log('lalla===>',isopen) | |
268 | - // this.childIsOpen[indexC] = !isopen | |
269 | - // if (!isopen) { | |
270 | - // this.totalPrice = this.totalPrice + (this.$store.state.cart.cartList[indexC].num * this.$store.state.cart.cartList[indexC].nowPrice) | |
271 | - // } else { | |
272 | - // this.totalPrice = this.totalPrice - (this.$store.state.cart.cartList[indexC].num * this.$store.state.cart.cartList[indexC].nowPrice) | |
273 | - // } | |
274 | - // let m = true | |
275 | - // for (let i = 0; i < this.childIsOpen.length; i++) { | |
276 | - // m = m & this.childIsOpen[i] | |
277 | - // } | |
278 | - // if (m === 1) { | |
279 | - // this.pIsoPen = true | |
280 | - // } else { | |
281 | - // this.pIsoPen = false | |
282 | - // } | |
283 | - // }, | |
284 | - // pChange(isopen) { | |
285 | - // this.pIsoPen = !isopen | |
286 | - // for (let i = 0; i < this.childIsOpen.length; i++) { | |
287 | - // this.childIsOpen[i] = !isopen | |
288 | - // } | |
289 | - // if (this.pIsoPen) { | |
290 | - // // 计算总价逻辑 | |
291 | - // if (this.childIsOpen.length !== 0) { | |
292 | - // for (let i = 0; i < this.childIsOpen.length; i++) { | |
293 | - // if (this.childIsOpen[i]) { | |
294 | - // this.totalPrice = this.totalPrice + (this.$store.state.cart.cartList[i].num * this.$store.state.cart.cartList[i].nowPrice) | |
295 | - // } | |
296 | - // } | |
297 | - // } | |
298 | - // } else { | |
299 | - // this.totalPrice = 0 | |
300 | - // } | |
301 | - // }, | |
302 | 300 | delCart(cart_id, index) { |
303 | - // console.log('userInfo',this.$store.state.user.userInfo) | |
304 | 301 | cart_id = parseInt(cart_id) |
305 | - // console.log('delcart------>cart_id',cart_id) | |
306 | - // console.log('cartlist====>delcart',this.$store.state.cart.cartList) | |
307 | - // console.log('delcart======>index',index) | |
308 | 302 | uni.showModal({ |
309 | 303 | title: '是否删除该商品', |
310 | - // content: '是否删除该商品', | |
311 | 304 | success: function (res) { |
312 | 305 | if (res.confirm) { |
313 | - // if (this.childIsOpen[index]) { | |
314 | - // this.totalPrice = this.totalPrice - (this.$store.state.cart.cartList[index].nowPrice*this.$store.state.cart.cartList[index].num) | |
315 | - // } | |
316 | - console.log('index===>',index) | |
306 | + | |
317 | 307 | store.dispatch('cart/delCart', { |
318 | 308 | uid: this.$store.state.user.userInfo.uid, |
319 | 309 | openid: this.$store.state.user.userInfo.openid, | ... | ... |