|
|
@@ -15,44 +15,49 @@ Page({
|
|
|
// 获取购物车商品
|
|
|
getCartProds(){
|
|
|
API.getCartProds().then(res=>{
|
|
|
- let num = 0,checkIds=[];
|
|
|
- res.carts.forEach(item=>{
|
|
|
- if(item.checkFlag){
|
|
|
- num+=1;
|
|
|
- checkIds.push(item.id+'');
|
|
|
- }
|
|
|
- })
|
|
|
- this.setData({
|
|
|
- cartList:res.carts,
|
|
|
- totalPrice:res.checkedSumAmount,
|
|
|
- checkedIds:checkIds,
|
|
|
- isAllChecked:num === res.carts.length
|
|
|
- })
|
|
|
+ this.handleCartsFn(res);
|
|
|
})
|
|
|
},
|
|
|
+ // 处理商品函数
|
|
|
+ handleCartsFn(value){
|
|
|
+ let num = 0,checkIds=[];
|
|
|
+ value.carts.forEach(item=>{
|
|
|
+ if(item.checkFlag === "1"){
|
|
|
+ num+=1;
|
|
|
+ checkIds.push(item.id+'');
|
|
|
+ }
|
|
|
+ })
|
|
|
+ this.setData({
|
|
|
+ cartList:value.carts,
|
|
|
+ totalPrice:value.checkedSumAmount,
|
|
|
+ checkedIds:checkIds,
|
|
|
+ isAllChecked:num === value.carts.length
|
|
|
+ })
|
|
|
+ },
|
|
|
// 购物车选中/取消
|
|
|
handleCheckboxFn(prodId){
|
|
|
- let {mercProdId,id,prodAttrId,quantity} = this.data.cartList.filter(item=>item.id == prodId)[0];
|
|
|
- let data = {
|
|
|
- mercId:mercProdId,
|
|
|
- prodId:id,
|
|
|
- prodAttrId,
|
|
|
- count:quantity
|
|
|
- }
|
|
|
- API.checkedCartProds(data).then(res=>{
|
|
|
-
|
|
|
+ let {id} = this.data.cartList.filter(item=>item.id == prodId)[0];
|
|
|
+ API.checkedCartProds({id}).then(res=>{
|
|
|
+ this.handleCartsFn(res);
|
|
|
})
|
|
|
},
|
|
|
+ // 选中/取消选中所有
|
|
|
+ handleCheckedAll(){
|
|
|
+ API.handleCheckedAll().then(res=>{
|
|
|
+
|
|
|
+ })
|
|
|
+ },
|
|
|
+ // 取消选中所有
|
|
|
+ handleUncheckedAll(){
|
|
|
+ API.handleUncheckedAll().then(res=>{
|
|
|
+
|
|
|
+ })
|
|
|
+ },
|
|
|
// 删除选中的商品
|
|
|
deleteProd(){
|
|
|
- let ids = [];
|
|
|
- this.data.cartList.map(item=>{
|
|
|
- if(item.checkFlag){
|
|
|
- ids.push(item.id);
|
|
|
- }
|
|
|
- });
|
|
|
- API.deleteCartProds().then(res=>{
|
|
|
-
|
|
|
+ let ids = this.data.checkedIds;
|
|
|
+ API.deleteCartProds({ids}).then(res=>{
|
|
|
+ this.handleCartsFn(res);
|
|
|
})
|
|
|
},
|
|
|
// 复选框变化事件
|
|
|
@@ -63,7 +68,6 @@ Page({
|
|
|
item.checkFlag = checkedIds.includes(item.id.toString());
|
|
|
return item;
|
|
|
});
|
|
|
- console.log("33333333",oldCheckIds,checkedIds);
|
|
|
// 取消选中
|
|
|
if(oldCheckIds.length > checkedIds.length){
|
|
|
let id = oldCheckIds.filter(item=>!checkedIds.includes(item));
|
|
|
@@ -82,10 +86,15 @@ Page({
|
|
|
toggleAll() {
|
|
|
const isAllChecked = !this.data.isAllChecked;
|
|
|
const cartList = this.data.cartList.map(item => {
|
|
|
- item.checkFlag = isAllChecked;
|
|
|
+ item.checkFlag = isAllChecked;
|
|
|
return item;
|
|
|
});
|
|
|
this.setData({ cartList, isAllChecked });
|
|
|
+ if(isAllChecked){
|
|
|
+ this.handleCheckedAll();
|
|
|
+ }else{
|
|
|
+ this.handleUncheckedAll();
|
|
|
+ }
|
|
|
this.calculateTotal();
|
|
|
},
|
|
|
// 增加数量
|