购物车:接入修改数量 API(全选、不选)
parent
92cb6252ae
commit
6bdbebe2a0
|
@ -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) {
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue