购物车 - 结算你的心仪商品
<template>
<view>
<view class="margin">
<block v-for="(goods,index) in goodsList" :key="index">
<view class="u-flex">
<u-checkbox v-model="goods.is_checked" @change="changeChoose" shape="circle" :name="goods.id"></u-checkbox>
<u-image width="100%" height="300rpx" :src="goods.goods.cover_url"></u-image>
<view class="flex">
<view class="flex-top">
<text>{{goods.goods.title}}</text>
<text>{{goods.goods.description}}</text>
</view>
<view class="u-flex">
<text class="price">¥{{goods.goods.price}}</text>
<u-number-box v-model="goods.num" :min="1" :max="100" :index="goods.id"
@minus="minusNum" @plus="plusNum"></u-number-box>
<u-icon name="trash" color="red" @click="removeCart(goods.id)"></u-icon>
</view>
<pre><code> </view>
</view>
</block>
</view>
<view class="bottom">
<u-checkbox v-model="isAllchecked" shape="circle" @change="changeAllBtn">全选</u-checkbox>
<text class="u-flex u-m-l-80">合计: ¥ {{allMoney}}</text>
<view class="u-m-r-40 1">
<u-button shape="circle" :custom-style="customStyle" @click="toOrder">去结算</u-button>
</view>
</view>
</view>
</code></pre>
</template>
<script>
const that = this
export default {
data() {
return {
goodsList:[], // 渲染的数据列表
// isAllchecked: false, //是否全选
customStyle: {color: '#fff',backgroundColor: 'red'}, //去结算按钮的颜色样式
}
},
onLoad(){
this.getCartData()
},
computed: {
// 计算总价
allMoney: function(){
return this.goodsList.reduce((prev,next)=> {
return prev + next.is_checked * next.goods.price * next.num
},0)
},
isAllchecked: {
get: function(){
for(let i=0;i<this.goodsList.length;i++){
if(this.goodsList[i].is_checked == false) return false
}
return true
},
set: function(){
}
}
},
methods: {
//获取渲染的列表
async getCartData(){
const cartData = await this.$u.api.cartDataList({include: 'goods'})
console.log(cartData);
const {data} = cartData
data.forEach((value,index,data) =>{
data[index].is_checked = (data[index].is_checked == 1) ? true : false
return data
})
this.goodsList = data
},
async minusNum(e){
console.log(e);
let cartId = e.index
console.log(e.value);
let params = {num: e.value}
await this.$u.api.cartNumChange(cartId,params)
},
async plusNum(e){
console.log(e);
let cartId = e.index
console.log(e.value);
let params = {num: e.value}
await this.$u.api.cartNumChange(cartId,params)
},
// 移除购物车的商品
async removeCart(id){
await this.$u.api.cartRemove(id)
this.getCartData()
this.$u.toast('删除成功')
},
// 改变商品选中后选择
async changeChoose(e){
const {name,value} = e // name为id value为true或者false
for (let i = 0; i < this.goodsList.length; i++) {
if(this.goodsList[i].id == name){
this.goodsList[i].is_checked = !this.goodsList[i].is_checked
}
}
let adArr = this.goodsList.filter(function (value){
return value.is_checked == true
})
console.log(adArr);
let newArr = []
for (let i = 0; i < adArr.length; i++) {
newArr.push(adArr[i].id)
}
console.log(newArr);
await this.$u.api.cartChangeChosen({cart_ids: newArr})
},
async changeAllBtn(e){
const {name,value} = e
if(value == false){
for (let i = 0; i < this.goodsList.length; i++) {
this.goodsList[i].is_checked = false
}
await this.$u.api.cartChangeChosen({cart_ids: []})
this.getCartData()
}else {
for (let i = 0; i < this.goodsList.length; i++) {
this.goodsList[i].is_checked = true
}
let newArr = []
for (let i = 0; i < this.goodsList.length; i++) {
newArr.push(this.goodsList[i].id)
}
await this.$u.api.cartChangeChosen({cart_ids: newArr})
this.getCartData()
}
},
toOrder(){
this.$u.route({url: 'pages/cart/order'
})
}
}
}
</script>
<style lang="scss" scoped>
.flex{
display: flex;
flex-direction: column;
justify-content: space-between;
}
.flex-top{
display: flex;
flex-direction: column;
justify-content: center;
}
.price{
color: red;
}
.bottom{
display: flex;
position: fixed;
width: 100%;
bottom: 100rpx;
justify-content: space-between;
background-color: #fff;
}
.margin{
margin-bottom: 100rpx;
}
</style>
原文地址: https://www.cveoy.top/t/topic/oyVH 著作权归作者所有。请勿转载和采集!