From edf50893285f13c7c975b376a70ac3b164f48a13 Mon Sep 17 00:00:00 2001
From: xuxueyang <xuxy@fengyuntec.com>
Date: 星期一, 02 九月 2024 18:02:54 +0800
Subject: [PATCH] update 购物车加载速度

---
 sub_pages/customer/shopping/shopping.vue |  224 +++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 188 insertions(+), 36 deletions(-)

diff --git a/sub_pages/customer/shopping/shopping.vue b/sub_pages/customer/shopping/shopping.vue
index e9aaf14..7b36437 100644
--- a/sub_pages/customer/shopping/shopping.vue
+++ b/sub_pages/customer/shopping/shopping.vue
@@ -10,24 +10,27 @@
 			<view class="name">
 				15974805814
 			</view>
+			<view class="m-l-a m-r-0">
+				<uni-icons v-if="list&&list.length>0" type="trash" size="28" @click="batchDelete"></uni-icons>
+			</view>
 		</view>
 		<view class="">
 			<no-data v-if="!list||list.length===0" style="width: 100%;"></no-data>
 			<view class="shopping-item m-t-20" v-for="(item,index) of list" :key="index">
-
-
 				<view class="sup-title">
-					<radio :checked="ids.indexOf(item.id)>=0" @click="changeItem(item,'supplier')"></radio>
+					<radio :checked="ids.indexOf('supplier@'+item.supplierId)>=0" @click="changeItem(item,'supplier')">
+					</radio>
 					{{ item.supplierName || '-' }}
 				</view>
 				<u-divider></u-divider>
-				<view v-for="(dto,j) of item.flowerList" :key="j">
+				<view v-for="(dto,j) of item.flowerList" :key="dto.id">
 					<u-divider v-if="j>0"></u-divider>
 					<u-swipe-action>
-						<u-swipe-action-item :options="options1" @click="(e)=>{clickSwipeButton(dto)}">
+						<u-swipe-action-item :options="options1" @click="(e)=>{clickSwipeButton(dto,true)}">
 							<view class="item-each flex">
 								<radio :checked="ids.indexOf(dto.id)>=0" @click="changeItem(dto,'flower')"></radio>
-								<image class="img img100 m-r-6 br-4" :src="dto.url||dto.cover"></image>
+								<image class="img img100 m-r-6 br-4" :lazy-load="true"
+									:class="[!dto.stock?'component-stock-zero':'']" :src="dto.url||dto.cover"></image>
 								<view class="flex1">
 									<view class="title" @click.stop="toDetail(dto)"><span class="m-r-5"
 											style="display: inline-block;">{{dto.categoryStr||''}}</span><span
@@ -45,7 +48,8 @@
 										<view class="button-icons flex m-l-a m-r-0">
 											<uni-icons v-if="dto.num&&dto.num>=1" type="minus" size="32"
 												@click.stop="addnum(dto,-1)"></uni-icons>
-											<view class="curnums" v-if="dto.num&&dto.num>=1">{{ dto.num }}</view>
+											<view class="curnums" @click.stop="updateItemNum(dto)"
+												v-if="dto.num&&dto.num>=1">{{ dto.num }}</view>
 											<uni-icons type="plus-filled" size="32"
 												@click.stop="addnum(dto,1)"></uni-icons>
 										</view>
@@ -60,7 +64,7 @@
 			</view>
 
 		</view>
-		<view style="min-height:200rpx">
+		<view style="min-height:140rpx;background-color: #fff;">
 		</view>
 		<view class="bottom-price flex">
 			<view class="m-t-12">
@@ -74,7 +78,7 @@
 				提交订单
 			</view>
 		</view>
-		<view style="min-height:200rpx">
+		<view style="min-height:140rpx">
 		</view>
 		<common-footer flg="2"></common-footer>
 	</view>
@@ -96,28 +100,78 @@
 			}
 		},
 		onShow() {
-			if (this.sign['shopping']) {
-				this.init()
-			}
+			// if (this.sign['shopping']) {
+
+			// }
+			this.init()
 		},
 		mounted() {
-			this.init()
+			// this.init()
+		},
+		async onPullDownRefresh() {
+			await this.init()
+			uni.stopPullDownRefresh()
 		},
 		computed: {
 			totalprice() {
 
 				let totalprice = 0
-				this.list && this.list.forEach(dto => {
-					dto.flowerList && dto.flowerList.forEach(item => {
-						if (this.ids.indexOf(item.id) >= 0) {
-							totalprice += item.price * item.num
-						}
+				if (this.ids.length > 0) {
+					this.list && this.list.forEach(dto => {
+						dto.flowerList && dto.flowerList.forEach(item => {
+							if (this.ids.indexOf(item.id) >= 0) {
+								totalprice += item.price * item.num
+							}
+						})
 					})
-				})
+				}
+
 				return totalprice.toFixed(2)
 			},
 		},
 		methods: {
+
+			async batchDelete() {
+				// /api/customer/flower/cart/delete/batch
+				var arr = []
+
+				for (var id of this.ids) {
+					console.log('id', id)
+					if (('' + id).startsWith('supplier@')) {
+
+					} else {
+						arr.push(parseInt(id))
+					}
+				}
+
+				if (arr.length == 0) {
+					this.$message.showToast('请选择需要删除的商品')
+					return
+				}
+
+				await this.$message.confirm(`是否批量删除这${arr.length}个商品`)
+
+
+				this.$message.showLoading()
+				const {
+					code
+				} = await this.$http.request('post', '/api/customer/flower/cart/delete/batch', {
+					data: {
+						ids: arr,
+					}
+				})
+				this.$message.hideLoading()
+				if (code === 0) {
+					this.$store.dispatch('sign_add', 'shopnum');
+					this.ids = []
+					this.checkall = false
+					this.init()
+				}
+
+
+
+
+			},
 			async submitPay() {
 				// order/confirm/info
 				var arr = []
@@ -139,38 +193,53 @@
 				}
 				if (arr.length < 1) {
 					console.log('请选择商品', arr, this.ids)
+					this.$message.showToast('请选择商品')
 					return
 				}
 				this.$message.showLoading()
 				const {
 					code,
 					data
-				} = await http.request('post', '/api/customer/flower/order/confirm/info', {
+				} = await this.$http.request('post', '/api/customer/flower/order/confirm/info', {
 					data: {
 						flowers: arr,
 					}
 				})
-				this.$message.hideLoading()
+				let that = this
 				if (code === 0) {
-					this.$storage.setItem('_cache_shopping_dto', JSON.stringify(data))
+					that.$message.hideLoading()
+					that.$storage.setItem('_cache_shopping_dto', JSON.stringify(data))
 					uni.navigateTo({
 						url: '/sub_pages/customer/shopping/confirm'
 					})
+				} else {
+					//提示消失快
+					setTimeout(() => {
+						that.$message.hideLoading()
+					}, 2000)
 				}
+
 			},
-			async clickSwipeButton(item) {
+			async clickSwipeButton(item, check) {
+				// console.log('clickSwipeButton', e)
 				//删除商品,重新加载数据?
+				if (check) {
+					await this.$message.confirm('是否删除商品')
+
+				}
 
 				this.$message.showLoading()
 				const {
 					code
-				} = await http.request('get', '/api/customer/flower/cart/delete', {
+				} = await this.$http.request('get', '/api/customer/flower/cart/delete', {
 					params: {
 						id: item.id,
 					}
 				})
 				this.$message.hideLoading()
 				if (code === 0) {
+					this.$store.dispatch('sign_add', 'shopnum');
+
 					if (this.ids.indexOf(item.id) >= 0) {
 						this.ids.splice(this.ids.indexOf(item.id), 1)
 					}
@@ -198,7 +267,17 @@
 							break
 						}
 					}
-					console.log('this.list', this.list)
+					// console.log('this.list', this.list)
+					// 解决滑动的swipe不自动关闭的问题
+					if (check) {
+						let arr = this.list
+						this.list = []
+						this.$nextTick(() => {
+							this.list = arr
+						})
+					}
+
+
 				}
 			},
 
@@ -218,6 +297,9 @@
 								this.ids.push(item.id)
 							}
 						})
+						if (this.ids.indexOf('supplier@' + dto.supplierId) < 0) {
+							this.ids.push('supplier@' + dto.supplierId)
+						}
 					})
 					this.checkall = true
 				}
@@ -233,11 +315,26 @@
 								this.ids.push(item.id)
 							}
 						})
+						//如果所有的话都悬赏了,设置为return true;
+						var tmpSelectAll = true
+						for (var i = 0; i < this.list.length; i++) {
+							for (var j = 0; j < this.list[i].flowerList.length; j++) {
+								if (this.ids.indexOf(this.list[i].flowerList[j].id) < 0) {
+									tmpSelectAll = false
+									break
+								}
+							}
+
+						}
+						this.checkall = tmpSelectAll
+
+
 					} else {
 						this.checkall = false
 						this.ids.splice(this.ids.indexOf('supplier@' + dto.supplierId), 1)
+						console.log('this.ids splice', this.ids)
 						dto.flowerList.forEach(item => {
-							this.ids.splice(this.ids.indexOf('supplier@' + item.supplierId), 1)
+							this.ids.splice(this.ids.indexOf(item.id), 1)
 						})
 					}
 
@@ -260,9 +357,26 @@
 							}
 						}
 						if (has) {
-							this.ids.push(dto.supplierId)
+							if (this.ids.indexOf('supplier@' + dto.supplierId) < 0) {
+								this.ids.push('supplier@' + dto.supplierId)
+								this.$forceUpdate()
+							}
+
+						} else {
+							console.log('has', this.ids, dto)
+						}
+						//如果所有的话都悬赏了,设置为return true;
+						var tmpSelectAll = true
+						for (var i = 0; i < this.list.length; i++) {
+							for (var j = 0; j < this.list[i].flowerList.length; j++) {
+								if (this.ids.indexOf(this.list[i].flowerList[j].id) < 0) {
+									tmpSelectAll = false
+									break
+								}
+							}
 
 						}
+						this.checkall = tmpSelectAll
 
 					} else {
 						this.checkall = false
@@ -270,7 +384,35 @@
 						if (this.ids.indexOf(dto.id) >= 0) {
 							this.ids.splice(this.ids.indexOf(dto.id), 1)
 						}
+						//还要删除供应商的id
+						if (this.ids.indexOf('supplier@' + dto.supplierId) >= 0) {
+							this.ids.splice(this.ids.indexOf('supplier@' + dto.supplierId), 1)
+						}
+
 					}
+
+				}
+			},
+			async updateItemNum(item) {
+				const res = await this.$message.confirm('', {
+					editable: true,
+					title: '请输入想要购买的数量'
+				})
+				if (res.content && res.confirm) {
+					// 发送请求
+					var t = parseInt(res.content)
+					if (isNaN(t) || t < 0) {
+						this.$message.showToast('数目需要大于等于0')
+
+					} else {
+						if (!item.stock || t > item.stock) {
+							this.$message.showToast('库存不足无法修改')
+							return
+						}
+						this.addnum(item, (t - item.num))
+
+					}
+				} else {
 
 				}
 			},
@@ -278,19 +420,20 @@
 				if (dto.num + addnum >= 0) {
 
 				} else {
+					console.log('addnum', dto, addnum)
 					return
 				}
 				if (!dto.stock) {
 					dto.stock = 0
 				}
-				if (dto.num + addnum > dto.stock) {
-					this.$message.showToast('库存不足,无法添加')
+				if (addnum > 0 && dto.num + addnum > dto.stock) {
+					this.$message.showToast('库存不足,无法修改')
 					return
 				}
 				this.$message.showLoading()
 				const {
 					code
-				} = await http.request('post', '/api/customer/flower/cart/change-num', {
+				} = await this.$http.request('post', '/api/customer/flower/cart/change-num', {
 					data: {
 						id: dto.id,
 						num: addnum
@@ -301,12 +444,18 @@
 					dto.num += addnum
 					if (dto.num < 1) {
 						//id删除清空,并且
-						await this.clickSwipeButton(dto)
+						await this.clickSwipeButton(dto, false)
 						// await this.init()
 					}
+					this.$store.dispatch('sign_add', 'shopnum');
+					this.$forceUpdate()
 				}
 			},
 			async init() {
+
+				this.ids = []
+				this.checkall = false
+
 				if (!this.currentInfo.id) {
 					this.$message.showToast('请先登录')
 					return
@@ -315,8 +464,9 @@
 					this.$message.showToast('请先前往个人中心补充个人信息')
 					return
 				}
+				this.$store.dispatch('sign_clear', 'shopping');
+
 				this.$message.showLoading()
-				await this.$store.dispatch('sign_clear', 'shopping');
 				const {
 					code,
 					data
@@ -325,7 +475,7 @@
 				this.list = []
 				if (code === 0) {
 					this.list = data || []
-
+					this.$forceUpdate()
 				}
 			},
 			toDetail(dto) {
@@ -348,7 +498,9 @@
 			left: 0rpx;
 			padding: 20rpx;
 			right: 0rpx;
-			bottom: 160rpx;
+			// bottom: 160rpx;
+			bottom: 120rpx;
+			z-index: 11;
 		}
 
 		.shopping-item {
@@ -402,8 +554,8 @@
 				line-height: 40rpx;
 
 				.curnums {
-					margin-left: 10rpx;
-					margin-right: 10rpx;
+					padding-left: 20rpx;
+					padding-right: 20rpx;
 				}
 			}
 		}

--
Gitblit v1.9.3