From 8f8155aa4c83f7c2577ac123add550766b6a7ce3 Mon Sep 17 00:00:00 2001
From: heyujie <516346543@qq.com>
Date: 星期一, 07 六月 2021 16:53:19 +0800
Subject: [PATCH] 修改配置

---
 pages/index/index.vue |  160 +++++++++++++++++++++++++++++-----------------------
 1 files changed, 89 insertions(+), 71 deletions(-)

diff --git a/pages/index/index.vue b/pages/index/index.vue
index c2e94b2..db772ff 100644
--- a/pages/index/index.vue
+++ b/pages/index/index.vue
@@ -48,8 +48,7 @@
 					</view> -->
 				<view class="list-item" v-for="(x,i) in wList" @tap="toDetail(x)">
 					<view class="left-img">
-						<!-- <image :src="'/httpImage/' + x.imgUrl" mode=""></image> -->
-						<image :src="'http://192.168.20.10:7009'+'/httpImage/'+ x.imgUrl" mode="">
+						<image :src="'http://'+ getResourceUrl() +'/httpImage/'+ x.imgUrl" mode="">
 						</image>
 					</view>
 					<view class="right-desc">
@@ -72,7 +71,7 @@
 								鏉ヨ嚜锛歿{x.pos}}
 							</view>
 							<view class="time">
-								{{x.time}}
+								{{x.date}} {{x.time}}
 							</view>
 						</view>
 					</view>
@@ -106,21 +105,30 @@
 					wsport: '1883',
 					path: '/ws',
 				},
-				onTopic: 'MIDDOL-TEST', // this.$mqtt.subscribe('MIDDOL-TEST')
+				onTopic: 'MIDDOL-TEST',
 				Qos: 0,
 				sendMassage: 'Hello EMQ-X锛�',
 				time: 0,
 				client: null,
 				activeChannel: "鍏ㄩ儴璁惧",
 				//MQTT杩炴帴鐨勯厤缃�
+				// clientId: 'WebClient-' + parseInt(Math.random() * 100000),
+				// username: 'admin',
+				// password: 'admin123',
+				// useSSL: false,
+				// mqttVersion: 4,
+				// timeout: 3
+
 				options: {
 					wsOptions: {},
 					protocolVersion: 4, //MQTT杩炴帴鍗忚鐗堟湰
 					clientId: 'WebClient-' + parseInt(Math.random() * 100000),
 					username: 'admin',
 					password: 'admin123',
+					keepalive: 60,
+					reconnectPeriod: 100,
 					// reconnectPeriod: 1000, //1000姣锛屼袱娆¢噸鏂拌繛鎺ヤ箣闂寸殑闂撮殧
-					// connectTimeout: 60 * 1000, //1000姣锛屼袱娆¢噸鏂拌繛鎺ヤ箣闂寸殑闂撮殧
+					connectTimeout: 60 * 1000, //1000姣锛屼袱娆¢噸鏂拌繛鎺ヤ箣闂寸殑闂撮殧
 					resubscribe: true //濡傛灉杩炴帴鏂紑骞堕噸鏂拌繛鎺ワ紝鍒欎細鍐嶆鑷姩璁㈤槄宸茶闃呯殑涓婚
 				},
 				showActionSheet: {
@@ -138,56 +146,9 @@
 			bottomDrawer
 		},
 		mounted() {
-			// const connectUrl = 'ws://test.mosquitto.org:8080'
-			// const connectUrl = 'wx://test.mosquitto.org:8080'
-			// const client = mqtt.connect(connectUrl, options)
-			// client.on('connect', function(mes) {
-			//     // 璁㈤槄
-			// 	client.subscribe("testtopic");
-			// })
-
-			// client.on('reconnect', (error) => {
-			// 	console.log('姝e湪閲嶈繛:', error)
-			// })
-
-			// client.on('error', (error) => {
-			// 	console.log('杩炴帴澶辫触:', error)
-			// })
-
-			// client.on('message', (topic, message) => {
-			// 	console.log('鏀跺埌娑堟伅锛�', topic, message.toString())
-			// })
-			let _this = this
-			// var hosts = 'ws://' + this.serve.host + ':' + this.serve.wsport + this.serve.path
-			// var hosts = 'wx://192.168.20.189:15675/ws'
-			var hosts = 'ws://192.168.20.189:15675/ws'
-			const c = mqtt.connect(
-				hosts,
-				_this.options
-			);
-			c.on('connect', function() {
-				// console.log('杩炴帴鎴愬姛');
-				c.subscribe(_this.onTopic, {
-					qos: _this.Qos
-				}, function(error) {
-					if (!error) {
-						// console.log('璁㈤槄鎴愬姛');
-					}
-				});
-			});
-			c.on('reconnect', function(error) {
-				console.log("璧拌繖閲�111")
-			});
-			c.on('error', function(error) {
-				uni.hideLoading();
-			});
-			c.on('message', function(topic, message) {
-				const data = message.toString()
-				// console.log('鏀跺埌鏉ヨ嚜' + topic + '鐨勬秷鎭�' + JSON.parse(data).id);
-				_this.handleMsg(data)
-			});
-
+			this.connectAndSubscribe('ws://' + getApp().globalData.mqUrl + '/ws')
 			// _this.$mqtt.subscribe('MIDDOL-TEST')
+			this.listenIPChange()
 		},
 		// mqtt: {
 		// 	'MIDDOL-TEST'(data) {
@@ -238,6 +199,16 @@
 			this.pullData()
 		},
 		methods: {
+			listenIPChange() {
+				uni.$on('changeClientIP', (data) => {
+					this.disconnect()
+					this.connectAndSubscribe(`ws://${data.host}/ws`)
+					getApp().globalData.mqUrl = data.host
+					uni.$emit("connectStatus", {
+						status: this.client.connected
+					})
+				})
+			},
 			pullData(l, v) {
 				DB.selectTableData('warning_list', l, v).then((res) => {
 					this.wList = res
@@ -245,28 +216,75 @@
 					console.log(6677, e)
 				})
 			},
+			connectAndSubscribe(hosts) {
+				let _this = this
+				console.log(hosts)
+				this.client = mqtt.connect(
+					hosts,
+					_this.options
+				);
+				this.client.on('connect', function() {
+					console.log('杩炴帴鎴愬姛');
+					_this.client.subscribe(_this.onTopic, {
+						qos: _this.Qos
+					}, function(error) {
+						if (!error) {
+							console.log('璁㈤槄鎴愬姛');
+						}
+					});
+				});
+				this.client.on('reconnect', function(error) {
+					console.log("璧拌繖閲�111")
+				});
+				this.client.on('error', function(error) {
+					console.log("杩炴帴澶辫触")
+				});
+				this.client.on('message', function(topic, message) {
+					console.log('鏂板涓�鏉℃暟鎹�')
+					const data = message.toString()
+					_this.handleMsg(data)
+				});
+			},
+			getResourceUrl() {
+				return getApp().globalData.resourseUrl
+			},
+			disconnect() {
+				if (!this.client || !this.client.connected) {
+					console.log('瀹㈡埛绔湭杩炴帴')
+					return;
+				}
+				this.client.end();
+				this.client = null
+				console.log('宸叉柇寮�杩炴帴');
+			},
 			handleMsg(data) {
 				const obj = JSON.parse(data)
-				const [date, time] = obj.updateTime.split(" ")
-				const t = new Date(obj.updateTime)
+				let [date, time] = obj.picDate.split(" ")
+				time = time.substring(0, time.length - 4);
+				const t = new Date(obj.picDate)
 				DB.insertTableData('warning_list',
 					`'${obj.id}', '${time}', '${date}', '${obj.cameraAddr}', '${obj.taskName}', '${obj.alarmRules[0].alarmLevel}', '${obj.picMaxUrl[0]}','${t.getTime()}', 0`
 				).then(() => {
 					console.log('鎴愬姛鎻掑叆涓�鏉℃暟鎹�')
+					if (this.activeChannel == '鍏ㄩ儴璁惧' || this.activeChannel == obj.cameraAddr) {
+						if (this.wList.find(function(item) {
+								return item.id == obj.id
+							}) == undefined) {
+							this.wList.unshift({
+								id: obj.id,
+								time,
+								date,
+								pos: obj.cameraAddr,
+								name: obj.taskName,
+								warningLevel: obj.alarmRules[0].alarmLevel,
+								imgUrl: obj.picMaxUrl[0]
+							})
+						}
+					}
 				}, () => {
 					console.log('鎻掑叆涓�鏉℃暟鎹け璐�')
 				})
-				if (this.activeChannel == '鍏ㄩ儴璁惧' || this.activeChannel == obj.cameraAddr) {
-					this.wList.unshift({
-						id: obj.id,
-						time,
-						date,
-						pos: obj.cameraAddr,
-						name: obj.taskName,
-						warningLevel: obj.alarmRules[0].alarmLevel,
-						imgUrl: obj.picMaxUrl[0]
-					})
-				}
+
 			},
 			goFrontPage() {
 				uni.navigateTo({
@@ -288,13 +306,13 @@
 					})
 				})
 				plus.sqlite.selectSql({
-					name: 'warns',
-					sql: `select id from warning_list`,
+					name: "warns",
+					sql: 'select id from warning_list',
 					success(e) {
-						console.log('鎵�鏈塱d锛氾細', e)
+						console.log('鎵�鏈塱d', e);
 					},
 					fail(e) {
-						console.log('鍑洪敊浜�', e)
+						console.log('澶辫触鎵�鏈塱d', e);
 					}
 				})
 			},

--
Gitblit v1.8.0