购物车:接入修改数量 API

pull/1/MERGE
YunaiV 2023-08-29 23:58:33 +08:00
parent f579101f7c
commit 92cb6252ae
1 changed files with 77 additions and 75 deletions

View File

@ -114,8 +114,8 @@
<text class='checkAll'>全选({{selectValue.length}})</text> <text class='checkAll'>全选({{selectValue.length}})</text>
</checkbox-group> </checkbox-group>
</view> </view>
<view class='money acea-row row-middle' v-if="footerswitch==true"> <view class='money acea-row row-middle' v-if="footerswitch">
<text class='font-color'>{{selectCountPrice}}</text> <text class='font-color'>{{ fen2yuan(selectCountPrice) }}</text>
<form @submit="subOrder" report-submit='true'> <form @submit="subOrder" report-submit='true'>
<button class='placeOrder bg-color' formType="submit">立即下单</button> <button class='placeOrder bg-color' formType="submit">立即下单</button>
</form> </form>
@ -140,7 +140,7 @@
<script> <script>
let sysHeight = 0 let sysHeight = 0
import { getCartCounts, changeCartNum, cartDel, getResetCart} from '@/api/order.js'; import { cartDel, getResetCart } from '@/api/order.js';
import { getProductHot, collectAll, getProductDetail } from '@/api/store.js'; import { getProductHot, collectAll, getProductDetail } from '@/api/store.js';
import { toLogin } from '@/libs/login.js'; import { toLogin } from '@/libs/login.js';
import { mapGetters } from "vuex"; import { mapGetters } from "vuex";
@ -155,7 +155,6 @@
}, },
data() { data() {
return { return {
cartCount: 0,
goodsHidden: false, goodsHidden: false,
footerswitch: true, footerswitch: true,
hostProduct: [], hostProduct: [],
@ -163,13 +162,14 @@
hotLimit: 10, hotLimit: 10,
hotScroll: false, hotScroll: false,
cartList: { cartList: { //
valid: [], valid: [], //
invalid: [] invalid: [] //
}, },
isAllSelect: false, // isAllSelect: false, //
selectValue: [], // selectValue: [], //
selectCountPrice: 0.00, selectCountPrice: 0.00, //
cartCount: 0, //
loading: false, loading: false,
loadTitle: '加载更多', // loadTitle: '加载更多', //
@ -211,13 +211,11 @@
valid: [], valid: [],
invalid: [] invalid: []
}; };
this.isAllSelect = false; //
this.selectValue = []; //
this.selectCountPrice = 0.00;
this.cartCount = 0;
this.getCartList(); this.getCartList();
this.isAllSelect = false; //
this.selectValue = []; //
this.selectCountPrice = 0.00;
// TODO
this.cartCount = 0;
} }
}, },
methods: { methods: {
@ -250,7 +248,6 @@
that.loadend = false; that.loadend = false;
that.cartList.valid = []; that.cartList.valid = [];
that.getCartList(); that.getCartList();
that.getCartNum();
} }
}); });
}) })
@ -425,7 +422,6 @@
that.loadend = false; that.loadend = false;
that.cartList.valid = []; that.cartList.valid = [];
that.getCartList(); that.getCartList();
that.getCartNum();
}); });
else else
return that.$util.Tips({ return that.$util.Tips({
@ -443,7 +439,7 @@
productId.push(validList[index].productId); productId.push(validList[index].productId);
} }
} }
}; }
return productId; return productId;
}, },
subCollect: function(event) { subCollect: function(event) {
@ -563,31 +559,27 @@
that.selectValue = value; that.selectValue = value;
that.switchSelect(); that.switchSelect();
}, },
inArray: function(search, array) { /**
for (let i in array) { * 合计金额数量
if (array[i] == search) { */
return true;
}
}
return false;
},
switchSelect: function() { switchSelect: function() {
let that = this; const validList = this.cartList.valid;
let validList = that.cartList.valid; const selectValue = this.selectValue;
let selectValue = that.selectValue;
let selectCountPrice = 0.00;
if (selectValue.length < 1) { if (selectValue.length < 1) {
that.selectCountPrice = selectCountPrice; this.selectCountPrice = 0.00;
} else { return;
for (let index in validList) {
if (that.inArray(validList[index].id, selectValue)) {
selectCountPrice = that.$util.$h.Add(selectCountPrice, that.$util.$h.Mul(validList[index]
.cartNum, validList[
index].price))
}
}
that.selectCountPrice = selectCountPrice;
} }
//
let selectCountPrice = 0.00;
let cartCount = 0
for (let index in validList) {
if (this.inArray(validList[index].id, selectValue)) {
selectCountPrice = this.$util.$h.Add(selectCountPrice, this.$util.$h.Mul(validList[index].count, validList[index].sku.price))
cartCount += validList[index].count
}
}
this.selectCountPrice = selectCountPrice;
this.cartCount = cartCount;
}, },
/** /**
* 购物车手动填写 * 购物车手动填写
@ -606,55 +598,57 @@
this.$set(this.cartList, 'valid', this.cartList.valid) this.$set(this.cartList, 'valid', this.cartList.valid)
} }
}, },
/**
* 减少购买数量
*/
subCart: function(index) { subCart: function(index) {
let that = this; let item = this.cartList.valid[index];
let status = false; if (item.count < 1) {
let item = that.cartList.valid[index]; this.$util.Tips({
item.cartNum = Number(item.cartNum) - 1; title: '商品数量不能减少!'
if (item.cartNum < 1) status = true; });
if (item.cartNum <= 1) { return;
item.cartNum = 1; }
//
item.count = Number(item.count) - 1;
if (item.count <= 1) {
item.count = 1;
item.numSub = true; item.numSub = true;
} else { } else {
item.numSub = false; item.numSub = false;
item.numAdd = false; item.numAdd = false;
} }
if (false == status) { //
that.setCartNum(item.id, item.cartNum, function(data) { this.setCartNum(item.id, item.count);
that.cartList.valid[index] = item;
that.switchSelect();
that.getCartNum();
});
}
}, },
/**
* 增加购买数量
*/
addCart: function(index) { addCart: function(index) {
let that = this; //
let item = that.cartList.valid[index]; let item = this.cartList.valid[index];
item.cartNum = Number(item.cartNum) + 1; item.count = Number(item.count) + 1
if (item.cartNum >= item.stock) { if (item.count >= item.sku.stock) {
item.cartNum = item.stock; item.count = item.sku.stock;
item.numAdd = true; item.numAdd = true;
item.numSub = false; item.numSub = false;
} else { } else {
item.numAdd = false; item.numAdd = false;
item.numSub = false; item.numSub = false;
} }
that.setCartNum(item.id, item.cartNum, function(data) { //
that.cartList.valid[index] = item; this.setCartNum(item.id, item.count);
that.switchSelect();
that.getCartNum();
});
}, },
setCartNum(cartId, cartNum, successCallback) { /**
let that = this; * 修改购物项为指定数量
changeCartNum(cartId, cartNum).then(res => { */
successCallback && successCallback(res.data); setCartNum(cartId, cartNum) {
}); TradeCartApi.updateCart({
}, id: cartId,
getCartNum: function() { count: cartNum
let that = this; }).then(res => {
getCartCounts(true, 'sum').then(res => { //
that.cartCount = res.data.count; this.getCartList()
}); });
}, },
getCartData() { getCartData() {
@ -688,7 +682,7 @@
validList[index].numSub = validList[index].cartNum === 1; validList[index].numSub = validList[index].cartNum === 1;
// //
const sku = validList[index].sku; const sku = validList[index].sku;
validList[index].numAdd = !!(sku && validList[index].count === sku.stock); validList[index].numAdd = sku && validList[index].count < sku.stock;
// selectValue // selectValue
// why SPU SKU invalidList SPU // why SPU SKU invalidList SPU
if (sku && sku.stock > 0) { if (sku && sku.stock > 0) {
@ -779,7 +773,15 @@
fen2yuan(price) { fen2yuan(price) {
return Util.fen2yuan(price) return Util.fen2yuan(price)
} },
inArray: function(search, array) {
for (let i in array) {
if (array[i] === search) {
return true;
}
}
return false;
},
}, },
onReachBottom() { onReachBottom() {
// TODO // TODO