Browse Source

尽量让数据在本地更新,避免不必要的频繁加载数据和刷新页面,进行了优化。

soft5566 3 years ago
parent
commit
a7e6743f3e
1 changed files with 195 additions and 100 deletions
  1. 195 100
      src/views/serveAC/index.vue

+ 195 - 100
src/views/serveAC/index.vue

@@ -1140,11 +1140,12 @@
 			 * @param {Object} room
 			 */
 			dialog_delete_rooms_click(room) {
-				if (this.currentData.rooms[this.delete_room_index].tableData.length > 0) {
-					this.$message.error('房间中有设备,不能删除!');
+				var _this = this;
+				if (_this.currentData.rooms[_this.delete_room_index].tableData.length > 0) {
+					_this.$message.error('房间中有设备,不能删除!');
 					return;
 				}
-				let data = this.currentData.title_any_where.split('-')
+				let data = _this.currentData.title_any_where.split('-')
 				delRooms({
 					school: data[0],
 					build: data[1],
@@ -1153,49 +1154,53 @@
 				}).then((res) => {
 					// console.log(res);
 					if (typeof res.code == 'undefined' || res.code == '') {
-						this.$message.error('返回数据格式问题,code未获取到!')
-						return
+						_this.$message.error('返回数据格式问题,code未获取到!');
+						return;
 					}
 
 					if (res.code == 200) {
 						// 隐藏删除对话框
-						this.dialog_delete_room = false
-						// 更新本地变量中的房间
-						let tmp = this.navData
-						for (var i = 0; i < tmp.length; i++) {
-							if (tmp[i].label == data[0] && typeof tmp[i].children !== 'undefined' && tmp[i]
-								.children.length > 0) {
-
-								let tmp1 = tmp[i].children
-								for (var j = 0; j < tmp1.length; j++) {
-									if (tmp1[j].label == data[1] && typeof tmp1[j].children !==
-										'undefined' && tmp1[j].children.length > 0) {
-
-										let tmp2 = tmp1[j].children
-										for (var k = 0; k < tmp2.length; k++) {
-											if (tmp2[k].label == data[2] && typeof tmp2[k].rooms !== 'undefined' && tmp2[k].rooms.length > 0) {
-
-												let tmp3 = tmp2[k].rooms
-												for (var p = 0; p < tmp3.length; p++) {
-													if (tmp3[p] == room) {
-														this.navData[i].children[j].children[k].rooms.splice(p, 1)
-													}
-												}
-												this.navData[i].children[j].children[k].rooms.sort()
-											}
-										}
-									}
-								}
-							}
-						}
+						_this.dialog_delete_room = false;
+						var arr = room + '|' + data[2] + '|' + data[1] + '|' + data[0];
+						var indexs = _this.navData_positioning(arr, 'label', 'fj');
+						// 删除本地变量中的房间,并排序
+						_this.navData[indexs[0]].children[indexs[1]].children[indexs[2]].rooms.splice(indexs[3], 1);
+						_this.navData[indexs[0]].children[indexs[1]].children[indexs[2]].rooms.sort();
+						// let tmp = _this.navData
+						// for (var i = 0; i < tmp.length; i++) {
+						// 	if (tmp[i].label == data[0] && typeof tmp[i].children !== 'undefined' && tmp[i].children.length > 0) {
+
+						// 		let tmp1 = tmp[i].children;
+						// 		for (var j = 0; j < tmp1.length; j++) {
+						// 			if (tmp1[j].label == data[1] && typeof tmp1[j].children !== 'undefined' && tmp1[j].children.length > 0) {
+
+						// 				let tmp2 = tmp1[j].children
+						// 				for (var k = 0; k < tmp2.length; k++) {
+						// 					if (tmp2[k].label == data[2] && typeof tmp2[k].rooms !== 'undefined' && tmp2[k].rooms.length > 0) {
+
+						// 						let tmp3 = tmp2[k].rooms
+						// 						for (var p = 0; p < tmp3.length; p++) {
+						// 							if (tmp3[p] == room) {
+						// 								_this.navData[i].children[j].children[k].rooms.splice(p, 1);
+						// 							}
+						// 						}
+						// 						_this.navData[i].children[j].children[k].rooms.sort();
+						// 					}
+						// 				}
+						// 			}
+						// 		}
+						// 	}
+						// }
 						// 自动点击
-						this.currentData.clickElement.click()
+						setTimeout(() => {
+							_this.currentData.clickElement.click();
+						}, 300);
 					} else {
-						this.$message.error(res.message)
+						_this.$message.error(res.message);
 					}
 				}).catch((err) => {
 					// console.log(err);
-					this.$message.error(err.message)
+					_this.$message.error(err.message);
 				})
 			},
 			/**
@@ -1463,7 +1468,7 @@
 					title_any_where: '',
 					clickElement: e.target
 				};
-				// 分页每次单楼层时,都要初始化
+				// 分页每次单楼层时,都要初始化
 				this.pagination = {
 					pageSize: 4,
 					currentPage: 1,
@@ -1507,7 +1512,7 @@
 					cancelButtonText: '取消',
 					type: 'warning'
 				}).then(() => {
-					let indexs = _this.navData_positioning(id, 'id', 3);
+					let indexs = _this.navData_positioning(id, 'id', 'lc');
 					if (indexs.length > 0) {
 						let i = indexs[0];
 						let j = indexs[1];
@@ -1550,22 +1555,47 @@
 			 * 本地数据定位
 			 * @param {Object} arr
 			 */
-			navData_positioning(arr, option, num) {
-				// console.log(arr, option, num);
+			navData_positioning(arr, option, depth) {
+				// console.log(arr, option, depth);
 				if (option == 'id') { // 如果传的是id
 					let ids = arr.split('|');
-					let xq = ids.length == 3 ? ids[2] : ids[1];
-					let ld = ids.length == 3 ? ids[1] : ids[0];
-					let lc = ids.length == 3 ? ids[0] : 0;
+					let xq = 0;
+					let ld = 0;
+					let lc = 0;
+					let fj = 0;
+
+					if (depth == 'xq') {
+						xq = ids[0];
+					} else if (depth == 'ld') {
+						xq = ids[1];
+						ld = ids[0];
+					} else if (depth == 'lc') {
+						xq = ids[2];
+						ld = ids[1];
+						lc = ids[0];
+					} else if (depth == 'fj') {
+						xq = ids[3];
+						ld = ids[2];
+						lc = ids[1];
+						fj = ids[0];
+					} else {
+						this.$message.error('要查找的深度不对!');
+						return;
+					}
 
 					for (var i = 0; i < this.navData.length; i++) {
+						// 校区
 						if (this.navData[i].id == xq) {
-							// 校区
+							if (depth == 'xq') {
+								// 返回坐标
+								return [i];
+							}
+
 							var xiaoqu = this.navData[i].children;
 							for (var j = 0; j < xiaoqu.length; j++) {
 								// 楼栋
 								if (xiaoqu[j].id == ld) {
-									if (num == 2) {
+									if (depth == 'ld') {
 										// 返回坐标
 										return [i, j];
 									}
@@ -1573,9 +1603,22 @@
 									var loudong = xiaoqu[j].children;
 									for (var k = 0; k < loudong.length; k++) {
 										// 楼层
-										if (loudong[k].id == lc && num == 3) {
-											// 返回坐标
-											return [i, j, k];
+										if (loudong[k].id == lc) {
+											if (depth == 'lc') {
+												// 返回坐标
+												return [i, j, k];
+											}
+
+											var fangjian = loudong[k];
+											for (var m = 0; m < fangjian.length; m++) {
+												// 房间
+												if (fangjian[m] == fj) {
+													if (depth == 'fj') {
+														// 返回坐标
+														return [i, j, k, m];
+													}
+												}
+											}
 										}
 									}
 								}
@@ -1584,23 +1627,69 @@
 					}
 				} else if (option == 'label') { // 如果传的是label
 					let labels = arr.split('|');
-					let xq = labels.length == 2 ? labels[1] : labels[0];
-					let ld = labels.length == 2 ? labels[0] : 0;
+
+					let xq = '';
+					let ld = '';
+					let lc = '';
+					let fj = '';
+
+					if (depth == 'xq') {
+						xq = labels[0];
+					} else if (depth == 'ld') {
+						xq = labels[1];
+						ld = labels[0];
+					} else if (depth == 'lc') {
+						xq = labels[2];
+						ld = labels[1];
+						lc = labels[0];
+					} else if (depth == 'fj') {
+						xq = labels[3];
+						ld = labels[2];
+						lc = labels[1];
+						fj = labels[0];
+					} else {
+						this.$message.error('要查找的深度不对!');
+						return;
+					}
+
 					for (var i = 0; i < this.navData.length; i++) {
+						// 校区
 						if (this.navData[i].label == xq) {
-							if (num == 1) {
+							if (depth == 'xq') {
 								// 返回坐标
 								return [i];
 							}
-							// 校区
+
 							var xiaoqu = this.navData[i].children;
 							for (var j = 0; j < xiaoqu.length; j++) {
 								// 楼栋
 								if (xiaoqu[j].label == ld) {
-									if (num == 2) {
+									if (depth == 'ld') {
 										// 返回坐标
 										return [i, j];
 									}
+
+									var loudong = xiaoqu[j].children;
+									for (var k = 0; k < loudong.length; k++) {
+										// 楼层
+										if (loudong[k].label == parseInt(lc)) {
+											if (depth == 'lc') {
+												// 返回坐标
+												return [i, j, k];
+											}
+
+											var fangjian = loudong[k].rooms;
+											for (var m = 0; m < fangjian.length; m++) {
+												// 房间
+												if (fangjian[m] == fj) {
+													if (depth == 'fj') {
+														// 返回坐标
+														return [i, j, k, m];
+													}
+												}
+											}
+										}
+									}
 								}
 							}
 						}
@@ -1945,7 +2034,7 @@
 					cancelButtonText: '取消',
 					type: 'warning'
 				}).then(() => {
-					let indexs = _this.navData_positioning(id, 'id', 2);
+					let indexs = _this.navData_positioning(id, 'id', 'ld');
 					if (indexs.length > 0) {
 						let i = indexs[0];
 						let j = indexs[1];
@@ -2006,10 +2095,10 @@
 
 							if (res.code == 200) {
 								let labels = _this.form_add_loudong.school;
-								let indexs = _this.navData_positioning(labels, 'label', 1);
+								let indexs = _this.navData_positioning(labels, 'label', 'xq');
 								let i = indexs[0];
 								_this.navData[i].children.push({
-									id: res.data.id, 
+									id: res.data.id,
 									label: _this.form_add_loudong.building,
 									isShow: true,
 									children: []
@@ -2048,11 +2137,11 @@
 
 							if (res.code == 200) {
 								let labels = _this.form_add_louceng.building + '|' + _this.form_add_louceng.school;
-								let indexs = _this.navData_positioning(labels, 'label', 2);
+								let indexs = _this.navData_positioning(labels, 'label', 'ld');
 								let i = indexs[0];
 								let j = indexs[1];
 								_this.navData[i].children[j].children.push({
-									id: res.data.id, 
+									id: res.data.id,
 									label: _this.form_add_louceng.floor,
 									rooms: []
 								});
@@ -2079,34 +2168,43 @@
 			 * 添加房间对话框
 			 */
 			dialog_add_room_click() {
+				var _this = this;
 				this.$refs["add_room_form"].validate(validate => {
 					if (validate) {
 						// console.log(this.form_add_room);
-						addRooms(this.form_add_room).then((res) => {
+						addRooms(_this.form_add_room).then((res) => {
 							// console.log(res);
 							if (typeof res.code == 'undefined' || res.code == '') {
-								this.$message.error('返回数据格式问题,code未获取到!')
-								return
+								_this.$message.error('返回数据格式问题,code未获取到!');
+								return;
 							}
 
 							if (res.code == 200) {
-								this.get_loudong_data()
-								this.$message.success('房间添加成功!')
-								this.dialog_add_room = false
-								let _this = this
+								var school = _this.form_add_room.school;
+								var building = _this.form_add_room.building
+								var floor = _this.form_add_room.floor;
+								var room = _this.form_add_room.room;
+								var arr = floor + '|' + building + '|' + school;
+								var ids = _this.navData_positioning(arr, 'label', 'lc');
+								_this.navData[ids[0]].children[ids[1]].children[ids[2]].rooms.push(building + '-' + room);
+								_this.navData[ids[0]].children[ids[1]].children[ids[2]].rooms.sort();
+								// console.log(_this.navData);
+								// this.get_loudong_data()
+								_this.$message.success('房间添加成功!');
+								_this.dialog_add_room = false;
 								setTimeout(() => {
-									_this.currentData.clickElement.click()
-								}, 1000)
+									_this.currentData.clickElement.click();
+								}, 300)
 							} else {
-								this.$message.error(res.message)
+								_this.$message.error(res.message);
 							}
 						}).catch((err) => {
 							// console.log(err);
-							this.$message.error(err.message)
+							_this.$message.error(err.message);
 						})
 					} else {
-						this.$message.error('验证不通过')
-						return false
+						_this.$message.error('验证不通过');
+						return false;
 					}
 				})
 			},
@@ -2114,36 +2212,33 @@
 			 * 添加空调对话框
 			 */
 			dialog_add_air_click() {
-				this.$refs["add_device_form"].validate(validate => {
+				var _this = this;
+				_this.$refs["add_device_form"].validate(validate => {
 					if (validate) {
-						addAirs(this.form_add_device).then((res) => {
+						addAirs(_this.form_add_device).then((res) => {
 							// console.log(res);
 							if (typeof res.code == 'undefined' || res.code == '') {
-								this.$message.error('返回数据格式问题,code未获取到!')
-								return
+								_this.$message.error('返回数据格式问题,code未获取到!');
+								return;
 							}
 
 							if (res.code == 200) {
-								this.get_loudong_data()
-								this.$message.success('空调设备添加成功!')
-								this.dialog_add_device = false
-								let _this = this
+								// _this.get_loudong_data();
+								_this.$message.success('空调设备添加成功!');
+								_this.dialog_add_device = false;
 								setTimeout(() => {
-									_this.currentData.clickElement.click()
-								}, 1000)
-								setTimeout(() => {
-
+									_this.currentData.clickElement.click();
 								}, 300)
 							} else {
-								this.$message.error(res.message)
+								_this.$message.error(res.message);
 							}
 						}).catch((err) => {
 							// console.log(err);
-							this.$message.error(err.message)
+							_this.$message.error(err.message);
 						})
 					} else {
-						this.$message.error('验证不通过')
-						return false
+						_this.$message.error('验证不通过');
+						return false;
 					}
 				})
 			},
@@ -2151,35 +2246,35 @@
 			 * 刪除空调对话框
 			 */
 			dialog_delete_air_click() {
+				var _this = this;
 				let delAir = {
-					school: this.currentData.school,
-					build: this.currentData.building,
-					floors: this.currentData.floor,
-					dom: this.delAir.room,
-					air_ip: this.delAir.deleteAirIp
+					school: _this.currentData.school,
+					build: _this.currentData.building,
+					floors: _this.currentData.floor,
+					dom: _this.delAir.room,
+					air_ip: _this.delAir.deleteAirIp
 				}
 				// console.log(delAir);
 				deleteAirs(delAir).then((res) => {
 					// console.log(res);
 					if (typeof res.code == 'undefined' || res.code == '') {
-						this.$message.error('返回数据格式问题,code未获取到!')
+						_this.$message.error('返回数据格式问题,code未获取到!')
 						return
 					}
 
 					if (res.code == 200) {
-						this.get_loudong_data()
-						this.$message.success('空调 ' + delAir.air_ip + ' 删除成功!')
-						this.dialog_delete_air = false
-						let _this = this
+						_this.get_loudong_data()
+						_this.$message.success('空调 ' + delAir.air_ip + ' 删除成功!')
+						_this.dialog_delete_air = false
 						setTimeout(() => {
 							_this.currentData.clickElement.click()
 						}, 1000)
 					} else {
-						this.$message.error(res.message)
+						_this.$message.error(res.message)
 					}
 				}).catch((err) => {
 					// console.log(err);
-					this.$message.error(err.message)
+					_this.$message.error(err.message)
 				})
 			},
 			/**