购物车:接入修改数量 API(全选、不选)

pull/1/MERGE
YunaiV 2023-08-30 08:46:15 +08:00
parent 92cb6252ae
commit 6bdbebe2a0
2 changed files with 96 additions and 110 deletions

View File

@ -4,8 +4,12 @@ export function addCart(data) {
return request.post("app-api/trade/cart/add", data); return request.post("app-api/trade/cart/add", data);
} }
export function updateCart(data) { export function updateCartCount(data) {
return request.put("app-api/trade/cart/update", data); return request.put("app-api/trade/cart/update-count", data);
}
export function updateCartSelected(data) {
return request.put("app-api/trade/cart/update-selected", data);
} }
export function resetCart(data) { export function resetCart(data) {

View File

@ -22,11 +22,11 @@
<block v-for="(item,index) in cartList.valid" :key="index"> <block v-for="(item,index) in cartList.valid" :key="index">
<view class='item acea-row row-between-wrapper'> <view class='item acea-row row-between-wrapper'>
<!-- #ifndef MP --> <!-- #ifndef MP -->
<checkbox :value="(item.id).toString()" :checked="item.selected" <checkbox :value="item.id.toString()" :checked="item.selected"
:disabled="!item.canChecked && footerswitch" style="margin-right: 10rpx;" /> :disabled="!item.canChecked && footerswitch" style="margin-right: 10rpx;" />
<!-- #endif --> <!-- #endif -->
<!-- #ifdef MP --> <!-- #ifdef MP -->
<checkbox :value="item.id" :checked="item.selected" <checkbox :value="item.id.toString()" :checked="item.selected"
:disabled="!item.canChecked && footerswitch" /> :disabled="!item.canChecked && footerswitch" />
<!-- #endif --> <!-- #endif -->
<navigator :url='"/pages/goods_details/index?id=" + item.spu.id' hover-class='none' <navigator :url='"/pages/goods_details/index?id=" + item.spu.id' hover-class='none'
@ -39,7 +39,7 @@
<view class='line1' :class="item.canChecked?'':'reColor'"> <view class='line1' :class="item.canChecked?'':'reColor'">
{{ item.spu.name }} {{ item.spu.name }}
</view> </view>
<view class='infor line1' v-if="item.sku.properties"> <view class='infor line1' v-if="item.sku">
<text v-for="property in item.sku.properties" style="padding-left: 2px">{{property.valueName}}</text> <text v-for="property in item.sku.properties" style="padding-left: 2px">{{property.valueName}}</text>
</view> </view>
<view class='money' v-if="item.canChecked">{{ fen2yuan(item.sku.price) }}</view> <view class='money' v-if="item.canChecked">{{ fen2yuan(item.sku.price) }}</view>
@ -490,90 +490,101 @@
checkboxAllChange: function(event) { checkboxAllChange: function(event) {
let value = event.detail.value; let value = event.detail.value;
if (value.length > 0) { if (value.length > 0) {
this.setAllSelectValue(1) this.setAllSelectValue(true)
} else { } else {
this.setAllSelectValue(0) this.setAllSelectValue(false)
} }
}, },
setAllSelectValue: function(status) { /**
let that = this; * 全选 / 全不选
let selectValue = []; */
let valid = that.cartList.valid; setAllSelectValue: function(selected) {
if (valid.length > 0) { const valid = this.cartList.valid;
let newValid = valid.map(item => { const values = [];
if (status) { for (const item of valid) {
if (that.footerswitch) { values.push(item.id.toString());
if (item.canChecked) {
item.selected = true;
selectValue.push(item.id);
} else {
item.selected = false;
}
} else {
item.selected = true;
selectValue.push(item.id);
}
that.isAllSelect = true;
} else {
item.selected = false;
that.isAllSelect = false;
}
return item;
});
that.$set(that.cartList, 'valid', newValid);
that.selectValue = selectValue;
that.switchSelect();
} }
TradeCartApi.updateCartSelected({
ids: values,
selected: selected
}).then(res => {
this.getCartList()
})
}, },
/**
* 更新是否选中
*/
checkboxChange: function(event) { checkboxChange: function(event) {
let that = this; // uniapp event
let value = event.detail.value; const valid = this.cartList.valid;
let valid = that.cartList.valid; const oldValues = [];
let arr1 = []; for (const item of valid) {
let arr2 = []; if (item.canChecked && item.selected) {
let arr3 = []; oldValues.push(item.id.toString());
let newValid = valid.map(item => { } else if (!this.footerswitch && item.selected) {
if (that.inArray(item.id, value)) { oldValues.push(item.id.toString());
if (that.footerswitch) {
if (item.canChecked) {
item.selected = true;
arr1.push(item);
} else {
item.selected = false;
} }
} else {
item.selected = true;
arr1.push(item);
} }
} else { const newValues = event.detail.value;
item.selected = false;
arr2.push(item); //
const selectedItem = newValues.find(item => !oldValues.includes(item));
if (selectedItem) {
TradeCartApi.updateCartSelected({
ids: [selectedItem],
selected: true
}).then(res => {
this.getCartList()
})
return;
} }
return item; //
}); const cancelSelectedItem = oldValues.find(item => !newValues.includes(item));
if (that.footerswitch) { if (cancelSelectedItem) {
arr3 = arr2.filter(item => !item.canChecked); TradeCartApi.updateCartSelected({
ids: [cancelSelectedItem],
selected: false
}).then(res => {
this.getCartList()
})
} }
that.$set(that.cartList, 'valid', newValid);
that.isAllSelect = newValid.length === arr1.length + arr3.length;
that.selectValue = value;
that.switchSelect();
}, },
/** /**
* 合计金额数量 * 合计金额数量
*/ */
switchSelect: function() { switchSelect: function() {
//
const validList = this.cartList.valid; const validList = this.cartList.valid;
const selectValue = this.selectValue; const selectValue = [];
if (selectValue.length < 1) { let isAllSelect = true;
this.selectCountPrice = 0.00; if (validList && validList.length > 0) {
return; for (const item of validList) {
if (item.canChecked) {
if (item.selected) {
selectValue.push(item.id);
} else {
isAllSelect = false;
} }
continue;
} else {
if (!this.footerswitch && item.selected) {
selectValue.push(item.id);
}
if (!this.footerswitch && !item.selected) {
isAllSelect = false;
}
}
}
}
this.selectValue = selectValue;
this.isAllSelect = isAllSelect;
// //
let selectCountPrice = 0.00; let selectCountPrice = 0.00;
let cartCount = 0 let cartCount = 0
for (let index in validList) { for (let index in validList) {
if (this.inArray(validList[index].id, selectValue)) { if (this.inArray(validList[index].id, selectValue)
&& validList[index].sku) {
selectCountPrice = this.$util.$h.Add(selectCountPrice, this.$util.$h.Mul(validList[index].count, validList[index].sku.price)) selectCountPrice = this.$util.$h.Add(selectCountPrice, this.$util.$h.Mul(validList[index].count, validList[index].sku.price))
cartCount += validList[index].count cartCount += validList[index].count
} }
@ -675,7 +686,6 @@
const invalidList = cartList.invalidList; const invalidList = cartList.invalidList;
// //
const selectValue = [];
if (validList.length > 0) { if (validList.length > 0) {
for (let index in validList) { for (let index in validList) {
// //
@ -687,16 +697,12 @@
// why SPU SKU invalidList SPU // why SPU SKU invalidList SPU
if (sku && sku.stock > 0) { if (sku && sku.stock > 0) {
validList[index].canChecked = true; // validList[index].canChecked = true; //
selectValue.push(validList[index].id);
} else { } else {
validList[index].canChecked = false; // validList[index].canChecked = false; //
} }
} }
} }
this.$set(this.cartList, 'valid', validList); this.$set(this.cartList, 'valid', validList);
this.selectValue = selectValue;
let newArr = validList.filter(item => item.canChecked);
this.isAllSelect = newArr.length === selectValue.length && newArr.length;
this.switchSelect(); this.switchSelect();
// //
@ -723,36 +729,12 @@
let that = this; let that = this;
that.goodsHidden = !that.goodsHidden; that.goodsHidden = !that.goodsHidden;
}, },
/**
* 切换到管理
*/
manage: function() { manage: function() {
let that = this; this.footerswitch = !this.footerswitch;
that.footerswitch = !that.footerswitch; this.switchSelect();
let arr1 = [];
let arr2 = [];
let newValid = that.cartList.valid.map(item => {
if (that.footerswitch) {
if (item.canChecked) {
if (item.selected) {
arr1.push(item.id);
}
} else {
item.selected = false;
arr2.push(item);
}
} else {
if (item.selected) {
arr1.push(item.id);
}
}
return item;
});
that.cartList.valid = newValid;
if (that.footerswitch) {
that.isAllSelect = newValid.length === arr1.length + arr2.length;
} else {
that.isAllSelect = newValid.length === arr1.length;
}
that.selectValue = arr1;
that.switchSelect();
}, },
unsetCart: function() { unsetCart: function() {
let that = this, let that = this,