uni 多商家购物车

在这里插入图片描述

<template>
	<view>
		<view class="cartBox" style="padding-bottom: 90rpx;">
			<view class="cartShop" v-for="(items,index) in cartData.list" :key="index">
				<view class="cartName cartPad">
					<radio :checked="items.shopSelected==1" color="#ff2d52" style="transform:scale(0.8)" @click="shopselect(items)" />
					<view class="cartName-box" @tap="goShop(items)">
						<image :src="items.logo" class="cartName-img"></image>
						<view style="max-width:400rpx;overflow:hidden; white-space: nowrap;text-overflow:ellipsis;">{{items.marketname}}</view>
					</view>
					<image src="https://yiqihuijia.20080531.com/Public/wxminicon/dsp_arr_rgray2@3x.png" class="cartName-go" @tap="goShop(items)"></image>
				</view>
				<view class="cartLine cartPad"></view>
				<view v-for="(item,k) in items.cart" :key="k" class="cartCont" @click="goodSelect(items,item)">
					<view class="cartDetail">
						<radio :checked="item.goodsSelected==1" color="#ff2d52" style="transform:scale(0.8)" />
						<view class="cartDetail-img">
							<image :src="item.imgurl" class="cartDetail-imgs"></image>
						</view>
						<view class="cartDetail-title">
							<view class="cartDetail-txt">
								{{item.goodsname}}
							</view>
							<view class="cartDetail-lab">
								{{item.skuvalue}}
							</view>
						</view>
					</view>
					<view class="cartNum">
						<view class="cartNum-txt">
							<view style="margin-bottom: 15rpx;">
								¥<text style="font-size: 35rpx;">{{item.price}}</text>
							</view>
						</view>
						<view class="add-sub-con">
							<text class="link" :class="item.num==1? 'jj':'aj'" @click.stop="cutAction(item)">-</text>
							<text class="input1 num input">{{item.num}}</text>
							<text class="link" :class="item.num*1<item.kucun*1? 'aj':'jj'" @click.stop="addAction(item)">+</text>
						</view>
					</view>
				</view>
			</view>

			<view class="cartTotal">
				<view class="cartTotal-title">
					<view class="cartTotal-title">
						<radio :checked="cartData.allSelected==1" color="#ff2d52" style="transform:scale(0.8)" @click="selectAllAction" />
						<view class="cart-choose" @click="selectAllAction">
							全选
						</view>
						<view class="cart-del" @click="deleteItem">
							删除
						</view>
					</view>
					<view class="cart-num">
						<view class="cart-numTxt">
							合计金额
						</view>
						<text style="font-size: 25rpx;">¥</text>{{totalPrice.toFixed(2)}}
					</view>
				</view>
				<view class="cartTotal-btn" @tap="submitAction">
					结算
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				img: this.imgBaseUrl,
				cartData: {
					list: [{
							marketid: "0131",
							marketname: "好再来孕婴店",
							logo: "https://yiqihuijia.20080531.com/Upload/Market/202008/24/159825500113ergs.jpeg",
							shopSelected: "1",
							subTotal: "39.00",
							cart: [{
									cartid: "415",
									goodsid: "508",
									skuid: "879",
									skuvalue: "白色",
									num: "1",
									goodsname: "新生儿尿布2条装",
									marketid: "0131",
									imgurl: "https://yiqihuijia.20080531.com/Upload/MarketGoods/detail/202008/508/5f44ea1f962ba.jpg",
									price: "30.00",
									kucun: "300",
									goodsSelected: "1"
								},
								{
									cartid: "414",
									goodsid: "506",
									skuid: "867",
									skuvalue: "蓝色",
									num: "1",
									goodsname: "新生儿纯棉洗澡起泡擦",
									marketid: "0131",
									imgurl: "https://yiqihuijia.20080531.com/Upload/MarketGoods/detail/202008/506/5f44ddd45a5b9.jpg",
									price: "10.00",
									kucun: "1",
									goodsSelected: "1"
								}
							]
						},
						{
							marketid: "234",
							marketname: "地平线8号旅行箱店",
							logo: "https://yiqihuijia.20080531.com/Upload/MarketM/202008/26/1598424570ah95ua.jpeg",
							shopSelected: "1",
							subTotal: "400.00",
							cart: [{
								cartid: "411",
								goodsid: "516",
								skuid: "971",
								skuvalue: "幻影黑",
								num: "1",
								goodsname: "地平线8号大容量静音万向轮旅行箱",
								marketid: "234",
								imgurl: "https://yiqihuijia.20080531.com/Upload/MarketGoods/detail/202008/234/1599820041wkuxbt.jpeg",
								price: "400.00",
								kucun: "79",
								goodsSelected: "1"
							}]
						}
					],
					allSelected: "1",
					totalPrice: "439.00"
				},
				cart: []
			}
		},
		onShow() {
			this.getShopList()
		},
		computed: {
			//计算选中商品的总价
			totalPrice() {
				let prices = 0;
				if (!!this.cartData.list) {
					this.cartData.list.map(shopItem => {
						shopItem.cart.map(goodItem => {
							goodItem.goodsSelected == 1 ? prices += goodItem.price * goodItem.num : prices += 0;
						})
					})
				}
				return prices;
			}
		},
		methods: {
			getShopList() {
				if (this.cartData.allSelected == 1) {
					this.cartData.list.forEach(shopItem => {
						shopItem.cart.forEach(goodsItem => {
							this.cart.push(goodsItem)
						})
					})
				} else {
					this.cartData.list.forEach(shopItem => {
						shopItem.cart.forEach(goodsItem => {
							if (goodsItem.goodsSelected == 1) {
								this.cart.push(goodsItem)
							}
						})
					})
				}
			},
			// 选择店铺
			shopselect(shopItem) {
				//遍历此店铺中商品 并全部选择
				if (shopItem.shopSelected == 0) {
					shopItem.shopSelected = 1
					shopItem.cart.map(goodItem => {
						goodItem.goodsSelected = 1;
						this.cart.push(goodItem)
					});
				} else {
					shopItem.shopSelected = 0
					shopItem.cart.map(goodItem => {
						goodItem.goodsSelected = 0;
					});
					this.cart = this.cart.filter((item) => {
						return item.marketid != shopItem.cart[0].marketid
					})
				}
				this.changeSelectAllBtn();
			},
			// 选择商品
			goodSelect(shopItem, goodItem) {
				// 如果商品没选中 则不选中店铺
				if (goodItem.goodsSelected == 0) {
					goodItem.goodsSelected = 1
					shopItem.isSelectShop = false;
					//店铺中商品都满足条件 则返回true
					const shopS = shopItem.cart.every(goodItem => {
						return goodItem.goodsSelected == 1;
					});

					//商品都选中  则店铺选中
					if (shopS) {
						shopItem.shopSelected = 1;
					}
					this.cart.push(goodItem)
				} else {
					goodItem.goodsSelected = 0
					shopItem.shopSelected = 0;
					this.cart.forEach((el, index) => {
						if (el.cartid == goodItem.cartid) {
							this.cart.splice(index, 1)
						}
					})
				}
				//监听全选按钮状态
				this.changeSelectAllBtn();
			},
			// 选择全部
			selectAllAction() {
				//遍历数据数组--遍历店铺  修改选择
				if (this.cartData.allSelected == 0) {
					this.cartData.allSelected = 1
					this.cart = []
					this.cartData.list.map(shopItem => {
						shopItem.shopSelected = 1;
						shopItem.cart.map((goodItem, index) => {
							this.cart.push(goodItem)
							goodItem.goodsSelected = 1;
						});
					});
				} else {
					//反选
					this.cartData.allSelected = 0
					this.cartData.list.map(shopItem => {
						shopItem.shopSelected = 0;
						shopItem.cart.map(goodItem => {
							this.cart = []
							goodItem.goodsSelected = 0;
						});
					});
				}
			},
			/* 选择店铺或者商品 监听全选按钮状态 */
			changeSelectAllBtn() {
				const selectAll = this.cartData.list.every(shopItem => {
					return shopItem.shopSelected == 1;
				});
				if (selectAll) {
					this.cartData.allSelected = 1
				} else {
					this.cartData.allSelected = 0
				}
			},
			// 增加数量
			addAction(goodItem) {
				if (goodItem.num >= parseInt(goodItem.kucun)) {
					uni.showToast({
						title: '不能再多了~',
						icon: 'none'
					});
					return true
				}
				goodItem.num++;
			},
			// 减少数量
			cutAction(goodItem) {
				if (goodItem.num > 1) {
					goodItem.num--;
				} else {
					uni.showToast({
						title: '不能再少了~',
						icon: 'none'
					});
				}
			},
			// 删除
			deleteItem() {
				let delObj = {}
				this.cart.map((goodItem, index) => {
					delObj[goodItem.cartid] = goodItem.num
				});

				if (JSON.stringify(delObj) == "{}") {
					uni.showToast({
						title: "请选择要删除的商品",
						icon: "none"
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '确定删除选中的商品?',
						confirmColor: "#fe0909",
						success: (con) => {
							if (con.confirm) {
								console.log(JSON.stringify(delObj))
							} else if (con.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			// 结算
			submitAction() {
				let ALL = []
				this.cart.map(goodItem => {
					let cartarr = {}
					cartarr["cartid"] = goodItem.cartid
					cartarr["goodsid"] = goodItem.goodsid
					cartarr["num"] = goodItem.num
					cartarr["skuid"] = goodItem.skuid
					ALL.push(cartarr)
				});

				console.log(JSON.stringify(ALL))
			}
		}
	}
</script>

<style>
	page {
		background-color: #F5F5F5;
	}

	.cartBox {
		background-color: #F5F5F5;
		padding-top: 20rpx;
	}

	.cartShop {
		background-color: #FFFFFF;
		margin-bottom: 20rpx;
		overflow: hidden;
	}

	.cartPad {
		margin: 0 26rpx;
	}

	.cartName {
		height: 112rpx;
		font-size: 29rpx;
		color: #333333;
		display: flex;
		align-items: center;
	}

	.cartName-box {
		flex: 1;
		display: flex;
		align-items: center;
	}

	.cartName-img {
		width: 56rpx;
		height: 56rpx;
		margin: 0 18rpx;
		border-radius: 50%;
	}

	.cartName-imgs {
		width: 113rpx;
		height: 35rpx;
		margin-left: 18rpx;
	}

	.cartName-go {
		width: 16rpx;
		height: 27rpx;
	}

	.cartLine {
		height: 2rpx;
		background-color: #e5e5e5;
	}

	.cartCont {
		margin: 0 26rpx;
	}

	.cartDetail {
		display: flex;
		align-items: center;
		margin-top: 22rpx;
	}

	.cartDetail-img {
		width: 108rpx;
		height: 108rpx;
		border: solid 1rpx #e5e5e5;
		margin-left: 17rpx;
	}

	.cartDetail-imgs {
		width: 90rpx;
		height: 90rpx;
		margin: 9rpx;
	}

	.cartDetail-title {
		font-size: 29rpx;
		color: #333333;
		margin-left: 24rpx;
	}

	.cartDetail-txt {
		max-width: 520rpx;
		line-height: 1.2;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.cartDetail-lab {
		font-size: 26rpx;
		color: #999999;
	}

	.cartNum {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		margin-top: 15rpx;
		padding-bottom: 26rpx;
	}

	.cartNum-txt {
		font-size: 25rpx;
		color: #ff2d52;
	}

	.cartTotal {
		width: 100%;
		height: 91rpx;
		background-color: #FFFFFF;
		font-size: 28rpx;
		color: #333333;
		display: flex;
		align-items: center;
		padding-left: 27rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 10;
	}

	.cartTotal-title {
		flex: 1;
		display: flex;
		align-items: center;
	}

	.cart-choose {
		margin: 0 21rpx 0 14rpx;
	}

	.cart-num {
		font-size: 31rpx;
		color: #ff2d52;
		text-align: right;
		padding-right: 21rpx;
	}

	.cart-numTxt {
		font-size: 25rpx;
		color: #999999;
		margin-bottom: 10rpx;
	}

	.cartTotal-btn {
		width: 207rpx;
		height: 91rpx;
		font-size: 31rpx;
		color: #FFFFFF;
		background-color: #ff2d52;
		line-height: 91rpx;
		text-align: center;
	}
	.add-sub-con {
		margin-left: 168rpx;
	}

	.add-sub-con .link {
		float: left;
		width: 60rpx;
		height: 60rpx;
		line-height: 62rpx;
		text-align: center;
		font-size: 32rpx;
		color: #666;
		border: #e5e5e5 solid 1px;
		background-color: #F8F8F8;
	}

	.add-sub-con .link.aj {
		color: #fe0909;
		border: #e5e5e5 solid 1px;
		background: #fff;
	}

	.add-sub-con .link.jj {
		color: #666;
		border: #e5e5e5 solid 1px;
		background-color: #F8F8F8;
	}

	.add-sub-con .input {
		float: left;
		display: block;
	}

	.add-sub-con .input {
		width: 85rpx;
		height: 60rpx;
		line-height: 60rpx;
		box-sizing: border-box;
		text-align: center;
		font-size: 30rpx;
		-webkit-border-radius: 0;
		border: 1px solid #e5e5e5;
		border-left: none;
		border-right: none;
	}
</style>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值