zhaoxiaohai 2 years ago
parent
commit
d85f3df501
2 changed files with 18 additions and 26 deletions
  1. 8 17
      app.js
  2. 10 9
      custom-tab-bar/index.js

+ 8 - 17
app.js

@@ -5,7 +5,6 @@ import {
 App({
     onLaunch() {},
     initSocket() {
-        return;
         let that = this;
         this.globalData.SocketTask = wx.connectSocket({
             url: 'ws://121.37.152.76:8080/yos/webSocket/' + wx.getStorageSync('userMsg').token,
@@ -14,34 +13,26 @@ App({
             }
         })
         this.globalData.SocketTask.onOpen(function (res) {
-            console.log('WebSocket连接已打开!readyState=' + that.globalData.SocketTask.readyState);
             that.globalData.socketEstablish = true;
         })
         this.globalData.SocketTask.onMessage(function (res) {
-            that.globalData.callback(res)
+            that.globalData.socket.callback(res)
         })
         this.globalData.SocketTask.onError(function (res) {
-            console.log('readyState=' + that.globalData.SocketTask.readyState)
-            setTimeout(() => {
-                that.initSocket()
-            }, 1000)
+            that.globalData.socketEstablish = false;
         })
         this.globalData.SocketTask.onClose(function (res) {
-            console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState)
-            if (that.globalData.socketEstablish == false) {
-                setTimeout(() => {
-                    that.initSocket()
-                }, 1000)
-            } else {
-                that.globalData.socketEstablish = false;
-            }
+            that.globalData.socketEstablish = false;
         })
     },
     globalData: {
         http: new ApiModel(), //接口文件
         queryPer: require("./utils/queryPermissions"), //权限查询
         socketEstablish: false, //是否已经建立socket
-        SocketTask: '',
-        callback: function () {},
+        SocketTask: '', // Socket方法
+        socket: { // Socket回调
+            that: null, //保存this
+            callback: function () {}
+        },
     },
 })

+ 10 - 9
custom-tab-bar/index.js

@@ -83,22 +83,23 @@ Component({
             this.setData({
                 active: obj.name
             });
-            this.unReadMessageCount();
+            //socket
+            if (!getApp().globalData.socketEstablish) getApp().initSocket();
+            getApp().globalData.socket = {
+                that: this,
+                callback: this.unReadMessageCount
+            }
         },
         /* 更新信息数量 */
         unReadMessageCount() {
             _Http.basic({
                 "classname": "system.message.Message",
                 "method": "unReadMessageCount",
-                "content": {
-                    nochace: true
-                }
+                "content": {nochace: true}
             }, false).then(res => {
-                if (res.msg != '成功') return;
-                if (res.data.fcount == 0) return;
-                if (res.data.fcount > 99) res.data.fcount = '99+';
-                this.setData({
-                    'tabbarList[1].fcount': res.data.fcount
+                if (res.msg != '成功' || res.data.fcount == 0) return;
+                getApp().globalData.socket.that.setData({
+                    'tabbarList[1].fcount': res.data.fcount > 99 ? '99+' : res.data.fcount
                 })
             })
         }