Browse Source

1、改造webSocket连接;
2、其他优化;

wenningning 1 year ago
parent
commit
a79fcec8c3

+ 9 - 0
sleep/api/device_member_bind.js

@@ -12,6 +12,15 @@ export function getDeviceByDtypeAndMemberId(params){
     })
 }
 
+// 获取根据设备类型用户绑定的设备及在线情况
+export function getDeviceOnlineByDtypeAndMemberId(params){
+    return request({
+        url: `care/care_device_member_bind/get_devices_online_by_dtype_member_id`,
+        config: {method: Method.GET, needToken: true, loading: true},
+        params
+    })
+}
+
 // 根据设备类型绑定设备
 export function bindDeviceByDtype(params){
     return request({

+ 44 - 4
sleep/pages/log/report/friendRealTime.vue

@@ -80,6 +80,10 @@
           </view>
         </view>
       </view>
+
+      <button class="bg-blue margin-lg" @click="handleReportClick">查看睡眠报告</button>
+
+      <button class="bg-blue margin-left-lg margin-right-lg" @click="handleWarnLogClick">查看报警记录</button>
     </view>
     <view style="z-index: 9999999" class="cu-modal" :class="modalName ?'show':''" @tap="hideModal">
       <view style="background-image: linear-gradient(45deg, #4d9efa, #156fce);z-index: 9999999" class="cu-dialog" @tap.stop="">
@@ -137,12 +141,15 @@ export default {
       lastMsgTime: 0, // 最后一次设备消息时间
       memberId: '',
       modalName: false,
-      isConnect: false
+      isConnect: false,
+      unionId: null,
+      mySns: null
 		};
 	},
   onLoad(option) {
-    const { uid } = option
+    const { uid, unionId } = option
     this.memberId = uid
+    this.unionId = unionId
     this.API_getDeviceList()
   },
   beforeDestroy() {
@@ -184,7 +191,8 @@ export default {
       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/' + time + '/' + parseInt(this.memberId),
+        url: wsUrl + '/sleep_report/monitor/' + time ,
+        // url: wsUrl + '/sleep_report/monitor/' + time + '/' + parseInt(this.memberId),
         success(res) {
           // 这里是接口调用成功的回调,不是连接成功的回调,请注意
         },
@@ -205,7 +213,8 @@ export default {
             _this.pjhx = '--'
           }
           _this.mySocketTask.send({data: 'ping'})
-        }, 30000) // 30秒的心跳包
+        }, 60000) // 30秒的心跳包
+        this.sendSns()
       })
       this.mySocketTask.onError(err => {
         this.connecting = false
@@ -220,6 +229,7 @@ export default {
       })
       this.mySocketTask.onMessage(res => {
         let msg = res.data
+        // console.log('收到消息2222:', msg)
         switch (msg.length) {
           case 1:
             this.isConnect = true
@@ -258,6 +268,26 @@ export default {
         clearInterval(this.timer)
       })
     },
+    sendSns() {
+      if (this.deviceList.length > 0) {
+        let sns = []
+        this.deviceList.forEach(item => {
+          sns.push(item.imei)
+        })
+        const data = sns.join(',')
+        if (!this.mySns) {
+          this.mySns = data
+        } else {
+          if (data === this.mySns) {
+            return
+          }
+        }
+        this.mySocketTask.send({data: data})
+      } else {
+        this.mySns = ''
+        this.mySocketTask.send({data: ''})
+      }
+    },
     nowChart1() {
       this.onInit1(this.chartObj1)
       let canvas = this.$refs.xinlvRefChart.canvas
@@ -427,6 +457,16 @@ export default {
     },
     RadioChange(e) {
       this.homeFrom.nowDevice = e.detail.value
+    },
+    handleReportClick() {
+      uni.navigateTo({
+        url: '/pages/log/report/friendReport?uid=' + this.uid + '&unionId=' + this.unionId
+      })
+    },
+    handleWarnLogClick() {
+      uni.navigateTo({
+        url: '/pages/log/warn/index?unionId=' + this.unionId
+      })
     }
 	}
 };

+ 0 - 6
sleep/pages/log/report/friendReport.vue

@@ -355,7 +355,6 @@
         </view>
 
       </view>
-      <button v-if="uid" class="bg-blue margin" @click="handleRealTimeClick">查看实时状态</button>
 
     </view>
     <view class="cu-modal bottom-modal" :class="calendarShow === true?'show':''" @tap="hideModal">
@@ -439,11 +438,6 @@ export default {
         url: '/pages/log/report/weekReport'
       })
     },
-    handleRealTimeClick() {
-      uni.navigateTo({
-        url: '/pages/log/report/friendRealTime?uid=' + this.uid
-      })
-    },
     // 获取睡眠报告
     getNewSleepReport() {
       const _this = this

+ 2 - 2
sleep/pages/tabBar/find.vue

@@ -94,8 +94,8 @@ export default {
     getCareHealthAdvert() {
       const data = {
         groupId: 12,
-        // named: 'MATTRESS_FIND_1'
-        named: 'MATTRESS_FIND_2'
+        named: 'MATTRESS_FIND_1'
+        // named: 'MATTRESS_FIND_2'
       }
       API_doctorAdvert.listByGroupId(data).then(res => {
         this.healthAdvertList = res

+ 55 - 15
sleep/pages/tabBar/home.vue

@@ -366,7 +366,9 @@ export default {
       bindShow: false,
       modalName: false,
       activeBreak: false,
-      isConnect: false
+      isConnect: false,
+      mySns: '',
+      closed: false
 		};
 	},
   // onLoad() {
@@ -413,9 +415,6 @@ export default {
     this.closeWebsocket()
   },
   onShow() {
-    if (this.memberId) {
-      this.API_getDeviceList()
-    }
     const imei = Storage.getItem('nowChangeDevice')
     if (imei) {
       this.homeFrom.nowDevice = imei
@@ -424,6 +423,7 @@ export default {
       this.deviceAdded = false
     }
     if (this.memberId) {
+      this.API_getDeviceList()
       if (this.connected) {
         console.log('发送确认在线消息...')
         this.mySocketTask.send({data: '1'})
@@ -440,8 +440,10 @@ export default {
           }
         }, 1000)
       } else {
-        console.log('重新连接...')
-        this.connect()
+        if (this.deviceList.length > 0) {
+          console.log('重新连接...')
+          this.connect()
+        }
       }
     }
   },
@@ -451,13 +453,18 @@ export default {
       const _this = this
       API_deviceMemberBind.getDeviceByDtypeAndMemberId({dtype: deviceDtypeEnum.DEVICE_TYPE.SMCD, memberId: this.memberId}).then(res => {
         if (res) {
+          _this.deviceList = res
           if (res.length === 0) {
             _this.deviceAdded = false
+            if (this.homeFrom.nowDevice) {
+              _this.homeFrom.nowDevice = ''
+              _this.sendSns()
+            }
             Storage.removeItem("nowChangeDevice")
             Storage.removeItem("myBindDevices")
           } else {
             _this.deviceAdded = true
-            _this.deviceList = res
+            _this.sendSns()
             let nowDevice = Storage.getItem("nowChangeDevice")
             if (!nowDevice) {
               Storage.setItem("nowChangeDevice", res[0].imei)
@@ -489,10 +496,10 @@ export default {
       }
       this.connecting = true
       let wsUrl = API.base.replace('http', 'ws')
-      const time = Math.round(new Date()) + "" + Math.round(Math.random()*100)
+      const time = Math.round(new Date()) + "" + Math.round(Math.random()*100000)
       this.mySocketTask = uni.connectSocket({
-        url: wsUrl + '/sleep_report/monitor/' + time + '/' + this.memberId,
-        // url: 'ws://wdkl.natapp1.cc/sleep_report/monitor/' + this.memberId,
+        // url: wsUrl + '/sleep_report/monitor/' + time + '/' + this.memberId,
+        url: wsUrl + '/sleep_report/monitor/' + time ,
         success(res) {
           // 这里是接口调用成功的回调,不是连接成功的回调,请注意
         },
@@ -513,7 +520,8 @@ export default {
             _this.pjhx = '--'
           }
           _this.mySocketTask.send({data: 'ping'})
-        }, 30000) // 30秒的心跳包
+        }, 60000) // 60秒的心跳包
+        this.sendSns()
       })
       this.mySocketTask.onError(err => {
         this.connecting = false
@@ -531,9 +539,11 @@ export default {
         // console.log('收到消息:', msg)
         switch (msg.length) {
           case 1:
+            // 判断是否连接
             this.isConnect = true
-                break
+            break
           case 234:
+            // 实时睡眠数据
             if (this.homeFrom.nowDevice !== msg.substring(2, 14)) {
               return
             }
@@ -563,7 +573,7 @@ export default {
           case 13: // REMOVE_DEVICE 有其他人绑定了这台设备
                 _this.API_getDeviceList()
                 break
-          default:
+          default: // 报警信息
             const data =  JSON.parse(res.data)
             console.log('收到消息为:', data)
             if (data.sn && this.homeFrom.nowDevice === data.sn) {
@@ -575,10 +585,40 @@ export default {
       this.mySocketTask.onClose(res => {
         console.log('webSocket关闭了...')
         this.connected = false
-        this.msg = false
+        this.closed = true
         clearInterval(this.timer)
       })
     },
+    sendSns() {
+      if (this.connected) {
+        console.log('......................')
+        if (this.deviceList.length > 0) {
+          let sns = []
+          this.deviceList.forEach(item => {
+            sns.push(item.imei)
+          })
+          const data = sns.join(',')
+          console.log(data)
+          console.log(this.mySns)
+          if (!this.mySns) {
+            this.mySns = data
+          } else {
+            if (data === this.mySns && !this.closed) {
+              return
+            } else {
+              this.closed = false
+              this.mySns = data
+            }
+          }
+          console.log('发送imei给服务器...')
+          this.mySocketTask.send({data: data})
+        } else {
+          this.mySns = ''
+          this.mySocketTask.send({data: ''})
+        }
+      }
+
+    },
     nowChart1() {
       this.onInit1(this.chartObj1)
       let canvas = this.$refs.xinlvRefChart.canvas
@@ -922,7 +962,7 @@ export default {
         })
         return
       }
-      this.closeWebsocket()
+      // this.closeWebsocket()
       uni.navigateTo({
         url: '/pages/user/shareUser/index'
       })

+ 11 - 4
sleep/pages/tabBar/my.vue

@@ -245,6 +245,9 @@
               <view class="title">昵称</view>
               <input type="text" v-model="nickname" class="text-left" placeholder="请输入昵称"/>
             </view>
+            <view class="cu-form-group">
+              <view class="text-df text-gray">设置昵称,以便播报,6字以内</view>
+            </view>
           </form>
         </view>
         <view class="cu-bar bg-white justify-end">
@@ -267,7 +270,7 @@
           <form>
             <view class="cu-form-group solid-bottom">
               <view class="title">使用人</view>
-              <input type="text" v-model="updateDeviceFrom.userName" class="text-left" placeholder="请输入昵称" :maxlength="4"/>
+              <input type="text" v-model="updateDeviceFrom.userName" class="text-left" placeholder="请输入昵称" :maxlength="6"/>
             </view>
           </form>
         </view>
@@ -338,7 +341,7 @@ export default {
     this.memberId = Storage.getItem('uid')
     if (this.memberId) {
       this.unionId = this.userInfo.union_id
-      if (this.userInfo.nickname.length > 4 || this.userInfo.mobile === this.userInfo.nickname || this.userInfo.nickname === '微信用户') {
+      if (this.userInfo.nickname.length > 6 || this.userInfo.mobile === this.userInfo.nickname || this.userInfo.nickname === '微信用户') {
         const _this = this
         uni.showModal({
           title: '提示',
@@ -648,6 +651,10 @@ export default {
         })
         return
       }
+      if (this.nickname.length > 6) {
+        uni.showToast({title: '昵称长度不能超过6个', icon: 'none', duration: 3000})
+        return
+      }
       this.updateShow = false
       const params = {
         face: this.userInfo.face,
@@ -706,8 +713,8 @@ export default {
         uni.showToast({title: '请输入使用人昵称', icon: 'none', duration: 3000})
         return
       }
-      if (this.updateDeviceFrom.userName.length > 4) {
-        uni.showToast({title: '昵称长度不能超过4个', icon: 'none', duration: 3000})
+      if (this.updateDeviceFrom.userName.length > 6) {
+        uni.showToast({title: '昵称长度不能超过6个', icon: 'none', duration: 3000})
         return
       }
       this.updateUserNameShow = false

+ 8 - 1
sleep/pages/user/alertSetting/index.vue

@@ -203,6 +203,9 @@
               <view class="title">昵称</view>
               <input type="text" v-model="userInfo.nickname" class="text-left" placeholder="请输入昵称"/>
             </view>
+            <view class="cu-form-group">
+              <view class="text-df text-gray">设置昵称,以便播报,6字以内</view>
+            </view>
           </form>
         </view>
         <view class="cu-bar bg-white justify-end">
@@ -266,7 +269,7 @@ export default {
     if (this.userInfo) {
       this.API_getSwitch()
       this.switchFrom.member_union_id = this.userInfo.union_id
-      if (this.userInfo.nickname.length > 4 || this.userInfo.mobile === this.userInfo.nickname || this.userInfo.nickname === '微信用户') {
+      if (this.userInfo.nickname.length > 6 || this.userInfo.mobile === this.userInfo.nickname || this.userInfo.nickname === '微信用户') {
         const _this = this
         uni.showModal({
           title: '提示',
@@ -403,6 +406,10 @@ export default {
         })
         return
       }
+      if (this.nickname.length > 6) {
+        uni.showToast({title: '昵称长度不能超过6个', icon: 'none', duration: 3000})
+        return
+      }
       this.updateShow = false
       const _this = this
       this.saveUserInfo({nickname: this.userInfo.nickname}).then(() => {

+ 0 - 38
sleep/pages/user/broadcastDevice/index.js

@@ -1,38 +0,0 @@
-import request, { Method } from '@/utils/request'
-
-/**
- * 获取播报设备列表
- * @param params
- * @returns {Promise<any>}
- */
-export function getList(params) {
-    return request({
-        url: 'care/mqtt_device/get_list',
-        config: {method: Method.GET, needToken: true, loading: true}
-    })
-}
-
-/**
- * 新增播报设备
- * @param params
- * @returns {Promise<any>}
- */
-export function add(params) {
-    return request({
-        url: 'care/mqtt_device',
-        config: {method: Method.POST, needToken: true, loading: true},
-        params
-    })
-}
-
-/**
- * 删除播报设备
- * @param ids
- * @returns {Promise<any>}
- */
-export function del(ids) {
-    return request({
-        url: `care/mqtt_device/${ids}`,
-        config: {method: Method.DELETE, needToken: true, loading: true}
-    })
-}

+ 18 - 7
sleep/pages/user/broadcastDevice/index.vue

@@ -18,9 +18,18 @@
                     <text class="text_6 text_7">{{ formatTime(item.create_time) }}</text>
                   </view>
                 </view>
-                <view class="flex-col justify-start items-center bg-yellow" style="border-radius: 6.25rem;padding: 0.25rem 0;width: 3.75rem;"
-                      @click="getPages('/pages/log/iot/index?imei=' + item.imei + '&dtype=' + dtype)"><text class="text_4">卡片详情</text></view>
-                <view class="flex-col justify-start items-center text-wrapper" @click="deleteById(item.bind_id)"><text class="text_4">解绑</text></view>
+                <view class="flex-col justify-start items-center" :class="item.bool_online ? 'bg-green' : 'bg-red'" style="border-radius: 6.25rem;padding: 0.25rem 0;width: 2.35rem;">
+                  <text v-if="item.bool_online" class="text_4">在线</text>
+                  <text v-else class="text_4">离线</text>
+                </view>
+                <view class="flex-col justify-start items-center">
+                  <view class="text-wrapper margin-bottom">
+                    <text class="text_4"  @click="getPages('/pages/log/iot/index?imei=' + item.imei + '&dtype=' + dtype)">卡片详情</text>
+                  </view>
+                  <view class="text-wrapper margin-top-xl">
+                    <text class="text_4" @click="deleteById(item.bind_id)">解绑</text>
+                  </view>
+                </view>
               </view>
 
               <button style="width: 100%;margin-top: 10upx" class="flex-row justify-center items-center section_7 space-x-10" @click="bindShow = 'show'">
@@ -66,6 +75,7 @@
 import * as Foundation from '@/ui-utils/Foundation'
 import * as API_deviceMemberBind from '@/api/device_member_bind'
 import * as deviceDtypeEnum from '@/utils/enum/DeviceDTypeEnum'
+import {getDeviceOnlineByDtypeAndMemberId} from "../../../api/device_member_bind";
 export default {
   name: "broadcastDevice",
   data() {
@@ -86,7 +96,7 @@ export default {
   methods: {
     API_getList() {
       let _this = this
-      API_deviceMemberBind.getDeviceByDtypeAndMemberId({dtype: this.dtype, memberId: this.memberId}).then(res => {
+      API_deviceMemberBind.getDeviceOnlineByDtypeAndMemberId({dtype: this.dtype, memberId: this.memberId}).then(res => {
           _this.deviceList = res
       })
     },
@@ -262,11 +272,12 @@ export default {
             }
           }
           .text-wrapper {
-            padding: 0.25rem 0;
+            //padding: 0.25rem 0;
             background-image: linear-gradient(90deg, #2c3ff2 0%, #1e09de 100%);
             border-radius: 6.25rem 0px 0px 6.25rem;
-            width: 2.75rem;
-            height: 1.25rem;
+            width: 3.35rem;
+            height: 1.50rem;
+            text-align: center;
           }
           .text_4 {
             color: #ffffff;

+ 32 - 10
sleep/pages/user/shareUser/index.vue

@@ -66,7 +66,15 @@
                     <text class="text_6 text_7">{{ formatTime(item.create_time) }}</text>
                   </view>
                 </view>
-                <view class="flex-col justify-start items-center text-wrapper" @click="lookReport(item.union_id, item.friend_id)"><text class="text_4">查看</text></view>
+                <view class="flex-col justify-start">
+                  <view class="text-wrapper2 bg-button margin-bottom">
+                    <text class="text_4"  @click="lookReport(item.union_id, item.friend_id)">查看</text>
+                  </view>
+                  <view class="text-wrapper2 bg-button2 margin-top-xl">
+                    <text class="text_4" @click="deleteById(item.id)">取消</text>
+                  </view>
+
+                </view>
               </view>
 
               <view class="flex-row justify-center items-center section_7 space-x-10" @click="modalName ='DialogModal'">
@@ -201,7 +209,7 @@ import * as API_vitalSignsLook from '@/api/vitalSignsLook'
         let _this = this
         uni.showModal({
           title: '警告',
-          content: '删除操作后数据不可复原,您确定要删除此数据?',
+          content: this.tabCur === 2 ? '删除操作后该亲友将不再接收预警消息,是否删除?' : '取消后不能再查看该用户睡眠报告,是否取消?',
           success: function (res) {
             if (res.confirm) {
               API_vitalSignsLook.deleteLogLook(id).then(
@@ -281,7 +289,7 @@ import * as API_vitalSignsLook from '@/api/vitalSignsLook'
       },
       lookReport(unionId, uid) {
         uni.navigateTo({
-          url: '/pages/log/report/friendReport?unionId=' + unionId + '&uid=' + uid
+          url: '/pages/log/report/friendRealTime?unionId=' + unionId + '&uid=' + uid
         })
       }
 		},
@@ -367,13 +375,20 @@ import * as API_vitalSignsLook from '@/api/vitalSignsLook'
             background-image: linear-gradient(90deg, #2c3ff2 0%, #1e09de 100%);
             border-radius: 6.25rem 0px 0px 6.25rem;
             width: 2.75rem;
-            height: 1.25rem;
-            .text_4 {
-              color: #ffffff;
-              font-size: 0.75rem;
-              font-family: PingFangSC;
-              line-height: 0.69rem;
-            }
+            height: 1.50rem;
+            text-align: center;
+          }
+          .text-wrapper2 {
+            border-radius: 6.25rem 0px 0px 6.25rem;
+            width: 2.75rem;
+            height: 1.50rem;
+            text-align: center;
+          }
+          .bg-button {
+            background-image: linear-gradient(90deg, #2c3ff2 0%, #1e09de 100%);
+          }
+          .bg-button2 {
+            background-image: linear-gradient(90deg, #339ce2 0%, #077bba 100%);
           }
         }
         .section_7 {
@@ -418,5 +433,12 @@ import * as API_vitalSignsLook from '@/api/vitalSignsLook'
       }
     }
   }
+  .text_4 {
+    color: #ffffff;
+    font-size: 0.75rem;
+    font-family: PingFangSC;
+    //line-height: 0.69rem;
+    padding-bottom: 0.25rem;
+  }
 }
 </style>

+ 0 - 18
sleep/utils/date.js

@@ -1,18 +0,0 @@
-export const getYear = () => {
-	const date = new Date();
-	return date.getFullYear()
-}
-
-export const getMonth = (key) => {
-	const date = new Date();
-	return addZero(date.getMonth() + 1)
-}
-
-export const getDay = (key) => {
-	const date = new Date();
-	return addZero(date.getDate());
-}
-
-const addZero = function(date) {
-	return date > 9 ? date : `0${date}`;
-};

+ 10 - 8
sleep/utils/request.js

@@ -6,10 +6,10 @@ import store from '@/store'
 import checkToken from '@/utils/checkToken'
 
 export const API = {
-	base: 'https://api.base.wdklian.com',
-	buyer: 'https://api.buyer.wdklian.com',
-	// base: 'http://192.168.1.199:7000',
-	// buyer: 'http://192.168.1.199:7002'
+	// base: 'https://api.base.wdklian.com',
+	// buyer: 'https://api.buyer.wdklian.com',
+	base: 'http://192.168.1.199:7000',
+	buyer: 'http://192.168.1.199:7002'
 }
 
 const config = {
@@ -104,10 +104,12 @@ function service(options) {
 			fail(err){
 				uni.hideLoading()
 				console.log('错误消息====', err)
-				uni.showToast({
-					title: res.data.message,
-					icon: 'none'
-				})
+				if (err.data) {
+					uni.showToast({
+						title: err.data.message,
+						icon: 'none'
+					})
+				}
 				reject(err)
 			}
 		});