From a5eb227aca246fa6bae00ee460b7300e83a516c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E5=95=BE?= <1144797966@qq.com> Date: Wed, 31 Jul 2024 10:52:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/order_food/order_food.vue | 44 +++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/pages/order_food/order_food.vue b/pages/order_food/order_food.vue index 1ded8e5..ec4e852 100644 --- a/pages/order_food/order_food.vue +++ b/pages/order_food/order_food.vue @@ -302,7 +302,7 @@ backgroundColor: '#000000' }); } - + this.countScrollTitle(e.scrollTop); }, onLoad(e) { @@ -369,7 +369,7 @@ if (res.code == 0) { uni.cache.set('shopUser', res.data) this.handlemessage() //监听websocket返回 - // this.productqueryProduct() //list 数据 + this.productqueryProduct() //list 数据 } else { setTimeout(() => { uni.pro.switchTab('index/index') @@ -401,7 +401,7 @@ switch (msg.type) { case 'sku': // sku 数量 查询这个商品的价格和数量 this.$set(this, 'amountcartNumber', msg.amount) - this.productqueryProduct() //list 数据 + // this.productqueryProduct() //list 数据 break; case 'clearCart': this.cartLists = msg @@ -427,8 +427,7 @@ break; case 'addcart': this.cartLists = msg - // console.log(msg, 1111) - this.productqueryProduct() //list 数据 + // this.productqueryProduct() //list 数据 try { if (msg.data.length != 0) { let nums = 0 @@ -453,10 +452,35 @@ } catch (e) { //TODO handle the exception } + try { + // 处理购物车相同id的情况下 数量相加 + var summedArray = msg.data.reduce((acc, current) => { + const existing = acc.find(item => item.productId === current.productId); + if (existing) { + existing.number += current.number; + } else { + acc.push({ + ...current + }) + } + return acc; + }, []); + // 购物车总数据与列表页面互通 + this.shopList.productInfo.forEach((item) => { + return item.products.filter(e => { + return summedArray.find(i => { + if (e.id == i.productId) { + e.cartNumber = i.number + } + }) + }) + }) + } catch (e) {} break; } - // console.log(this.cartLists.data, 1111111) + // 判断是否显示购物车 this.cartListsdatashow = this.cartLists.data.length == 0 ? false : true + } } catch (e) { //TODO handle the exception @@ -667,7 +691,7 @@ }); }, // 点击菜单商品滚动到指定为止 - titleClickHandle(id,index) { + titleClickHandle(id, index) { this.titleTopNumIndexFalg = false; this.titleTopNumIndex = index; uni.createSelectorQuery() @@ -692,7 +716,7 @@ }, // 计算滚动到那个标题 countScrollTitle: _.throttle(function(top) { - + if (this.titleTopNums.length > 1 && this.titleTopNumIndexFalg) { for (let i = 0; i <= this.titleTopNums.length - 1; i++) { if (top >= this.titleTopNums[i] && top < this.titleTopNums[i + 1]) { @@ -704,7 +728,7 @@ } } //这里必须要每次滚动前 清除一次 - clearTimeout(this.timer) + clearTimeout(this.timer) // 如果停留则表示滚动结束 一旦空了1s就判定为滚动结束 this.timer = setTimeout(() => { this.titleTopNumIndexFalg = true; @@ -719,7 +743,7 @@ .select(`#title${i.id}`) .boundingClientRect((res) => { topNums.push(res.top - this.menuInfo.top - this.menuInfo.height - this - .paddingBtmSize*8 - + .paddingBtmSize * 8 - this.tabHeadHeight + 40); }) .exec();