From 1efff531ba544af15f009057503c000f218c1534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89?= <814457906@qq.com> Date: Thu, 9 Nov 2023 11:35:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagesC/addHeartRate/addHeartRate.vue | 94 +++++++++++++++++++++++++- pagesC/addPressure/addPressure.vue | 99 +++++++++++++++++++++++++++- pagesC/addsugar/addsugar.vue | 2 +- 3 files changed, 188 insertions(+), 7 deletions(-) diff --git a/pagesC/addHeartRate/addHeartRate.vue b/pagesC/addHeartRate/addHeartRate.vue index a42a8d8..f241562 100644 --- a/pagesC/addHeartRate/addHeartRate.vue +++ b/pagesC/addHeartRate/addHeartRate.vue @@ -37,8 +37,24 @@ hr: '', timetext: this.formatDate(Date.now()), measureTime: this.formatDate(Date.now()), + uploadType: undefined, + timeoutObj: null, + socketOpen: false, + SOCKETURL: '', }; }, + onLoad(options) { + bound(uni.getStorageSync('userinfo').cardNo).then(res => { + if (res.code == 200) { + res.data.forEach(e => { + if (e.deviceType == 1) { + this.SOCKETURL = `ws://112.6.122.71:8009/fd/webSocket/${e.sn}` + this.scoket(); + } + }) + } + }) + }, methods: { formatDate(timestamp) { const now = new Date(parseInt(timestamp)) @@ -55,11 +71,78 @@ timeconfirm(time) { this.timetext = time }, + scoket() { + const that = this + this.socketOpen = false + try { + uni.connectSocket({ + url: that.SOCKETURL + }) + uni.onSocketOpen(res => { + console.log('webScoket连接已打开', res); + that.socketOpen = true + that.reset() + }) + + uni.onSocketError(err => { + console.log('webScoket连接打开失败', err); + if (err && err.code != 1000) { + setTimeout(() => { + that.socketOpen = true + uni.connectSocket({ + url: that.SOCKETURL + }) + }, 3 * 1000) + } + }) + + uni.onSocketClose(err => { + console.log('webScoket连接关闭', err); + if (err && err.code !== 1000) { + setTimeout(() => { + that.socketOpen = true + uni.connectSocket({ + url: that.SOCKETURL + }) + }, 3 * 1000) + } + }) + uni.onSocketMessage(res => { + console.log("webScoket监听收到的信息", res); + this.uploadType = 2 + this.hr = JSON.parse(res.data).pulse + this.$forceUpdate() + }) + } catch (e) { + console.log(e); + } + }, + // 心跳响应 + reset() { + let that = this + clearInterval(that.timeoutObj); + that.timeoutObj = setInterval(() => { + uni.sendSocketMessage({ + data: 'ping', + success(res) { + console.log('正在发送心跳'); + }, + fail(err) { + console.log('心跳发送失败,重新连接...'); + console.log(this.SOCKETURL) + that.socketOpen = true + uni.connectSocket({ + url: this.SOCKETURL + }) + } + }) + }, 60000) + }, submit() { if (this.hr) { let data = { hr: this.hr, - uploadType: 1, + uploadType: this.uploadType ? this.uploadType : 1, identity: uni.getStorageSync('userinfo').cardNo, measureTime: this.timetext, } @@ -91,7 +174,12 @@ return true } } - } + }, + onUnload() { + if (this.socketOpen) { + uni.closeSocket(); + } + }, } @@ -211,4 +299,4 @@ } } } - + \ No newline at end of file diff --git a/pagesC/addPressure/addPressure.vue b/pagesC/addPressure/addPressure.vue index d27dc7c..22b4644 100644 --- a/pagesC/addPressure/addPressure.vue +++ b/pagesC/addPressure/addPressure.vue @@ -49,6 +49,9 @@ import { savebp } from '@/api/examinationapi/add.js' + import { + bound, + } from '@/api/examinationapi/add.js' export default { data() { return { @@ -57,8 +60,24 @@ hr: '', timetext: this.formatDate(Date.now()), measureTime: this.formatDate(Date.now()), + uploadType: undefined, + timeoutObj: null, + socketOpen: false, + SOCKETURL: '', }; }, + onLoad(options) { + bound(uni.getStorageSync('userinfo').cardNo).then(res => { + if (res.code == 200) { + res.data.forEach(e => { + if (e.deviceType == 1) { + this.SOCKETURL = `ws://112.6.122.71:8009/fd/webSocket/${e.sn}` + this.scoket(); + } + }) + } + }) + }, methods: { formatDate(timestamp) { const now = new Date(parseInt(timestamp)) @@ -75,6 +94,75 @@ timeconfirm(time) { this.timetext = time }, + scoket() { + const that = this + this.socketOpen = false + try { + uni.connectSocket({ + url: that.SOCKETURL + }) + uni.onSocketOpen(res => { + console.log('webScoket连接已打开', res); + that.socketOpen = true + that.reset() + }) + + uni.onSocketError(err => { + console.log('webScoket连接打开失败', err); + if (err && err.code != 1000) { + setTimeout(() => { + that.socketOpen = true + uni.connectSocket({ + url: that.SOCKETURL + }) + }, 3 * 1000) + } + }) + + uni.onSocketClose(err => { + console.log('webScoket连接关闭', err); + if (err && err.code !== 1000) { + setTimeout(() => { + that.socketOpen = true + uni.connectSocket({ + url: that.SOCKETURL + }) + }, 3 * 1000) + } + }) + uni.onSocketMessage(res => { + console.log("webScoket监听收到的信息", res); + this.uploadType = 2 + this.dbp = JSON.parse(res.data).dbp + this.sbp = JSON.parse(res.data).sbp + this.hr = JSON.parse(res.data).pulse + this.$forceUpdate() + }) + } catch (e) { + console.log(e); + } + }, + // 心跳响应 + reset() { + let that = this + clearInterval(that.timeoutObj); + that.timeoutObj = setInterval(() => { + uni.sendSocketMessage({ + data: 'ping', + success(res) { + console.log('正在发送心跳'); + }, + fail(err) { + console.log('心跳发送失败,重新连接...'); + console.log(this.SOCKETURL) + that.socketOpen = true + uni.connectSocket({ + url: this.SOCKETURL + }) + } + }) + }, 60000) + }, submit() { if (this.ruls(this.dbp) && this.ruls(this.sbp) && this.ruls(this.hr)) { let data = { @@ -83,7 +171,7 @@ hr: this.hr, identity: uni.getStorageSync('userinfo').cardNo, measureTime: this.timetext, - uploadType: 1, + uploadType: this.uploadType ? this.uploadType : 1, } savebp(data).then(res => { if (res.code == 200) { @@ -112,8 +200,13 @@ } else { return true } + }, + }, + onUnload() { + if (this.socketOpen) { + uni.closeSocket(); } - } + }, } @@ -228,4 +321,4 @@ } } } - + \ No newline at end of file diff --git a/pagesC/addsugar/addsugar.vue b/pagesC/addsugar/addsugar.vue index 001751b..f44fd55 100644 --- a/pagesC/addsugar/addsugar.vue +++ b/pagesC/addsugar/addsugar.vue @@ -316,4 +316,4 @@ } } } - + \ No newline at end of file