You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
zhishifufei_uniapp/pages/store/shoppingCart.vue

432 lines
10 KiB

<template>
<BaseContainer class="shopping-cart flex">
<NavBar title="购物车" />
<view class="cart-container">
<view class="cart-info flex flex-center-x">
<view> {{ goodsList.length }} 件商品</view>
<view class="edit" @click="isEdit = !isEdit;checkedGoods = [];">{{ isEdit ? '完成' : '编辑' }}</view>
</view>
<view class="list">
<view v-for="item in goodsList" :key="item.id" class="item">
<i class="iconfont icongouxuan" :class="{ active: checkedGoods.includes(item.id) }" @click="checkGoods(item.id)"></i>
<view class="image">
<image mode="aspectFit" class="img" :src="item.productInfo.image" alt="" />
</view>
<view class="text">
<view class="title">{{ item.productInfo.store_name }}</view>
<view class="label flex">
<view v-for="(label, k) in item.productInfo.label">{{ label }}</view>
</view>
<view class="group">
<view class="price">
¥<span class="num">{{ item.productInfo.price }}</span>
</view>
<view class="num">
<text @click="reduceCartNum(item)">-</text>
<input type="number" v-model="item.cart_num" @blur="(e) => changeInput(e, item)"/>
<text @click="addCartNum(item)">+</text>
</view>
</view>
</view>
</view>
</view>
<view v-if="!goodsList.length" class="empty">
<image mode="aspectFill" :src="getImgPath('/wap/first/zsff/images/empty.png')" alt="暂无商品" />
<view>暂无商品</view>
</view>
</view>
<view class="cart-footer flex flex-center-x">
<label class="radio" @click="changeAllSelect">
<radio value="全部" :checked="allSelect" /><text>全选</text>
</label>
<view v-if="!isEdit" class="total">合计:<text>¥{{ total }}</text></view>
<view v-if="!isEdit" class="pay-btn" @click="toPay">去结算<text v-if="checkedGoods.length > 0">({{ checkedGoods.length }})</text></view>
<view v-else class="delete-btn" @click="deleteGoods">删除</view>
</view>
</BaseContainer>
</template>
<script>
import { getCartList, deleteCart, createOrder, changeCartNum } from "@/api/store";
import {
getInitData
} from "@/api/special";
import PayDialog from "@/components/PayDialog/index.vue";
export default {
components: {
PayDialog,
},
data() {
return {
special_id: '',
allSelect: false,
selectGoods: [],
goodsList: [],
originGoodsList: [],
checkedGoods: [],
total: 0,
payDialogOpen: false, // 是否显示支付弹窗
pay_type_num: 20,
is_alipay: false, //支付宝是否开启
is_yue: false, //余额是否开启
now_money: 0, //余额
templateId: "",
wxpayH5: false,
initDataLoading: false,
isEdit: false,
createOrder,
};
},
watch: {
checkedGoods: {
deep: true,
handler() {
console.log(this.checkedGoods);
let total = 0;
this.goodsList.forEach(v => {
if (this.checkedGoods.includes(v.id)) {
total += Number(v.cart_num) * Number(v.productInfo.price);
}
});
this.total = total;
},
},
goodsList: {
deep: true,
handler() {
let total = 0;
this.goodsList.forEach(v => {
if (this.checkedGoods.includes(v.id)) {
total += Number(v.cart_num) * Number(v.productInfo.price);
}
});
this.total = total;
},
},
},
onLoad() {
this.getCartList();
this.getInitData();
},
methods: {
getInitData() {
getInitData().then(({ data }) => {
const { isWechat, is_alipay, is_yue, now_money, wxpayH5 } = data;
Object.assign(this, {
isWechat,
url: isWechat ? "/pages/index/login" : "/pages/login/phone_check",
is_alipay: is_alipay == 1,
is_yue: is_yue == 1,
now_money: Number(now_money),
wxpayH5,
initDataLoading: true,
});
});
},
changeAllSelect() {
this.allSelect = !this.allSelect;
if (this.allSelect) {
const arr = [];
this.goodsList.forEach(v => {
arr.push(v.id);
});
this.checkedGoods = arr;
} else {
this.checkedGoods = [];
}
},
async getCartList() {
try {
const { data } = await getCartList();
console.log(data);
this.goodsList = [...data.valid]
this.originGoodsList = [...data.valid];
} catch (err) {
console.log(err);
}
},
checkGoods(id) {
if (this.checkedGoods.includes(id)) {
this.checkedGoods = this.checkedGoods.filter(item => item !== id);
} else {
this.checkedGoods.push(id);
}
this.allSelect = this.checkedGoods.length === this.goodsList.length;
},
reduceCartNum(item) {
if (Number(item.cart_num) - 1 > 1) {
this.changeCartNum(item.id, Number(item.cart_num) - 1);
}
item.cart_num = Number(item.cart_num) - 1 > 1 ? Number(item.cart_num) - 1 : 1;
},
addCartNum(item) {
this.changeCartNum(item.id, Number(item.cart_num) + 1);
item.cart_num = Number(item.cart_num) + 1;
},
changeInput(e, item) {
const value = String(e.detail.value);
if (value && Number(value) > 0) {
item.cart_num = Number(value);
this.changeCartNum(item.id, Number(value));
} else {
item.cart_num = this.originGoodsList.filter(v => v.id === item.id)[0].cart_num;
}
},
async changeCartNum(cartId, cartNum) {
try{
const { data } = await changeCartNum({ cartId, cartNum})
this.getCartList();
}catch(e){
//TODO handle the exception
}
},
deleteGoods() {
if (this.checkedGoods.length == 0) {
this.$util.showMsg("请选择商品");
} else {
uni.showModal({
title: "提示",
content: `确定删除${this.checkedGoods.length}件商品?`,
success: async ({ confirm }) => {
if (!confirm) return;
// uni.showModal({ mask: true });
try {
const { data } = await deleteCart({
ids: this.checkedGoods.join(',')
});
uni.hideLoading();
this.$util.showMsg("删除成功");
this.goodsList = this.goodsList.filter(item => !this.checkedGoods.includes(item.id));
this.checkedGoods = [];
} catch (err) {
uni.hideLoading();
console.log(err);
}
},
});
}
},
toPay() {
if (this.total === 0) {
this.$util.showMsg("请选择商品");
} else {
uni.navigateTo({
url: `/pages/special/confirm_order?cartId=${this.checkedGoods.join(',')}`,
});
// this.$util.checkLogin(async () => {
// try {
// const { code, data, msg } = await createOrder({
// productId: this.storeInfo.id,
// cartNum: 1,
// });
// if (code === 200) {
// uni.navigateTo({
// url: "/pages/special/confirm_order?cartId=" + data.cartId,
// });
// } else {
// this.$util.showMsg(msg);
// }
// } catch (err) {
// this.$util.showMsg(err.msg);
// }
// }, true);
}
},
},
};
</script>
<style lang="scss" scoped>
.shopping-cart {
background: #f6f6f6;
flex-direction: column;
.cart-container {
height: 100%;
.cart-info {
height: 78rpx;
color: #BBBBBB;
font-size: 28rpx;
padding: 0 35rpx 0 44rpx;
justify-content: space-between;
}
.list {
width: 690rpx;
margin: 0 auto;
}
.list .item {
margin-bottom: 30rpx;
padding: 26rpx 24rpx;
background: #fff;
display: flex;
border-radius: 10rpx;
align-items: center;
.iconfont {
display: inline-block;
width: 40rpx;
height: 40rpx;
border: 1px solid #e5e5e5;
border-radius: 50%;
box-sizing: border-box;
vertical-align: middle;
font-size: 0;
line-height: 40rpx;
text-align: center;
color: #c3c3c3;
margin-right: 12rpx;
&.active {
border: 0;
background: #2c8eff;
font-size: 20rpx;
color: #ffffff;
}
}
.label {
height: 38rpx;
view {
height: 38rpx;
line-height: 38rpx;
background: #e1feee;
padding: 0 15rpx;
margin-right: 10rpx;
color: #28ce8f;
font-size: 20rpx;
&:nth-child(2) {
color: #ff5c6b;
background: #fee1e1;
}
}
}
}
.list .image {
width: 156rpx;
height: 184rpx;
margin-right: 52rpx;
flex-shrink: 0;;
}
.list .image .img {
width: 100%;
height: 100%;
}
.list .text {
flex: 1;
display: flex;
flex-direction: column;
padding-top: 13rpx;
}
.list .title {
overflow: hidden;
font-size: 28rpx;
line-height: 35rpx;
height: 70rpx;
color: #333;
-webkit-line-clamp: 2;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
margin-bottom: 13rpx;
}
.list .group {
margin-top: auto;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 6rpx;
.num {
display: flex;
text {
width: 60rpx;
height: 60rpx;
border-radius: 30rpx 0 0 30rpx;
text-align: center;
line-height: 60rpx;
border: 2rpx solid #E6E6E6;
box-sizing: border-box;
&:last-child {
border-radius: 0 30rpx 30rpx 0;
}
}
input {
width: 80rpx;
height: 60rpx;
text-align: center;
border-top: 2rpx solid #E6E6E6;
border-bottom: 2rpx solid #E6E6E6;
}
}
}
.list .price {
font-size: 32rpx;
color: #f8473e;
font-weight: 600;
display: flex;
}
.list .price .num {
font-size: 32rpx;
}
.list .cart-image {
width: 52rpx;
height: 52rpx;
}
.empty {
margin-top: 100rpx;
font-size: 28rpx;
text-align: center;
color: #bbb;
}
.empty image {
display: block;
width: 414rpx;
height: 305rpx;
margin: 0 auto;
pointer-events: none;
}
}
.cart-footer {
width: 100%;
height: 100rpx;
padding-left: 30rpx;
background: #fff;
box-shadow: 0rpx -19rpx 54rpx 0rpx rgba(34,34,34,0.05);
flex-shrink: 0;
position: fixed;
bottom: 0;
left: 0;
label {
color: #999999;
font-size: 28rpx;
}
.total {
margin-left: auto;
margin-right: 20rpx;
font-size: 30rpx;
color: #333333;
font-weight: bold;
text {
color: #FF2825;
}
}
.pay-btn, .delete-btn {
width: 240rpx;
height: 100rpx;
background: #0F74BB;
text-align: center;
line-height: 100rpx;
font-size: 28rpx;
color: #fff;
}
.delete-btn {
background: #F8473E;
margin-left: auto;
}
}
}
</style>