Browse Source

剩余房间问题修复

soft5566 2 years ago
parent
commit
6f9a91ae5e
2 changed files with 101 additions and 63 deletions
  1. 100 62
      pages/index/index.vue
  2. 1 1
      utils/common.js

+ 100 - 62
pages/index/index.vue

@@ -374,39 +374,42 @@
 					d = arrDate.date
 				} else
 					d = param
-				that.$myRequest({
-					url: '/hotelReservation/zhotel/appday_list.action?date=' + d,
-				}).then(res => {
-					// console.log(res.data);
-					if (res.data.code === 200) {
-						let data = res.data.data;
-						data.forEach(item => {
-							let it = {
-								date: item.date,
-								h_type: item.h_type,
-								id: item.id,
-								price: item.price,
-								y_num: item.y_num,
-								z_num: item.z_num,
-								s_yum: item.s_yum
-							}
-							if (item.h_type.indexOf('标准') !== -1 && item.s_yum < that.biaozhun_min) {
-								that.biaozhun_min = item.s_yum
-							}
-							if (item.h_type.indexOf('高级') !== -1 && item.s_yum < that.gaoji_min) {
-								that.gaoji_min = item.s_yum
-							}
-							if (item.h_type.indexOf('豪华') !== -1 && item.s_yum < that.haohua_min) {
-								that.haohua_min = item.s_yum
-							}
-							that.roomType.push(it)
-						});
-					} else {
-						uni.showToast({
-							title: '未获得房型'
-						})
-					}
-				});
+
+				if (d !== undefined) {
+					that.$myRequest({
+						url: '/hotelReservation/zhotel/appday_list.action?date=' + d,
+					}).then(res => {
+						// console.log(res.data);
+						if (res.data.code === 200) {
+							let data = res.data.data;
+							data.forEach(item => {
+								let it = {
+									date: item.date,
+									h_type: item.h_type,
+									id: item.id,
+									price: item.price,
+									y_num: item.y_num,
+									z_num: item.z_num,
+									s_yum: item.s_yum
+								}
+								if (item.h_type.indexOf('标准') !== -1 && item.s_yum < that.biaozhun_min) {
+									that.biaozhun_min = item.s_yum
+								}
+								if (item.h_type.indexOf('高级') !== -1 && item.s_yum < that.gaoji_min) {
+									that.gaoji_min = item.s_yum
+								}
+								if (item.h_type.indexOf('豪华') !== -1 && item.s_yum < that.haohua_min) {
+									that.haohua_min = item.s_yum
+								}
+								that.roomType.push(it)
+							});
+						} else {
+							uni.showToast({
+								title: '未获得房型'
+							})
+						}
+					});
+				}
 			},
 			// 获取用户信息openid,没有则弹出进行授权
 			getUserInfo() {
@@ -612,6 +615,10 @@
 			},
 			change(e) {
 				var that = this;
+				that.biaozhun_min = 1000
+				that.gaoji_min = 1000
+				that.haohua_min = 1000
+				
 				var before = e.range.before;
 				var after = e.range.after;
 
@@ -622,47 +629,78 @@
 				if (this.dates.length !== 0) {
 					let temp = this.roomType;
 					this.info.selected = []
-					this.dates.forEach((date) => {
-						that.$myRequest({
-							url: '/hotelReservation/zhotel/appday_list.action?date=' + date,
-						}).then(res => {
-							// console.log(res.data);
-							if (res.data.code === 200) {
-								var min = 1000;
-								let data = res.data.data;
-								data.forEach(item => {
+					// 创建一个用于存储请求结果的数组
+					const results = [];
+
+					// 创建一个包含所有请求的Promise数组
+					const requestPromises = this.dates.map(date => {
+						return new Promise((resolve, reject) => {
+							uni.request({
+								url: '/hotelReservation/zhotel/appday_list.action?date=' + date,
+								success: res => {
+									// 将请求结果存储到结果数组中
+									results.push(res);
+									resolve();
+								},
+								fail: err => {
+									reject(err);
+								}
+							});
+						});
+					});
+
+					// 使用Promise.all()等待所有请求完成
+					Promise.all(requestPromises)
+						.then(() => {
+							// 所有请求完成后的处理
+							// 遍历每个元素
+							results.forEach(result => {
+								// 遍历每个元素中的"data"数组
+								result.data.data.forEach(item => {
+									// console.log(item);
+									var min = 1000;
+									// 在这里对每个数据项进行操作
 									if (item.h_type.indexOf('标准') !== -1 && item.s_yum < that.biaozhun_min) {
 										temp.forEach((it) => {
-											if (it.h_type.indexOf('标准') !== -1)
-												it.s_yum = item.s_yum
+											if (it.h_type.indexOf('标准') !== -1) {
+												this.$set(it, 's_yum', item.s_yum);
+												that.biaozhun_min = item.s_yum;
+											}
 										})
-									}
-									if (item.h_type.indexOf('高级') !== -1 && item.s_yum < that.gaoji_min) {
+									} else if (item.h_type.indexOf('高级') !== -1 && item.s_yum < that.gaoji_min) {
 										temp.forEach((it) => {
-											if (it.h_type.indexOf('高级') !== -1)
-												it.s_yum = item.s_yum
+											if (it.h_type.indexOf('高级') !== -1) {
+												this.$set(it, 's_yum', item.s_yum);
+												that.gaoji_min = item.s_yum;
+											}
 										})
-									}
-									if (item.h_type.indexOf('豪华') !== -1 && item.s_yum < that.haohua_min) {
+									} else if (item.h_type.indexOf('豪华') !== -1 && item.s_yum < that.haohua_min) {
 										temp.forEach((it) => {
-											if (it.h_type.indexOf('豪华') !== -1)
-												it.s_yum = item.s_yum
+											if (it.h_type.indexOf('豪华') !== -1) {
+												this.$set(it, 's_yum', item.s_yum);
+												that.haohua_min = item.s_yum;
+											}
 										})
 									}
 									if (min > item.s_yum)
 										min = item.s_yum
+
+									that.info.selected.push({
+										date: item.date,
+										info: min.toString()
+									})
 								});
-								this.info.selected.push({
-									date: date,
-									info: min.toString()
-								})
-							}
+							});
+							setTimeout(() => {
+								this.roomType = []
+								this.roomType = temp;
+							}, 500)
+
+						})
+						.catch(err => {
+							// 处理错误
+							console.error('异常:', err);
 						});
-					});
-					setTimeout(() => {
-						this.roomType = []
-						this.roomType = temp;
-					}, 2000)
 				} else {
 					this.info.selected = [{
 							date: before,

+ 1 - 1
utils/common.js

@@ -179,7 +179,7 @@ const utils = {
 			dates.push(formattedDate);
 			current.setDate(current.getDate() + 1);
 		}
-
+		
 		return dates;
 	}
 }