浏览代码

购物车

YY 2 年之前
父节点
当前提交
ff369d7836
共有 1 个文件被更改,包括 81 次插入32 次删除
  1. 81 32
      pages/market/index.vue

+ 81 - 32
pages/market/index.vue

@@ -63,7 +63,7 @@
 											</view>
 											<view class="num" v-if="num==1">
 												<uni-number-box @change="changeValue(item)" name="num" value="item"
-													min="1" v-model="item.num" />
+													:min="1" v-model="item.num" />
 											</view>
 											<view class="del" v-if="num==1">
 												<text class="iconfont icon-del-copy" @click="toDel(item)"></text>
@@ -136,15 +136,10 @@
 				totalMoney: 0,
 			};
 		},
-		onShow: function() {},
+		onShow: function() {
+
+		},
 		methods: {
-			// 公共跳转
-			toCommon(route, e) {
-				const that = this;
-				uni.navigateTo({
-					url: `${route}?id=${e && e._id ? e._id : ''}`
-				})
-			},
 			toPath(e) {
 				if (e && e.route) uni.redirectTo({
 					url: `/${e.route}`
@@ -189,11 +184,10 @@
 			// 选择店铺
 			marketChange(e) {
 				const that = this;
-				const list = that.list;
+				const list = this.list;
 				const {
 					value
 				} = e.detail;
-				let data = [];
 				for (const p1 of list) {
 					let p2 = value.find((i) => i == p1.id);
 					let a = p2 ? true : false;
@@ -201,28 +195,76 @@
 					for (let s of p1.goods) {
 						s.check = a
 					}
-					data.push(p1);
 				}
-				that.$set(that, `list`, data)
+				that.$set(that, `list`, list);
+				that.marketAllChange();
+				// 计算总额
+				that.countMoney();
+			},
+			//店铺全部选择true,全选自动选择,
+			marketAllChange() {
+				const that = this;
+				let list = that.list;
+				for (let val of list) {
+					if (val.check == true) {
+						that.$set(that, `isAll`, true)
+					} else {
+						that.$set(that, `isAll`, false)
+					}
+				}
+				that.$set(that, `list`, list)
 				// 计算总额
 				that.countMoney();
 			},
 			//选择商品
 			goodsChange(e) {
 				const that = this;
-				const list = that.list;
+				let list = that.list;
 				const {
 					value
 				} = e.detail;
-				let data = [];
-				console.log(value);
-				console.log(list);
 				let shop = list.find(f => f.goods.find(i => value.find(s => s == i.id)))
-				console.log(shop);
-				for (let val of shop.goods) {
-					let p2 = value.find((i) => i == val.id);
-					if (p2) val.check = true;
-					else val.check = false;
+				if (shop) {
+					for (let val of shop.goods) {
+						let p2 = value.find((i) => i == val.id);
+						if (p2) val.check = true;
+						else val.check = false;
+					}
+				} else {
+					list = list.map(i => {
+						i.goods = i.goods.map(g => ({
+							...g,
+							check: false
+						}))
+						return i;
+					})
+				}
+				that.$set(that, `list`, list)
+				that.goodsAllChange(value);
+				// 计算总额
+				that.countMoney();
+			},
+			//商品全部选择true,店铺自动选择,
+			goodsAllChange(value) {
+				const that = this;
+				let list = that.list;
+				let shop = list.find(f => f.goods.find(i => value.find(s => s == i.id)))
+				if (shop) {
+					for (let val of shop.goods) {
+						if (val.check == true) {
+							shop.check = true;
+						} else {
+							shop.check = false;
+						}
+					}
+				} else {
+					list = list.map(i => {
+						i.goods = i.goods.map(g => ({
+							...g,
+							check: false
+						}))
+						return i;
+					})
 				}
 				// 计算总额
 				that.countMoney();
@@ -231,7 +273,6 @@
 			changeValue(value) {
 				const that = this;
 				const list = that.list;
-				let data = [];
 				for (const val of list) {
 					for (let s of val.goods) {
 						if (s.id == value.id) {
@@ -239,9 +280,11 @@
 						}
 					}
 				}
+				that.$set(that, `list`, list)
 				// 计算总额
-				that.countMoney();
+				that.countMoney(list);
 			},
+			// 删除
 			toDel(e) {
 				const that = this;
 				const list = that.list;
@@ -275,18 +318,24 @@
 			// 计算总额
 			countMoney() {
 				const that = this;
-				const list = that.list;
+				const list = this.list;
 				let totalMoney = 0;
-				for (const val of list) {
-					for (let s of val.goods) {
-						if (s.check == true) {
-							let total = Number(s.money) * Number(s.num);
-							totalMoney += Number(total);
+				// 渲染结束执行下面方法
+				that.$nextTick(() => {
+					for (const val of list) {
+						for (let s of val.goods) {
+							if (s.check == true) {
+								let total = Number(s.money) * Number(s.num);
+								totalMoney += Number(total);
+							}
 						}
 					}
-				}
-				that.$set(that, `totalMoney`, totalMoney.toFixed(2))
+					that.$set(that, `totalMoney`, totalMoney.toFixed(2))
+				})
+
 			},
+			// 去结算
+			toSettle() {},
 		}
 	}
 </script>