Explorar o código

优化websocket相关逻辑

wenningning %!s(int64=2) %!d(string=hai) anos
pai
achega
41c251b21e
Modificáronse 3 ficheiros con 90 adicións e 95 borrados
  1. 71 90
      sleep/pages/home/index.vue
  2. 14 3
      sleep/pages/my/index.vue
  3. 5 2
      sleep/pages/report/report.vue

+ 71 - 90
sleep/pages/home/index.vue

@@ -357,7 +357,9 @@ export default {
       bindSn: '',
       bindShow: false,
       modalName: false,
-      activeBreak: false
+      activeBreak: false,
+      isConnect: false,
+      showCount: 0
 		};
 	},
   // onLoad() {
@@ -404,14 +406,6 @@ export default {
     this.activeBreak = true
     this.closeWebsocket()
   },
-  watch: {
-    connected() {
-      if (!this.activeBreak && !this.connected) {
-        console.log('监听到webSocket连接断开,现在开始连接')
-        this.init()
-      }
-    }
-  },
   // onShow() {
   //   const imei = Storage.getItem('nowChangeDevice')
   //   if (imei) {
@@ -419,16 +413,34 @@ export default {
   //   }
   // },
   onShow() {
+    this.showCount ++
+    if (this.showCount === 3 && this.memberId) {
+      this.showCount = 0
+      this.API_getDeviceList()
+    }
     const imei = Storage.getItem('nowChangeDevice')
     if (imei) {
       this.homeFrom.nowDevice = imei
       this.deviceAdded = true
-      if (!this.connected && this.memberId) {
+    } else {
+      this.deviceAdded = false
+    }
+    if (Storage.getItem('uid')) {
+      if (this.connected) {
+        console.log('发送确认在线消息...')
+        this.mySocketTask.send({data: '1'})
+        const _this = this
+        this.isConnect = false
+        setTimeout(() => {
+          if (_this.isConnect === false) {
+            console.log('没有正常返回...')
+            _this.connect()
+          }
+        }, 1000)
+      } else {
         console.log('重新连接...')
         this.connect()
       }
-    } else {
-      this.deviceAdded = false
     }
   },
 
@@ -444,6 +456,8 @@ export default {
         if (res) {
           if (res.length === 0) {
             _this.deviceAdded = false
+            Storage.removeItem("nowChangeDevice")
+            Storage.removeItem("myBindDevices")
           } else {
             _this.deviceAdded = true
             _this.deviceList = res
@@ -469,10 +483,15 @@ export default {
     },
     connect() {
       let _this = this
+      if (this.connected || this.connecting) {
+        console.log('webSocket已连接或者正在连接')
+        return
+      }
       this.connecting = true
       let wsUrl = API.base.replace('http', 'ws')
+      const time = Math.round(new Date()) + "" + Math.round(Math.random()*100)
       this.mySocketTask = uni.connectSocket({
-        url: wsUrl + '/sleep_report/monitor/' + this.memberId,
+        url: wsUrl + '/sleep_report/monitor/' + time + '/' + this.memberId,
         // url: 'ws://wdkl.natapp1.cc/sleep_report/monitor/' + this.memberId,
         success(res) {
           // 这里是接口调用成功的回调,不是连接成功的回调,请注意
@@ -510,39 +529,44 @@ export default {
       this.mySocketTask.onMessage(res => {
         let msg = res.data
         // console.log('收到消息:', msg)
-        if (msg.length === 234) {
-          if (this.homeFrom.nowDevice !== msg.substring(2, 14)) {
-            return
-          }
-          this.homeFrom.status = '在线'
-          this.lastMsgTime = Date.parse(new Date())
-          this.getValue(msg)
-          // this.getValue1(parseInt(msg.substring(22, 24), 16))
-          // this.getValue2(parseInt(msg.substring(24, 26), 16))
-          switch (msg.substring(26, 28)) {
-            case "03":
-              this.sleepStatus = '在床'
-              break;
-            case "04":
-              this.sleepStatus = '离床'
-              break;
-            case "05":
-              this.sleepStatus = '打鼾'
-              break;
-            case "06":
-              this.sleepStatus = '体动'
-              break;
-            default:
-              break;
-          }
-        } else if (msg.length === 7) {
-          Storage.setItem('refreshSleepReport', true)
-        } else {
-          const data =  JSON.parse(res.data)
-          console.log('收到消息为:', data)
-          if (data.sn && this.homeFrom.nowDevice === data.sn) {
-            this.list_alerts.push(data)
-          }
+        switch (msg.length) {
+          case 1:
+            this.isConnect = true
+                break
+          case 234:
+            if (this.homeFrom.nowDevice !== msg.substring(2, 14)) {
+              return
+            }
+            this.homeFrom.status = '在线'
+            this.lastMsgTime = Date.parse(new Date())
+            this.getValue(msg)
+            switch (msg.substring(26, 28)) {
+              case "03":
+                this.sleepStatus = '在床'
+                break;
+              case "04":
+                this.sleepStatus = '离床'
+                break;
+              case "05":
+                this.sleepStatus = '打鼾'
+                break;
+              case "06":
+                this.sleepStatus = '体动'
+                break;
+              default:
+                break;
+            }
+            break
+          case 7:
+            Storage.setItem('refreshSleepReport', true)
+                break
+          default:
+            const data =  JSON.parse(res.data)
+            console.log('收到消息为:', data)
+            if (data.sn && this.homeFrom.nowDevice === data.sn) {
+              this.list_alerts.push(data)
+            }
+            break
         }
       })
       this.mySocketTask.onClose(res => {
@@ -633,7 +657,7 @@ export default {
             }
           },
           max: function (value) {
-            return value.max + 20
+            return value.max + 30
           },
           // min: 0,
           // min: 'dataMin', // 取数据在该轴上的最大值作为最小刻度
@@ -722,49 +746,6 @@ export default {
       this.myChart1.setOption(this.options1)
       this.myChart2.setOption(this.options2)
     },
-    getValue1(value) {
-      // console.log("心率是", value)
-      this.pjxl = value
-      if (this.myData1.length > 60) {
-        this.myData1.splice(0,5)
-      }
-      if (value !== 0) {
-        for (let i = 0; i < 5; i++) {
-          this.myCount1 ++;
-          value =  parseInt(Math.random(100) * 500 + 300)
-          this.myData1.push([this.myCount1, value])
-        }
-      } else {
-        for (let i = 0; i < 5; i++) {
-          this.myCount1 ++;
-          value = 410 + parseInt(Math.random() * 50 - 30)
-          this.myData1.push([this.myCount1, value])
-        }
-      }
-      this.myChart1.setOption(this.options1)
-    },
-    getValue2(value) {
-      // console.log("呼吸值是", value)
-      this.pjhx = value
-      if (this.myData2.length > 60) {
-        this.myData2.splice(0,5)
-      }
-      if (value === 0) {
-        for (let i = 0; i < 5; i++) {
-          this.myCount2 ++;
-          value = 220 +  parseInt(Math.random() * 10 - 5)
-          this.myData2.push([this.myCount2, value])
-        }
-      } else {
-        value = parseInt((Math.random(10) * 200 + 100))
-        for (let i = 0; i < 5; i++) {
-          this.myCount2 ++;
-          value += parseInt(Math.random() * 10 - 5)
-          this.myData2.push([this.myCount2, value])
-        }
-      }
-      this.myChart2.setOption(this.options2)
-    },
 		handleAlertSetting() {
 			uni.navigateTo({
 				url: "/pages/alertSetting/alertSetting?imei=" + this.homeFrom.nowDevice

+ 14 - 3
sleep/pages/my/index.vue

@@ -317,14 +317,22 @@ export default {
       bindShow: false,
       updateShow: false,
       userInfo: null,
-      nickname: ''
+      nickname: '',
+      showCount: 0
     };
   },
+  onShow() {
+    this.deviceList = Storage.getItem('myBindDevices')
+    this.showCount ++
+    if (this.showCount === 3 && this.memberId) {
+      this.showCount = 0
+      this.API_getDeviceList()
+    }
+  },
   mounted() {
     this.memberId = Storage.getItem('uid')
     if (this.memberId) {
       this.userInfo = Storage.getItem('userInfo')
-      this.deviceList = Storage.getItem('myBindDevices')
       this.unionId = this.userInfo.union_id
       if (this.deviceList) {
         console.log('this.deviceList===', this.deviceList)
@@ -365,7 +373,10 @@ export default {
     API_getDeviceList() {
       const _this = this
       API_deviceMemberBind.getDeviceByDtypeAndMemberId({dtype: '睡眠床垫', memberId: this.memberId}).then(res => {
-        if (res.length > 0) {
+        if (res.length === 0) {
+          Storage.removeItem("nowChangeDevice")
+          Storage.removeItem("myBindDevices")
+        } else {
           _this.deviceList = res
           Storage.setItem("nowChangeDevice", res[0].imei)
           Storage.setItem("myBindDevices", res)

+ 5 - 2
sleep/pages/report/report.vue

@@ -911,7 +911,10 @@
       API_getDeviceList() {
         const _this = this
         API_deviceMemberBind.getDeviceByDtypeAndMemberId({dtype: '睡眠床垫', memberId: this.memberId}).then(res => {
-          if (res.length > 0) {
+          if (res.length === 0) {
+            Storage.removeItem("nowChangeDevice")
+            Storage.removeItem("myBindDevices")
+          } else {
             Storage.setItem("nowChangeDevice", res[0].imei)
             Storage.setItem("myBindDevices", res)
           }
@@ -1211,7 +1214,7 @@
 		background-color: #000000;
 		padding-bottom: 50px;
 		height: 100%;
-		
+
 		.image-wrapper_2 {
 			background: url("http://wdklmall.oss-cn-shenzhen.aliyuncs.com/mallgoods/goods/560AFD2D700A40F19029F9FA2FD4612E.png");
 			background-repeat: no-repeat;