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

zhangcheng committed
249 250 251
</script>

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

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

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

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

		.right {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: flex-end;
290
			padding-right: 31rpx;
张成 committed
291 292 293 294 295 296 297 298 299 300 301 302 303
			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
304 305
		}
	}
张成 committed
306 307 308 309

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

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

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

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

			.goods-left {
				display: flex;
				align-items: center;
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
				.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
354 355 356 357 358
			}
		}

		.size {
			margin-left: 30rpx;
359 360 361 362 363 364 365 366
			.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
367 368
			}

369 370 371
			.goods-skus {
				margin-top: 4rpx;
				height: 22rpx;
张成 committed
372
				font-size: 20rpx;
373 374 375 376
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 26rpx;
张成 committed
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 406 407
			
			.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
408 409 410 411

		}

		.detail-right {
weijiguang committed
412
			display: flex;
413 414
			justify-content: flex-start;
			align-items: center;
weijiguang committed
415 416 417 418 419 420 421 422
			// text {
			// 	width: 40rpx;
			// 	line-height: 40rpx;
			// 	text-align: center;
			// 	display: inline-block;
			// 	margin-right: 10rpx;
			// 	color: #000000;
			// }
423 424 425 426 427
			.subtract {
				width: 40rpx;
				height: 40rpx;
				background: url('../../static/imgs/jianhao.png') center center no-repeat;
				background-size:100% 100%;
weijiguang committed
428
				
429
			}
weijiguang committed
430
			.num {
431 432 433
				height: 28rpx;
				font-size: 32rpx;
				font-family: ArialMT;
张成 committed
434
				color: #000000;
435 436 437
				line-height: 28rpx;
				margin-left: 26rpx;
				margin-right: 26rpx;
weijiguang committed
438
				font-weight: bold;
张成 committed
439
			}
440 441 442 443 444 445
			.add {
				width: 40rpx;
				height: 40rpx;
				background: url('../../static/imgs/jiahao.png') center center no-repeat;
				background-size:100% 100%;
			}
张成 committed
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 478 479
		}
	}
}

.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
480
	bottom: 0;
张成 committed
481
	left: 0;
482
	box-shadow: 0px -4px 8px 0px rgba(102, 102, 102, 0.1);
张成 committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	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 {
499 500
			width: 45rpx;
			height: 54rpx;
张成 committed
501
			display: inline-block;
502
			background: url('../../static/imgs/gouwudai.png') center center no-repeat;
张成 committed
503 504 505 506 507
			background-size: cover;
			position: relative;

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

			}
		}
	}

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