时间:2019-05-03 来源:小程序工厂
没有发代码也看不出什么问题来~
猜测可能是频繁的操作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