<template>
|
<view class="carts">
|
<view class="c-title" :style="{top:statusBarHeight+ 'px'}">
|
<text>共{{cartsTotal}}件商品</text>
|
<view class="" @tap="delCarts">
|
<text>删除</text>
|
</view>
|
</view>
|
<view class="cartsView">
|
<view class="" v-if="cartsData.length > 0">
|
<view class="cartsItem" v-for="(item, index) in cartsData">
|
<view class="cartTitle" @tap="cartsClick(item, index)">
|
<image class="cIcon" :src="item.check == 0 ? '/static/imgs/check.png' : '/static/imgs/checked.png'" mode=""></image>
|
<text>{{item.addtime}}</text>
|
</view>
|
<view class="cartsGoods" @tap="$util.goPages('/pages/ginfo/ginfo?id=' + item.goodsid)">
|
<image class="cartsIcon" lazy-load :src="item.icon" mode="widthFix"></image>
|
<view class="carts-item">
|
<view class="carts-title">{{item.title}}</view>
|
<view class="carts-set">
|
<view class="carts-price">
|
<view class="">
|
<text>单价:¥</text>
|
<text class="red">{{item.price}}</text>
|
</view>
|
<view class="">
|
<text>总价:¥</text>
|
<text class="red">{{item.total}}</text>
|
</view>
|
</view>
|
</view>
|
</view>
|
</view>
|
<view class="carts-nums">
|
<view class="" @tap="setCartsNums(item, 1)">
|
<image src="/static/imgs/minus.png" mode=""></image>
|
</view>
|
<view class="">
|
<text>{{item.nums}}</text>
|
</view>
|
<view class="" @tap="setCartsNums(item, 2)">
|
<image src="/static/imgs/add.png" mode=""></image>
|
</view>
|
</view>
|
</view>
|
</view>
|
<view class="noData" v-else>
|
<image src="/static/imgs/noData.png" mode="" style="margin-top: 200upx;"></image>
|
<view class="">没有购物车信息</view>
|
<view class="noDataBtn" @tap="$util.goSwitch('/pages/index/index')">去看看</view>
|
</view>
|
</view>
|
<view class="cartsControll" v-if="cartsData.length > 0">
|
<view class="cartsControll-left" @tap="checkTapAll">
|
<image class="chooseIcon" :src="checkIds.length == cartsTotal ? '/static/imgs/checked.png' : '/static/imgs/check.png'" mode=""></image>
|
<text>全选</text>
|
</view>
|
<view class="cartsControll-right">
|
<text class="total">合计:</text>
|
<text>¥</text>
|
<text class="red">{{payTotal}}</text>
|
<view class="cartsBtn" @tap="goPays">结算({{payNums}})</view>
|
</view>
|
</view>
|
</view>
|
</template>
|
|
<script>
|
export default {
|
data() {
|
return {
|
cartsTotal: 0,
|
pageIndex: 1,
|
pageSize: 200,
|
cartsData: [],
|
payTotal: 0,
|
payNums: 0,
|
checkAll: false,
|
checkIds: [],
|
checkIdx: [1],
|
statusBarHeight: 0,
|
}
|
},
|
onShow() {
|
let t = this
|
this.checkIds = []
|
this.checkAll = false,
|
this.payNums = 0
|
this.payTotal = 0
|
this.getCarts()
|
setTimeout(() => {
|
|
}, 2000)
|
const windowInfo = uni.getWindowInfo();
|
this.statusBarHeight = windowInfo.windowTop || windowInfo.safeAreaInsets.top;
|
|
},
|
methods: {
|
getCarts () {
|
this.$util.post('Carts/getCarts', {pageIndex: this.pageIndex, pageSize: this.pageSize}).then(res => {
|
if(res.code == 1){
|
this.cartsData = res.data.list
|
this.cartsTotal = res.data.pageTotal
|
}
|
})
|
},
|
cartsClick (e, index) {
|
e.check = e.check == 1 ? 0 : 1
|
if(e.check == 1) {
|
this.payTotal = this.payTotal + parseFloat(e.total)
|
this.payNums = this.payNums + e.nums
|
this.checkIds.push(e.id)
|
this.checkIdx.push(index)
|
} else {
|
this.payTotal = this.payTotal - parseFloat(e.total)
|
this.payNums = this.payNums - e.nums
|
let idx = this.checkIds.indexOf(e.id)
|
let inx = this.checkIdx.indexOf(index)
|
this.checkIds.splice(idx, 1)
|
this.checkIdx.splice(inx, 1)
|
}
|
if(this.checkIds.length == this.cartsTotal){
|
this.checkAll = true
|
} else {
|
this.checkAll = false
|
}
|
},
|
//全选、反选
|
checkTapAll () {
|
let checkFlag = 0
|
if(!this.checkAll){
|
checkFlag = 1
|
this.checkAll = true
|
} else {
|
this.checkAll = false
|
}
|
for(let i in this.cartsData){
|
this.cartsData[i].check = checkFlag
|
if(checkFlag == 1 && this.checkIds.indexOf(this.cartsData[i].id) < 0) {
|
this.checkIds.push(this.cartsData[i].id)
|
this.checkIdx.push(parseInt(i))
|
this.payNums = this.payNums + parseFloat(this.cartsData[i].nums)
|
this.payTotal = this.payTotal + parseFloat(this.cartsData[i].total)
|
}
|
}
|
//反选
|
if(checkFlag != 1) {
|
this.checkIds = []
|
this.checkIdx = []
|
this.payNums = 0
|
this.payTotal = 0
|
}
|
},
|
//设置购物车数量
|
setCartsNums (e, y){
|
let n = e.nums
|
let t = this
|
if(y == 1){
|
if(n == 1) {
|
return
|
} else {
|
n = n > 1 ? n - 1 : 1
|
}
|
} else {
|
n = n + 1
|
}
|
t.$util.post('Carts/setCartsNums', {id: e.id, nums: n}).then(res => {
|
if(res.code == 1) {
|
e.nums = n
|
e.total = n * parseFloat(e.price)
|
//计算总价
|
if(t.payTotal != 0 && this.checkIds.indexOf(e.id) >= 0){
|
if(y == 1){
|
t.payTotal = t.payTotal - parseFloat(e.price)
|
} else {
|
t.payTotal = t.payTotal + parseFloat(e.price)
|
}
|
}
|
} else {
|
uni.showToast({
|
title: res.msg || '操作失败',
|
icon: 'error'
|
})
|
}
|
})
|
},
|
//购买
|
goPays () {
|
if(this.payNums == 0) {
|
return uni.showToast({
|
title: '未选中购买商品',
|
icon: 'error'
|
})
|
}
|
if(this.checkIds.length > 1) {
|
return uni.showToast({
|
title: '不支持多件购买',
|
icon: 'error'
|
})
|
}
|
let cartsIds = this.checkIds.join(',')
|
let url = '/pages/pays/pays?cartsIds=' + cartsIds
|
this.$util.goPages(url)
|
},
|
//删除
|
delCarts () {
|
let t = this
|
if(this.checkIds.length == 0){
|
return uni.showToast({
|
title: '未选中删除项',
|
icon: 'error'
|
})
|
}
|
uni.showModal({
|
title: '温馨提示',
|
content: '是否确定删除?',
|
success: function (res) {
|
if (res.confirm) {
|
console.log(t.checkIds)
|
t.$util.post('Carts/delCarts', {ids: t.checkIds}).then(res => {
|
if(res.code == 1){
|
for (let i = t.cartsData.length - 1; i >= 0; i--) {
|
if (t.checkIdx.includes(i)) {
|
t.cartsData.splice(i, 1)
|
}
|
}
|
t.cartsTotal = t.cartsTotal - t.checkIdx.length
|
t.checkIds = []
|
t.checkIdx = []
|
t.payNums = 0
|
t.payTotal = 0
|
return uni.showToast({
|
title: res.msg || '操作成功',
|
icon: 'success'
|
})
|
} else {
|
return uni.showToast({
|
title: res.msg || '操作失败',
|
icon: 'error'
|
})
|
}
|
})
|
}
|
}
|
})
|
}
|
}
|
}
|
</script>
|
|
<style>
|
@import url(/static/css/carts.css);
|
</style>
|