购物车:接入修改数量 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);
}
export function updateCart(data) {
return request.put("app-api/trade/cart/update", data);
export function updateCartCount(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) {

View File

@ -22,11 +22,11 @@
<block v-for="(item,index) in cartList.valid" :key="index">
<view class='item acea-row row-between-wrapper'>
<!-- #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;" />
<!-- #endif -->
<!-- #ifdef MP -->
<checkbox :value="item.id" :checked="item.selected"
<checkbox :value="item.id.toString()" :checked="item.selected"
:disabled="!item.canChecked && footerswitch" />
<!-- #endif -->
<navigator :url='"/pages/goods_details/index?id=" + item.spu.id' hover-class='none'
@ -39,7 +39,7 @@
<view class='line1' :class="item.canChecked?'':'reColor'">
{{ item.spu.name }}
</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>
</view>
<view class='money' v-if="item.canChecked">{{ fen2yuan(item.sku.price) }}</view>
@ -139,7 +139,7 @@
</template>
<script>
let sysHeight = 0
let sysHeight = 0
import { cartDel, getResetCart } from '@/api/order.js';
import { getProductHot, collectAll, getProductDetail } from '@/api/store.js';
import { toLogin } from '@/libs/login.js';
@ -490,90 +490,101 @@
checkboxAllChange: function(event) {
let value = event.detail.value;
if (value.length > 0) {
this.setAllSelectValue(1)
this.setAllSelectValue(true)
} else {
this.setAllSelectValue(0)
this.setAllSelectValue(false)
}
},
setAllSelectValue: function(status) {
let that = this;
let selectValue = [];
let valid = that.cartList.valid;
if (valid.length > 0) {
let newValid = valid.map(item => {
if (status) {
if (that.footerswitch) {
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();
}
/**
* 全选 / 全不选
*/
setAllSelectValue: function(selected) {
const valid = this.cartList.valid;
const values = [];
for (const item of valid) {
values.push(item.id.toString());
}
TradeCartApi.updateCartSelected({
ids: values,
selected: selected
}).then(res => {
this.getCartList()
})
},
/**
* 更新是否选中
*/
checkboxChange: function(event) {
let that = this;
let value = event.detail.value;
let valid = that.cartList.valid;
let arr1 = [];
let arr2 = [];
let arr3 = [];
let newValid = valid.map(item => {
if (that.inArray(item.id, value)) {
if (that.footerswitch) {
if (item.canChecked) {
item.selected = true;
arr1.push(item);
} else {
item.selected = false;
}
} else {
item.selected = true;
arr1.push(item);
}
} else {
item.selected = false;
arr2.push(item);
}
return item;
});
if (that.footerswitch) {
arr3 = arr2.filter(item => !item.canChecked);
}
that.$set(that.cartList, 'valid', newValid);
that.isAllSelect = newValid.length === arr1.length + arr3.length;
that.selectValue = value;
that.switchSelect();
// uniapp event
const valid = this.cartList.valid;
const oldValues = [];
for (const item of valid) {
if (item.canChecked && item.selected) {
oldValues.push(item.id.toString());
} else if (!this.footerswitch && item.selected) {
oldValues.push(item.id.toString());
}
}
const newValues = event.detail.value;
//
const selectedItem = newValues.find(item => !oldValues.includes(item));
if (selectedItem) {
TradeCartApi.updateCartSelected({
ids: [selectedItem],
selected: true
}).then(res => {
this.getCartList()
})
return;
}
//
const cancelSelectedItem = oldValues.find(item => !newValues.includes(item));
if (cancelSelectedItem) {
TradeCartApi.updateCartSelected({
ids: [cancelSelectedItem],
selected: false
}).then(res => {
this.getCartList()
})
}
},
/**
* 合计金额数量
*/
switchSelect: function() {
const validList = this.cartList.valid;
const selectValue = this.selectValue;
if (selectValue.length < 1) {
this.selectCountPrice = 0.00;
return;
}
//
const validList = this.cartList.valid;
const selectValue = [];
let isAllSelect = true;
if (validList && validList.length > 0) {
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 cartCount = 0
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))
cartCount += validList[index].count
}
@ -675,7 +686,6 @@
const invalidList = cartList.invalidList;
//
const selectValue = [];
if (validList.length > 0) {
for (let index in validList) {
//
@ -687,16 +697,12 @@
// why SPU SKU invalidList SPU
if (sku && sku.stock > 0) {
validList[index].canChecked = true; //
selectValue.push(validList[index].id);
} else {
validList[index].canChecked = false; //
}
}
}
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();
//
@ -723,36 +729,12 @@
let that = this;
that.goodsHidden = !that.goodsHidden;
},
/**
* 切换到管理
*/
manage: function() {
let that = this;
that.footerswitch = !that.footerswitch;
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();
this.footerswitch = !this.footerswitch;
this.switchSelect();
},
unsetCart: function() {
let that = this,