shopCar.vue 12.7 KB
Newer Older
zhangcheng committed
1
<template>
张成 committed
2 3 4 5 6 7 8
	<view>
		<u-popup :show="showShopCar && goods.length" mode="bottom" :round="10" :overlay="true"
			:closeOnClickOverlay="true" @close="closeT">
			<view class="shop-car">
				<view class="header">
					<text class="left">购物袋</text>
					<text class="right" @click="clearCar"><text class="delete-icon"></text>清空购物车</text>
zhangcheng committed
9
				</view>
张成 committed
10 11 12 13 14 15 16 17 18 19 20
				<view class="container">
					<view>
						<view class="empty" v-if="show == false">
							<image :src="imgPath" mode="widthFix" style="width: 400rpx;"></image>
							<view class="empty-text">空空如也的购物</view>
							<view class="empty-button" @click="goshopping">去选购</view>
						</view>
						<view v-if="show == true">
							<view class="goods-detail" v-for="(item, index) in goods" :key="index">
								<view class="detail-left">
									<view class="goods-left">
21 22 23 24
										<view class="checkBox">
											<view class="check"  @click="selected(item)" :class="item.flag?'checked':'nochecked'"></view>
										</view>
										<!-- <u-checkbox-group @change="selected(item)">
张成 committed
25 26 27 28
											<label>
												<u-checkbox shape="circle" class="selected" color="#555555"
													:checked="item.flag" />
											</label>
29 30 31 32 33
										</u-checkbox-group> -->
										<view>
											<image :src="item.pics.thumbnailApplet || item.pics.thumbnail"
											style="width: 120rpx;height: 120rpx;margin-top: 4rpx"></image>
										</view>
张成 committed
34 35
									</view>
									<view class="size">
36 37
										<view class="goods-name">{{ item.name }}</view>
										<view class="goods-skus">
张成 committed
38
											<text v-for="(rl, index) in item.sku.rules" :key="rl.ruleName">
张成 committed
39
												{{ rl.ruleName }}
张成 committed
40
												<text v-if="index != item.sku.rules.length - 1">/</text>
张成 committed
41
											</text>
42 43 44
										</view>
										<view class="goods-price">
											<text class="price-discount">{{ item.sku.discount }}</text>
张成 committed
45 46
											<text class="price-x"></text>
											<text class="price-xx">{{ item.sku.price }}</text>
47
										</view>
张成 committed
48 49 50
									</view>
								</view>
								<view class="detail-right">
51
									<view class="subtract" @click="reduce(item, index)"></view>
张成 committed
52
									<text class="num">{{ item.num }}</text>
53 54 55
									<view class="add" @click="add(item)" ></view>
									<!-- <text class="subtract" @click="reduce(item, index)">-</text> -->
									<!-- <u-icon class="subtract" name="minus-circle" @click="reduce(item, index)" color="#2979ff" size="22"></u-icon> -->
weijiguang committed
56
									<!-- <text @click="add(item)" class="add">+</text> -->
57
									<!-- <u-icon class="add" name="plus-circle-fill" color="#2979ff" @click="add(item)" size="22"></u-icon> -->
张成 committed
58 59 60 61
								</view>
							</view>
						</view>
					</view>
zhangcheng committed
62
				</view>
张成 committed
63
				<view style="height:200rpx"></view>
zhangcheng committed
64
			</view>
张成 committed
65 66 67

		</u-popup>
		<view class="end" v-if="goods.length">
张成 committed
68
			<view @click.stop="openShopCar" class="end-left">
zhangcheng committed
69
				<view style="display:flex">
张成 committed
70
					<view class="car-img">
张成 committed
71 72 73
						<text class="badge" v-if="totalNum">{{ totalNum }}</text>
					</view>
					<text class="shopClassStyle">{{ totalPrice.toFixed(2) }}</text>
zhangcheng committed
74
				</view>
张成 committed
75
			</view>
76
			<view class="end-right goSubmmit" @click="saveReserve" v-if="userms">
张成 committed
77
			</view>
78 79 80 81
			<!-- <view v-if="!userms" class="end-right goSubmmit" style="border-radius: 0;" open-type="getPhoneNumber" @getphonenumber="saveReserve">
				
			</view> -->
			<button v-if="!userms" class="end-right goSubmmit" style="border-radius: 0;" open-type="getPhoneNumber"
张成 committed
82 83
				@getphonenumber="saveReserve">
			</button>
zhangcheng committed
84
		</view>
85
		<show-toast ref="toast"/>
张成 committed
86
	</view>
zhangcheng committed
87 88 89
</template>

<script>
张成 committed
90 91 92 93 94 95 96 97 98 99 100
import { $EventBus } from "../../utils/EventBus";
import Menu from '@/request/menu';
import User from '@/request/user';
export default {
	data() {
		return {
			showShopCar: false,//是否弹出购物车列表
			show: true,
			allchecked: true,
			// checked:true,
			goods: [],//购物车商品信息
101 102
			loginInfo: "",
			imgPath: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F01%2F82%2F40%2F596fa6dc00bb4_610.jpg&refer=http%3A%2F%2Fpic.51yuansu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633499781&t=d37222e32213957ddbdd01d62e071309',
张成 committed
103 104 105 106 107 108
		}
	},
	created() {
		$EventBus.$off('updateCar');
	},
	mounted() {
109 110 111 112 113
		if(!this.userms){
			User.wxLoginAndGetOpenid(true).then(loginInfo=>{
				this.loginInfo = loginInfo
			})
		}
张成 committed
114 115 116 117 118 119 120 121 122 123 124
		this.goods = uni.getStorageSync('shopCarInfo') || [];
		$EventBus.$on('updateCar', () => {
			this.goods = uni.getStorageSync('shopCarInfo') || [];
		});
	},
	methods: {
		// 切换购物车列表显示隐藏
		openShopCar() {
			this.$nextTick(() => {
				this.showShopCar = !this.showShopCar;
			})
zhangcheng committed
125
		},
张成 committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139
		closeT(e) {
			this.showShopCar = false;
		},
		// 清空购物车
		clearCar() {
			uni.removeStorageSync('shopCarInfo');
			$EventBus.$emit('updateCar');
		},
		// 购物车为空时点击去购物,收起购物袋
		goshopping() {
			this.showShopCar = false;
		},
		//跳转到结算
		saveReserve(e) {
140
			if(this.totalPrice < 0 || this.totalNum <= 0) {
141
				this.showToast({ title: '请选择商品', icon: 'error' })
142 143
				return;
			}
张成 committed
144 145
			let Authorization = uni.getStorageSync('Authorization');
			let shopCarInfo = uni.getStorageSync('shopCarInfo').filter(v => v.flag == true);
146
			if (shopCarInfo) {
张成 committed
147 148 149
				if (Authorization) {
					uni.navigateTo({
						url: `/orderSubPackage/pages/settlement/index?buyType=1`
zhangcheng committed
150
					})
张成 committed
151 152
				} else {
					this.loginByPhoneNumber(e)
zhangcheng committed
153
				}
张成 committed
154 155 156 157 158 159
			}
			return;
		},
		// 付款前未登录发起授权
		loginByPhoneNumber(e) {
			if (e.detail.errMsg == 'getPhoneNumber:ok') {
160
				e.loginInfo = this.loginInfo
张成 committed
161 162
				User.getPhoneNumber(e);
			} else if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
163
				this.showToast({ title: '已拒绝手机号授权', icon: 'error' })
张成 committed
164 165
			}
		},
张成 committed
166
		change(e) { },
张成 committed
167 168 169 170 171 172 173 174 175 176 177 178
		selected(item) {
			item.flag = !item.flag
			if (!item.flag) {
				this.allchecked = false
			} else {
				const a = this.goods.filter((item) => {
					return item.flag == true
				})
				if (a) {
					this.allchecked = true
				} else {
					this.allchecked = false
zhangcheng committed
179
				}
zhangcheng committed
180
			}
张成 committed
181
			uni.setStorageSync('shopCarInfo', this.goods);
zhangcheng committed
182
		},
张成 committed
183 184 185 186 187 188 189
		selectgoods() {
			this.allchecked = !this.allchecked
			if (this.allchecked) {
				this.goods.map(item => {
					item.flag = true
				})
			} else {
zhangcheng committed
190
				this.goods.map(item => {
张成 committed
191
					item.flag = false
zhangcheng committed
192 193
				})
			}
张成 committed
194 195 196 197 198 199 200 201
			uni.setStorageSync('shopCarInfo', this.goods);
		},
		reduce(item, index) {
			item.num -= 1
			if (item.num == 0) {
				this.goods.splice(index, 1);
			}
			uni.setStorageSync('shopCarInfo', this.goods);
202
			$EventBus.$emit('updateCar');
张成 committed
203 204 205 206
		},
		add(item) {
			let s = 0;
			this.goods.forEach(function (val) {
张成 committed
207
				s += Number(val.num);
张成 committed
208
			}, 0);
209
			let countOfOrder = uni.getStorageSync('countOfOrder');
210
			console.log(s , countOfOrder)
211
			if (s >= countOfOrder) {
212
				this.showToast({
213
					title: '最多可一次购买'+countOfOrder+'杯',
张成 committed
214 215 216 217
					icon: 'none'
				});
				return;
			}
张成 committed
218
			let num = Number(item.num)
张成 committed
219 220
			item.num = num + 1
			uni.setStorageSync('shopCarInfo', this.goods);
221
			$EventBus.$emit('updateCar');
张成 committed
222 223 224 225 226 227 228 229 230
		}
	},
	computed: {
		userms() {
			return this.$store.getters.Authorization;
		},
		totalNum() {
			let totalNum = 0;
			this.goods.map(item => {
231
				item.flag ? totalNum += parseInt(item.num) : totalNum += 0
张成 committed
232 233 234 235 236 237 238 239 240 241 242
			})
			return totalNum
		},

		totalPrice() {
			let totalPrice = 0;
			this.goods.forEach(item => {
				const sku = item.sku;
				item.flag ? totalPrice += item.num * sku.discount : totalPrice += 0
			})
			return totalPrice
zhangcheng committed
243 244
		}
	}
张成 committed
245 246
}

zhangcheng committed
247 248 249
</script>

<style lang="scss">
张成 committed
250 251 252
.shopClassStyle {
	color: #000000;
	font-weight: bold;
253 254 255
	font-size: 32rpx;
	font-weight: 500;
	font-family: Futura-Medium, Futura;
张成 committed
256
	display: flex;
257 258
	margin-left: 38rpx;
	letter-spacing: 1px;
张成 committed
259 260 261 262 263 264 265 266 267 268 269
	align-items: center;
}

.shop-car {
	display: flex;
	flex-direction: column;
	height: 860rpx;

	.header {
		height: 88rpx;
		display: flex;
270
		border-bottom: 1px solid #ECECEC;
张成 committed
271 272 273 274 275

		.left {
			flex: 1;
			display: flex;
			align-items: center;
276
			padding-left: 31rpx;
张成 committed
277
			font-size: 28rpx;
278 279 280
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
张成 committed
281 282 283 284 285 286 287
		}

		.right {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: flex-end;
288
			padding-right: 31rpx;
张成 committed
289 290 291 292 293 294 295 296 297 298 299 300 301
			font-size: 20rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;

			.delete-icon {
				width: 18rpx;
				height: 18rpx;
				background: url(../../static/imgs/icon-delete.png) center center no-repeat;
				background-size: cover;
				display: inline-block;
				margin-right: 8rpx;
			}
zhangcheng committed
302 303
		}
	}
张成 committed
304 305 306 307

	.container {
		flex: 1;
		overflow: auto;
zhangcheng committed
308
	}
张成 committed
309 310 311 312 313 314 315 316
}

.goods {
	line-height: 80rpx;
	background-color: #FFFFFF;

	&-detail {
		display: flex;
317
		padding: 30rpx 26rpx 30rpx 32rpx;
张成 committed
318 319 320 321 322 323
		background-color: #fff;
		justify-content: space-between;
		align-items: center;

		.detail-left {
			display: flex;
324 325 326
			height: 128rpx;
			align-items: center;
			justify-content: space-between;
张成 committed
327 328 329 330

			.goods-left {
				display: flex;
				align-items: center;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
				.checkBox{
					margin-right: 32rpx;
					width: 40rpx;
					display: flex;
					align-items: center;
					height: 100%;

					.check{
						width: 100%;
						height: 40rpx;
					}

					.checked{
						background: url('../../static/imgs/duigouxuanzhong.png') center center no-repeat;
						background-size:100% 100%;
					}
					.nochecked{
						background: url('../../static/imgs/weixuanzhong.png') center center no-repeat;
						background-size:100% 100%;
					}
				}
张成 committed
352 353 354 355 356
			}
		}

		.size {
			margin-left: 30rpx;
357 358 359 360 361 362 363 364
			.goods-name{
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				padding-top: 5rpx;
				font-weight: 500;
				color: #333333;
				line-height: 40rpx;
张成 committed
365 366
			}

367 368 369
			.goods-skus {
				margin-top: 4rpx;
				height: 22rpx;
张成 committed
370
				font-size: 20rpx;
371 372 373 374
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 26rpx;
张成 committed
375
			}
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
			
			.goods-price {
				margin-top: 24rpx;

				.price-discount{
					height: 38rpx;
					font-size: 24rpx;
					font-family: Futura-Medium, Futura;
					font-weight: 500;
					color: #333333;
					line-height: 30rpx;
				}
				.price-x {
					margin-left: 6rpx;
					font-size: 20rpx;
					font-family: ArialMT;
					line-height: 22rpx;
					color: #999999;
				}

				.price-xx {
					text-decoration: line-through;
					height: 22rpx;
					font-size: 20rpx;
					font-family: ArialMT;
					color: #999999;
					line-height: 22px;
				}
				}

张成 committed
406 407 408 409

		}

		.detail-right {
weijiguang committed
410
			display: flex;
411 412
			justify-content: flex-start;
			align-items: center;
weijiguang committed
413 414 415 416 417 418 419 420
			// text {
			// 	width: 40rpx;
			// 	line-height: 40rpx;
			// 	text-align: center;
			// 	display: inline-block;
			// 	margin-right: 10rpx;
			// 	color: #000000;
			// }
421 422 423 424 425
			.subtract {
				width: 40rpx;
				height: 40rpx;
				background: url('../../static/imgs/jianhao.png') center center no-repeat;
				background-size:100% 100%;
weijiguang committed
426
				
427
			}
weijiguang committed
428
			.num {
429 430 431
				height: 28rpx;
				font-size: 32rpx;
				font-family: ArialMT;
张成 committed
432
				color: #000000;
433 434 435
				line-height: 28rpx;
				margin-left: 26rpx;
				margin-right: 26rpx;
weijiguang committed
436
				font-weight: bold;
张成 committed
437
			}
438 439 440 441 442 443
			.add {
				width: 40rpx;
				height: 40rpx;
				background: url('../../static/imgs/jiahao.png') center center no-repeat;
				background-size:100% 100%;
			}
张成 committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
		}
	}
}

.empty {

	position: relative;
	top: 220rpx;
	text-align: center;
	display: flex;
	align-items: center;
	flex-direction: column;

	&-text {
		color: #808080;
		margin-bottom: 50rpx;
	}

	&-button {
		width: 300rpx;
		height: 90rpx;
		color: orange;
		border: 1rpx solid orange;
		text-align: center;
		line-height: 90rpx;
		border-radius: 48rpx;
	}
}

.end {
	width: 100%;
	height: 92rpx;
	background-color: rgb(253, 253, 253);
	position: fixed;
张成 committed
478
	bottom: 0;
张成 committed
479
	left: 0;
480
	box-shadow: 0px -4px 8px 0px rgba(102, 102, 102, 0.1);
张成 committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	display: flex;
	z-index: 10075;
	align-items: center;

	&-left {
		width: 70%;
		display: flex;
		justify-content: space-between;
		padding: 0 30rpx;

		.end-flex {
			display: flex;
			align-items: center;
		}

		.car-img {
497 498
			width: 45rpx;
			height: 54rpx;
张成 committed
499
			display: inline-block;
500
			background: url('../../static/imgs/gouwudai.png') center center no-repeat;
张成 committed
501 502 503 504 505
			background-size: cover;
			position: relative;

			.badge {
				position: absolute;
506 507 508
				height: 26rpx;
				font-family: Futura-Medium, Futura;
				background: #FF72C1;
张成 committed
509 510
				right: -16rpx;
				top: -4rpx;
511 512 513
				min-width: 26rpx;
				font-weight: 500;
				font-size: 20rpx;
张成 committed
514 515
				font-weight: normal;
				color: #FFFFFF;
516
				line-height: 26rpx;
张成 committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50%;

			}
		}
	}

	&-right {
		width: 169rpx;
		font-size: 32rpx;
		line-height: 92rpx;
		background-color: #006ECF;
531
		text-align: right;
张成 committed
532 533 534
		color: #fff;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
zhangcheng committed
535
	}
536 537 538 539 540
	.goSubmmit{
		background: url('../../static/imgs/fukuan.png') center center no-repeat;
		width: 200rpx;
		height: 100%;
	}
张成 committed
541
}
zhangcheng committed
542
</style>