购物车列表数量处理

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' backgroundColor: '#000000'
}); });
} }
this.countScrollTitle(e.scrollTop); this.countScrollTitle(e.scrollTop);
}, },
onLoad(e) { onLoad(e) {
@@ -369,7 +369,7 @@
if (res.code == 0) { if (res.code == 0) {
uni.cache.set('shopUser', res.data) uni.cache.set('shopUser', res.data)
this.handlemessage() //监听websocket返回 this.handlemessage() //监听websocket返回
// this.productqueryProduct() //list 数据 this.productqueryProduct() //list 数据
} else { } else {
setTimeout(() => { setTimeout(() => {
uni.pro.switchTab('index/index') uni.pro.switchTab('index/index')
@@ -401,7 +401,7 @@
switch (msg.type) { switch (msg.type) {
case 'sku': // sku 数量 查询这个商品的价格和数量 case 'sku': // sku 数量 查询这个商品的价格和数量
this.$set(this, 'amountcartNumber', msg.amount) this.$set(this, 'amountcartNumber', msg.amount)
this.productqueryProduct() //list 数据 // this.productqueryProduct() //list 数据
break; break;
case 'clearCart': case 'clearCart':
this.cartLists = msg this.cartLists = msg
@@ -427,8 +427,7 @@
break; break;
case 'addcart': case 'addcart':
this.cartLists = msg this.cartLists = msg
// console.log(msg, 1111) // this.productqueryProduct() //list 数据
this.productqueryProduct() //list 数据
try { try {
if (msg.data.length != 0) { if (msg.data.length != 0) {
let nums = 0 let nums = 0
@@ -453,10 +452,35 @@
} catch (e) { } catch (e) {
//TODO handle the exception //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; break;
} }
// console.log(this.cartLists.data, 1111111) // 判断是否显示购物车
this.cartListsdatashow = this.cartLists.data.length == 0 ? false : true this.cartListsdatashow = this.cartLists.data.length == 0 ? false : true
} }
} catch (e) { } catch (e) {
//TODO handle the exception //TODO handle the exception
@@ -667,7 +691,7 @@
}); });
}, },
// 点击菜单商品滚动到指定为止 // 点击菜单商品滚动到指定为止
titleClickHandle(id,index) { titleClickHandle(id, index) {
this.titleTopNumIndexFalg = false; this.titleTopNumIndexFalg = false;
this.titleTopNumIndex = index; this.titleTopNumIndex = index;
uni.createSelectorQuery() uni.createSelectorQuery()
@@ -692,7 +716,7 @@
}, },
// 计算滚动到那个标题 // 计算滚动到那个标题
countScrollTitle: _.throttle(function(top) { countScrollTitle: _.throttle(function(top) {
if (this.titleTopNums.length > 1 && this.titleTopNumIndexFalg) { if (this.titleTopNums.length > 1 && this.titleTopNumIndexFalg) {
for (let i = 0; i <= this.titleTopNums.length - 1; i++) { for (let i = 0; i <= this.titleTopNums.length - 1; i++) {
if (top >= this.titleTopNums[i] && top < this.titleTopNums[i + 1]) { if (top >= this.titleTopNums[i] && top < this.titleTopNums[i + 1]) {
@@ -704,7 +728,7 @@
} }
} }
//这里必须要每次滚动前 清除一次 //这里必须要每次滚动前 清除一次
clearTimeout(this.timer) clearTimeout(this.timer)
// 如果停留则表示滚动结束 一旦空了1s就判定为滚动结束 // 如果停留则表示滚动结束 一旦空了1s就判定为滚动结束
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
this.titleTopNumIndexFalg = true; this.titleTopNumIndexFalg = true;
@@ -719,7 +743,7 @@
.select(`#title${i.id}`) .select(`#title${i.id}`)
.boundingClientRect((res) => { .boundingClientRect((res) => {
topNums.push(res.top - this.menuInfo.top - this.menuInfo.height - this topNums.push(res.top - this.menuInfo.top - this.menuInfo.height - this
.paddingBtmSize*8 - .paddingBtmSize * 8 -
this.tabHeadHeight + 40); this.tabHeadHeight + 40);
}) })
.exec(); .exec();