|
|
@@ -15,19 +15,65 @@ Page({
|
|
|
// 获取购物车商品
|
|
|
getCartProds(){
|
|
|
API.getCartProds().then(res=>{
|
|
|
- console.log("999999999999",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
|
|
|
+ })
|
|
|
})
|
|
|
},
|
|
|
+ // 购物车选中/取消
|
|
|
+ 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=>{
|
|
|
+
|
|
|
+ })
|
|
|
+ },
|
|
|
+ // 删除选中的商品
|
|
|
+ deleteProd(){
|
|
|
+ let ids = [];
|
|
|
+ this.data.cartList.map(item=>{
|
|
|
+ if(item.checkFlag){
|
|
|
+ ids.push(item.id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ API.deleteCartProds().then(res=>{
|
|
|
+
|
|
|
+ })
|
|
|
+ },
|
|
|
// 复选框变化事件
|
|
|
handleCheckboxChange(e) {
|
|
|
+ const oldCheckIds = this.data.checkedIds;
|
|
|
const checkedIds = e.detail.value; // 选中的商品id数组(如 ['1', '2'])
|
|
|
const cartList = this.data.cartList.map(item => {
|
|
|
- item.checked = checkedIds.includes(item.id.toString());
|
|
|
+ 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));
|
|
|
+ this.handleCheckboxFn(id[0]);
|
|
|
+ }else{
|
|
|
+ this.handleCheckboxFn(checkedIds.at(-1));
|
|
|
+ }
|
|
|
this.setData({
|
|
|
cartList,
|
|
|
+ checkedIds,
|
|
|
isAllChecked: checkedIds.length === this.data.cartList.length
|
|
|
});
|
|
|
this.calculateTotal(); // 计算总价
|
|
|
@@ -36,7 +82,7 @@ Page({
|
|
|
toggleAll() {
|
|
|
const isAllChecked = !this.data.isAllChecked;
|
|
|
const cartList = this.data.cartList.map(item => {
|
|
|
- item.checked = isAllChecked;
|
|
|
+ item.checkFlag = isAllChecked;
|
|
|
return item;
|
|
|
});
|
|
|
this.setData({ cartList, isAllChecked });
|
|
|
@@ -46,7 +92,7 @@ Page({
|
|
|
increaseCount(e) {
|
|
|
const id = e.currentTarget.dataset.id;
|
|
|
const cartList = this.data.cartList.map(item => {
|
|
|
- if (item.id == id) item.count++;
|
|
|
+ if (item.id == id) item.quantity++;
|
|
|
return item;
|
|
|
});
|
|
|
this.setData({ cartList });
|
|
|
@@ -57,7 +103,7 @@ Page({
|
|
|
decreaseCount(e) {
|
|
|
const id = e.currentTarget.dataset.id;
|
|
|
const cartList = this.data.cartList.map(item => {
|
|
|
- if (item.id == id && item.count > 1) item.count--;
|
|
|
+ if (item.id == id && item.quantity > 1) item.quantity--;
|
|
|
return item;
|
|
|
});
|
|
|
this.setData({ cartList });
|
|
|
@@ -68,8 +114,8 @@ Page({
|
|
|
let totalPrice = 0;
|
|
|
let checkedCount = 0;
|
|
|
this.data.cartList.forEach(item => {
|
|
|
- if (item.checked) {
|
|
|
- totalPrice += item.price * item.count;
|
|
|
+ if (item.checkFlag) {
|
|
|
+ totalPrice += item.price * item.quantity;
|
|
|
checkedCount++;
|
|
|
}
|
|
|
});
|