小程序
小游戏
企业微信
微信支付
扫描小程序码分享
写了一个购物车。当用户疯狂点击添加商品时,可能会造成下面的这种情况
烦请大佬给点意见
1 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
没有发代码也看不出什么问题来~
猜测可能是频繁的操作setData导致~
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
addBuyNum(e, that) {
let nowNum = e.currentTarget.dataset.addnum;//当前选中商品的数量
let goodsId = e.currentTarget.dataset.goodsid;//商品id
let currentGoodsList = that.data.checkGoodsType;//商品列表
let iconType = e.currentTarget.dataset.type;//是增加商品还是减少商品
let stock = e.currentTarget.dataset.stock;//获取到商品列表时得知的商品库存
let index = e.currentTarget.dataset.index;
let item = e.currentTarget.dataset.item;
let shoppingCart = that.data.shoppingCart;//购物车中的商品
let goodsInfo = that.data.goodsInfo;//总商品
let ShoppingCartTotalPrice = 0;//购物车总价
if (iconType == 'add') {
if (nowNum >= stock) {//不请求数据先判断和之前的库存的差值
that.hud.showWarn("该商品库存不足", 2000);
item.buyNum = nowNum;
currentGoodsList.goodslist[index] = item;
that.setData({
checkGoodsType: currentGoodsList
});
}else{
item.buyNum = nowNum+1;//库存够
//保存选择状态
for (var goodsInfoItem in goodsInfo) {
if (goodsInfo[goodsInfoItem].id == currentGoodsList.id) {
goodsInfo[goodsInfoItem] = currentGoodsList;
}
checkGoodsType: currentGoodsList,
goodsInfo: goodsInfo
//加入购物车
if (shoppingCart.length == 0) {
item.totalPrice = (item.buyNum * item.saleprice);
item.totalPrice = Common.numberToFixed(item.totalPrice);
shoppingCart: new Array(item),
ShoppingCartTotalPrice: item.totalPrice
} else {
if (shoppingCart.map(goodsInCart => goodsInCart.id).indexOf(goodsId) == -1) {
shoppingCart = shoppingCart.concat(item);
shoppingCart.map((goodsInCart) => {
if (goodsId == goodsInCart.id) {
goodsInCart.buyNum = goodsInCart.buyNum + 1;
goodsInCart.totalPrice = (goodsInCart.buyNum * goodsInCart.saleprice);
goodsInCart.totalPrice = Common.numberToFixed(goodsInCart.totalPrice);
shoppingCart = shoppingCart.concat([]);
ShoppingCartTotalPrice = +that.data.ShoppingCartTotalPrice + Number(item.saleprice);
ShoppingCartTotalPrice = Common.numberToFixed(ShoppingCartTotalPrice);
shoppingCart: shoppingCart,
ShoppingCartTotalPrice: ShoppingCartTotalPrice
})
//减去商品数量
item.buyNum = nowNum - 1;
if (item.buyNum <= 0) { item.buyNum = 0 }
goodsInCart.buyNum = goodsInCart.buyNum - 1;
if (goodsInCart.buyNum == 0) {
var index = shoppingCart.indexOf(goodsInCart);
shoppingCart.splice(index, 1);
return;
ShoppingCartTotalPrice = +that.data.ShoppingCartTotalPrice - Number(item.saleprice);
goodsInfo: goodsInfo,
},
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
没有发代码也看不出什么问题来~
猜测可能是频繁的操作setData导致~
addBuyNum(e, that) {
let nowNum = e.currentTarget.dataset.addnum;//当前选中商品的数量
let goodsId = e.currentTarget.dataset.goodsid;//商品id
let currentGoodsList = that.data.checkGoodsType;//商品列表
let iconType = e.currentTarget.dataset.type;//是增加商品还是减少商品
let stock = e.currentTarget.dataset.stock;//获取到商品列表时得知的商品库存
let index = e.currentTarget.dataset.index;
let item = e.currentTarget.dataset.item;
let shoppingCart = that.data.shoppingCart;//购物车中的商品
let goodsInfo = that.data.goodsInfo;//总商品
let ShoppingCartTotalPrice = 0;//购物车总价
if (iconType == 'add') {
if (nowNum >= stock) {//不请求数据先判断和之前的库存的差值
that.hud.showWarn("该商品库存不足", 2000);
item.buyNum = nowNum;
currentGoodsList.goodslist[index] = item;
that.setData({
checkGoodsType: currentGoodsList
});
}else{
item.buyNum = nowNum+1;//库存够
currentGoodsList.goodslist[index] = item;
//保存选择状态
for (var goodsInfoItem in goodsInfo) {
if (goodsInfo[goodsInfoItem].id == currentGoodsList.id) {
goodsInfo[goodsInfoItem] = currentGoodsList;
}
}
that.setData({
checkGoodsType: currentGoodsList,
goodsInfo: goodsInfo
});
//加入购物车
if (shoppingCart.length == 0) {
item.totalPrice = (item.buyNum * item.saleprice);
item.totalPrice = Common.numberToFixed(item.totalPrice);
that.setData({
shoppingCart: new Array(item),
ShoppingCartTotalPrice: item.totalPrice
});
} else {
if (shoppingCart.map(goodsInCart => goodsInCart.id).indexOf(goodsId) == -1) {
item.totalPrice = (item.buyNum * item.saleprice);
item.totalPrice = Common.numberToFixed(item.totalPrice);
shoppingCart = shoppingCart.concat(item);
} else {
shoppingCart.map((goodsInCart) => {
if (goodsId == goodsInCart.id) {
goodsInCart.buyNum = goodsInCart.buyNum + 1;
goodsInCart.totalPrice = (goodsInCart.buyNum * goodsInCart.saleprice);
goodsInCart.totalPrice = Common.numberToFixed(goodsInCart.totalPrice);
}
});
shoppingCart = shoppingCart.concat([]);
}
ShoppingCartTotalPrice = +that.data.ShoppingCartTotalPrice + Number(item.saleprice);
ShoppingCartTotalPrice = Common.numberToFixed(ShoppingCartTotalPrice);
that.setData({
shoppingCart: shoppingCart,
ShoppingCartTotalPrice: ShoppingCartTotalPrice
})
}
}
} else {
//减去商品数量
item.buyNum = nowNum - 1;
if (item.buyNum <= 0) { item.buyNum = 0 }
currentGoodsList.goodslist[index] = item;
for (var goodsInfoItem in goodsInfo) {
if (goodsInfo[goodsInfoItem].id == currentGoodsList.id) {
goodsInfo[goodsInfoItem] = currentGoodsList;
}
}
shoppingCart.map((goodsInCart) => {
if (goodsId == goodsInCart.id) {
goodsInCart.buyNum = goodsInCart.buyNum - 1;
if (goodsInCart.buyNum == 0) {
var index = shoppingCart.indexOf(goodsInCart);
shoppingCart.splice(index, 1);
}
goodsInCart.totalPrice = (goodsInCart.buyNum * goodsInCart.saleprice);
goodsInCart.totalPrice = Common.numberToFixed(goodsInCart.totalPrice);
return;
}
});
shoppingCart = shoppingCart.concat([]);
ShoppingCartTotalPrice = +that.data.ShoppingCartTotalPrice - Number(item.saleprice);
ShoppingCartTotalPrice = Common.numberToFixed(ShoppingCartTotalPrice);
that.setData({
checkGoodsType: currentGoodsList,
goodsInfo: goodsInfo,
shoppingCart: shoppingCart,
ShoppingCartTotalPrice: ShoppingCartTotalPrice
})
}
},