|
|
import wx from '../../utils/wx';
|
|
|
import cartModel from '../../models/cart/cart';
|
|
|
import cartHandle from '../../models/cart/cart-handle';
|
|
|
|
|
|
const app = getApp();
|
|
|
const router = global.router;
|
|
|
|
|
|
const reverseSelected = {
|
|
|
Y: 'N',
|
|
|
N: 'Y'
|
|
|
};
|
|
|
|
|
|
Page({
|
|
|
data: {
|
|
|
isEditing: false
|
...
|
...
|
@@ -15,21 +21,207 @@ Page({ |
|
|
this.getCartData();
|
|
|
|
|
|
},
|
|
|
onHide: function () {
|
|
|
if (this.data.isEditing) {
|
|
|
this.editCartStatus({clearEditing: true});
|
|
|
}
|
|
|
},
|
|
|
showToast(title) {
|
|
|
wx.showToast({
|
|
|
title: title,
|
|
|
icon: 'none',
|
|
|
duration: 2000
|
|
|
});
|
|
|
},
|
|
|
showModal(title) {
|
|
|
return wx.showModal({
|
|
|
title: '',
|
|
|
content: title,
|
|
|
cancelText: '取消',
|
|
|
confirmText: '确认',
|
|
|
confirmColor: '#d0021b'
|
|
|
});
|
|
|
},
|
|
|
goShopping() {
|
|
|
router.go('home');
|
|
|
},
|
|
|
handleOrdinaryCartData(data) {
|
|
|
let resData = cartHandle.formatOrdinaryCartData(data, true);
|
|
|
|
|
|
this._allGoodsList = resData.allGoodsList;
|
|
|
delete resData.allGoodsList;
|
|
|
this.setData(resData);
|
|
|
|
|
|
return resData;
|
|
|
},
|
|
|
getCartData() {
|
|
|
cartModel.getCartData({uid: app.getUid()})
|
|
|
.then(res => {
|
|
|
if (res.code === 200 && res.data) {
|
|
|
this.setData(cartHandle.formatOrdinaryCartData(res.data));
|
|
|
} else {
|
|
|
return Promise.reject();
|
|
|
cartModel.getCartData({uid: app.getUid()}).then(res => {
|
|
|
if (res.code === 200 && res.data) {
|
|
|
return this.handleOrdinaryCartData(res.data);
|
|
|
} else {
|
|
|
return Promise.reject();
|
|
|
}
|
|
|
}).catch(() => {});
|
|
|
},
|
|
|
editCartStatus(e) {
|
|
|
let isEditing = !this.data.isEditing;
|
|
|
|
|
|
if (e.clearEditing) {
|
|
|
isEditing = false;
|
|
|
}
|
|
|
|
|
|
this._removeGoodsList = {};
|
|
|
this.setData({isEditing});
|
|
|
},
|
|
|
editCart(list, selected, buyNum) {
|
|
|
list = cartHandle.changeGoodsListData(list, selected, buyNum);
|
|
|
|
|
|
if (!list.length) {
|
|
|
return Promise.resolve({});
|
|
|
}
|
|
|
|
|
|
return cartModel.selectAndQueryCart({
|
|
|
product_sku_list: JSON.stringify(list),
|
|
|
uid: app.getUid()
|
|
|
}).then(res => {
|
|
|
if (res.code === 200 && res.data) {
|
|
|
return this.handleOrdinaryCartData(res.data);
|
|
|
} else {
|
|
|
return Promise.reject();
|
|
|
}
|
|
|
}).catch(() => {});
|
|
|
},
|
|
|
removeCartGoods(list) {
|
|
|
list = cartHandle.changeGoodsListData(list);
|
|
|
|
|
|
if (!list.length) {
|
|
|
return Promise.resolve({});
|
|
|
}
|
|
|
|
|
|
return cartModel.removeAndQueryCart({
|
|
|
product_sku_list: JSON.stringify(list),
|
|
|
uid: app.getUid()
|
|
|
}).then(res => {
|
|
|
if (res && res.code === 200) {
|
|
|
return this.handleOrdinaryCartData(res.data);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
chooseGoodsItem(e) {
|
|
|
let item = e.detail;
|
|
|
|
|
|
if (item.selected !== 'Y' && item.buy_number - item.storage_number > 0) {
|
|
|
return this.showToast('您勾选的商品库存不足');
|
|
|
}
|
|
|
|
|
|
item.selected = reverseSelected[item.selected] || 'Y';
|
|
|
this.editCart([item]);
|
|
|
},
|
|
|
chooseGoodsBundle(e) {
|
|
|
let bundle = e.detail;
|
|
|
|
|
|
if (bundle.selected !== 'Y' && bundle.pool_buy_number - bundle.pool_storage_number > 0) {
|
|
|
return this.showToast('您勾选的商品库存不足');
|
|
|
}
|
|
|
|
|
|
let selected = reverseSelected[bundle.selected] || 'Y';
|
|
|
this.editCart(bundle.goods_list, selected);
|
|
|
},
|
|
|
editGoodsNum(e) {
|
|
|
let goods = e.detail;
|
|
|
|
|
|
if (goods) {
|
|
|
return cartModel.editCartGoodsNum({
|
|
|
product_sku: goods.product_sku,
|
|
|
uid: app.getUid()
|
|
|
}, goods.isAdd).then(res => {
|
|
|
if (res && res.code === 200) {
|
|
|
this.editCart([goods], 'Y');
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
editBundleNum(e) {
|
|
|
let bundle = e.detail;
|
|
|
|
|
|
if (bundle) {
|
|
|
return cartModel.editCartBundleNum({
|
|
|
activity_id: bundle.pool_id,
|
|
|
batch_no: bundle.pool_batch_no,
|
|
|
uid: app.getUid()
|
|
|
}, bundle.isAdd).then(res => {
|
|
|
if (res && res.code === 200) {
|
|
|
this.editCart(bundle.goods_list, 'Y');
|
|
|
}
|
|
|
})
|
|
|
.catch(() => {});
|
|
|
}
|
|
|
},
|
|
|
editCartAction() {
|
|
|
this.setData({isEditing: !this.data.isEditing});
|
|
|
removeGoodsListInEdit(e) {
|
|
|
let info = e.detail;
|
|
|
|
|
|
if (info.goods_list) {
|
|
|
let list = info.goods_list || [];
|
|
|
|
|
|
list.map(item => {
|
|
|
this._removeGoodsList[item.indexKey] = info.selected === 'Y' ? item : false;
|
|
|
});
|
|
|
} else {
|
|
|
this._removeGoodsList[info.indexKey] = info.selected === 'Y' ? info : false;
|
|
|
}
|
|
|
},
|
|
|
mutilRemoveGoods() {
|
|
|
let removeList = [];
|
|
|
|
|
|
this._removeGoodsList = this._removeGoodsList || [];
|
|
|
|
|
|
for (let i in this._removeGoodsList) {
|
|
|
if (this._removeGoodsList.hasOwnProperty(i) && this._removeGoodsList[i]) {
|
|
|
removeList.push(this._removeGoodsList[i]);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
if (!removeList.length) {
|
|
|
return this.showToast('请勾选您要删除的商品');
|
|
|
}
|
|
|
|
|
|
this.showModal('是否删除选中的全部商品?').then(res => {
|
|
|
if (!res.confirm) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
return this.removeCartGoods(removeList);
|
|
|
});
|
|
|
},
|
|
|
cleanInvalidGoods() {
|
|
|
this.showModal('是否确认清空所有失效商品?').then(res => {
|
|
|
if (!res.confirm) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
let invalidGoods = this.data.invalidGoodsList;
|
|
|
let invalidList = [];
|
|
|
|
|
|
if (invalidGoods && invalidGoods.length) {
|
|
|
invalidGoods.map(list => {
|
|
|
invalidList.push.apply(invalidList, list);
|
|
|
})
|
|
|
}
|
|
|
|
|
|
return this.removeCartGoods(invalidList);
|
|
|
});
|
|
|
},
|
|
|
selectAllGoods() {
|
|
|
let isSelectAll = this.data.isSelectAll;
|
|
|
|
|
|
if (this.data.isEditing) {
|
|
|
// TODO;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
this.editCart(this._allGoodsList, isSelectAll ? 'N' : 'Y').then(res => {
|
|
|
if (!isSelectAll && res && res.hasLowStorage) {
|
|
|
this.showToast('您全选的商品中存在库存不足商品,已帮您自动取消勾选成功');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}); |
...
|
...
|
|