购物车列表数量处理

This commit is contained in:
魏啾
2024-07-31 10:52:35 +08:00
parent f6ac523cd0
commit a5eb227aca

View File

@@ -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();