qingyiay
2023-11-03 8b73cc807383e774b4481f0de35976269ef50b7a
pages/tabbar-page/index-tabbar/index-tabbar.vue
@@ -115,7 +115,24 @@
            });
         }
      },
      onHide() {
         console.log('页面隐藏')
      },
      onUnload() {
         console.log('页面卸载');
         this.$store.dispatch('websocketOnClose')
         clearInterval(this.globalIntervalId);
         this.changeisconnect(false)
      },
      onShow() {
         this.init();
      },
      onLoad() {
         this.$store.dispatch('websocketInit')
         this.$store.commit('changeisLogin', true)
         console.log(this.globalisconnect, 'index的ws数据');
         this.userAuthorization();
         this.messageReq();
         // 开启双人通话
         wx.setEnable1v1Chat({
            enable: true,
@@ -155,13 +172,10 @@
            if (v.startsWith('weigh')) {
               let nowWeighObj = JSON.parse(v.slice(7));
               if (this.globalweighHouseCode && nowWeighObj.eqCode == this.globalweighHouseCode) {
                  if (nowWeighObj.eqInfraredStatus) {
                     this.changeinfraredStatus(true);
                     this.changeWeigh(nowWeighObj.weigh);
                  } else {
                     this.changeinfraredStatus(false);
                     this.changeWeigh(nowWeighObj.weigh);
                  }
                  this.changeWeigh(nowWeighObj.weigh);
                  this.changeinfraredStatus(!!nowWeighObj.eqInfraredStatus);
                  let warningState = nowWeighObj.warning == 1
                  this.changeWarning(warningState)
               }
            } else if (v.startsWith('msg')) {
               function removeTags(str) {
@@ -178,36 +192,21 @@
               }
            } else if (v.startsWith('wsg')) {
               let wsgObj = JSON.parse(v.slice(5));
               if (uni.getStorageInfoSync('carNo') === wsgObj.carNo) {
                  changewsgVisiable(true)
                  changewsgContent(wsgObj.content)
               if (uni.getStorageSync('carNo') === wsgObj.carNo && !wsgObj.peopleWeigh) {
                  this.changewsgVisiable(true)
                  this.changewsgContent(wsgObj.content)
               }
               if (uni.getStorageSync('carNo') === wsgObj.carNo && wsgObj.peopleWeigh === 2) {
                  this.changeconfirmWeighVisiable(true)
                  this.changeconfirmWeighContent(wsgObj.content)
               }
            }
         }
      },
      onShow() {
         this.init();
         if (this.globalisconnect) {
            this.$store.dispatch('websocketInit')
            this.changeisconnect(false)
            this.$store.commit('changeisLogin', true)
            console.log(this.globalisconnect, 'index的ws数据');
         }
         this.userAuthorization();
         this.messageReq();
      },
      onHide() {
         console.log('页面隐藏')
      },
      onUnload() {
         console.log('页面卸载');
         this.$store.dispatch('websocketOnClose')
         clearInterval(this.globalIntervalId);
         this.changeisconnect(true)
      },
      methods: {
         ...mapMutations(['pushMessage', 'changeWeigh', 'changeinfraredStatus', 'changeisconnect', 'websocketInit',
            'changewsgVisiable', 'changewsgContent'
            'changewsgVisiable', 'changewsgContent', 'changeconfirmWeighVisiable', 'changeconfirmWeighContent',
            'changeWarning'
         ]),
         messagePage() {
            uni.navigateTo({
@@ -277,63 +276,6 @@
               fail() {
                  console.log('获取失败');
               },
            });
         },
         // 初始化websocket
         initWebsocket() {
            let wsUrl = `${webSocketUrl}?access_token=${uni.getStorageSync('token')}`;
            socket = uni.connectSocket({
               url: wsUrl,
               header: {
                  CLIENT_TOC: 'Y'
               },
               complete: res => {
                  console.log(res, 'socket结果');
                  if (res.errMsg == 'connectSocket:ok') {
                     this.changeisconnect(true);
                     this.isconnect = true;
                  }
               }
            });
            socket.onOpen(() => {
               console.log('onOpen');
               this.intervalId = setInterval(() => {
                  socket.send({
                     data: JSON.stringify({ type: 'ping' }),
                     success(e) {
                        console.log(e, '发送心跳成功');
                     }
                  });
               }, 30000);
            });
            // 获取服务器传来的数据,做相应处理
            socket.onMessage(res => {
               console.log('message', res);
               if (res.data.startsWith('weigh')) {
                  let nowWeighObj = JSON.parse(res.data.slice(7));
                  if (this.globalweighHouseCode && nowWeighObj.eqCode == this.globalweighHouseCode) {
                     if (nowWeighObj.eqInfraredStatus) {
                        this.changeinfraredStatus(true);
                        this.changeWeigh(nowWeighObj.weigh);
                     } else {
                        this.changeinfraredStatus(false);
                        this.changeWeigh(nowWeighObj.weigh);
                     }
                  }
               } else if (res.data.startsWith('msg')) {
                  this.messageList = JSON.parse(res.data.slice(5));
                  this.messagePushShow = true;
               } else {}
            });
            socket.onClose(e => {
               console.log('webSocketClose', e);
               this.isconnect = false;
               this.changeisconnect(false);
            });
            socket.onError(err => {
               console.log('socket报错', err);
               this.$u.toast('出现错误,请重新进入该页面,重试!!');
            });
         },
         messageconfirm() {