Browse Source

修改全局socket

xiasongtao 2 years ago
parent
commit
ac0419ad68
6 changed files with 218 additions and 251 deletions
  1. 30 12
      app.js
  2. 1 0
      pages/auth/index.js
  3. 14 49
      pages/mapIndex/mapIndex.js
  4. 100 112
      pages/sessionList/sessionList.js
  5. 68 78
      pages/userChat/userChat.js
  6. 5 0
      utils/socket.js

+ 30 - 12
app.js

@@ -85,8 +85,8 @@ App({
 
   globalData: {
 
-    // localHref:"https://https.ttq.so",           //新版糖化漂流接口测试
-    localHref: "https://api.ttq.so",           //新版糖化漂流接口正式
+    localHref:"https://https.ttq.so",           //新版糖化漂流接口测试
+    // localHref: "https://api.ttq.so",           //新版糖化漂流接口正式
     localDomin:"https://test.ttq.so",   //图片地址
     userInfo: null,                     //用户信息
     ttqBind: "",                        //判断用户绑定
@@ -104,21 +104,43 @@ App({
     advertisShow:true,
     couponShowTime:0,
     advertisShowTime:0,
-    socket9601:'wss://dm.ttq.so:9601',//正式
-    // socket9601: 'wss://httpss.ttq.so/wss',//测试
+    // socket9601:'wss://dm.ttq.so:9601',//正式
+    socket9601: 'wss://httpss.ttq.so/wss',//测试
     localSocket: {},
     callback: function () { }
   },
   initSocket() {
     let that = this
     that.globalData.localSocket = wx.connectSocket({
-      //此处 url 可以用来测试
-      url: `wss://httpss.ttq.so/wss`,
+      url: 'wss://httpss.ttq.so/wss'
     })
-    //版本库需要在 1.7.0 以上
     that.globalData.localSocket.onOpen(function (res) {
       console.log('WebSocket连接已打开!readyState=' + that.globalData.localSocket.readyState)
-      
+    })
+    that.globalData.localSocket.onMessage(function (res) {
+
+      console.log('onMessage')
+
+      var msg = JSON.parse(res.data);
+      if (msg.type == 'connect_success') {
+        wx.request({
+          method: "POST",
+          url: that.globalData.localHref + '/ThApi/Chat/bindClient.html',
+          data: {
+            token: wx.getStorageSync('userToken'),
+            client_id: msg.data.client_id
+          },
+          header: {
+            "Content-Type": "application/x-www-form-urlencoded"
+          },
+          success: function (res) {
+            console.log(res, 'resresres')
+          }
+        })
+      }else{
+        that.globalData.callback(res)
+        
+      }
     })
     that.globalData.localSocket.onError(function (res) {
       console.log('readyState=' + that.globalData.localSocket.readyState)
@@ -127,10 +149,6 @@ App({
       console.log('WebSocket连接已关闭!readyState=' + that.globalData.localSocket.readyState)
       that.initSocket()
     })
-    that.globalData.localSocket.onMessage(function (res) {
-      // 用于在其他页面监听 websocket 返回的消息
-      that.globalData.callback(res)
-    })
   },
 
 })

+ 1 - 0
pages/auth/index.js

@@ -40,6 +40,7 @@ Page({
                 console.log(res)
                 // 展示本地存储能力
                 wx.setStorageSync('userToken', res.data.data.token)
+                app.initSocket()
                 wx.reLaunch({
                   url: '../mapIndex/mapIndex'
                 })

+ 14 - 49
pages/mapIndex/mapIndex.js

@@ -675,10 +675,15 @@ Page({
     _this.setData({
       bindTtqShow: app.globalData.ttqBind,    //全局绑定赋值
 
-    });   
+    }); 
+    var that = this  
     this.getUserStart();                             //初始化个人信息
-    wx.closeSocket({})
-    this.conSocket()
+    // this.conSocket()
+    app.globalData.callback = function (res) {
+      that.setData({
+        messageNum: that.data.messageNum+1
+      })
+    }
   },
 
   onHide: function () {
@@ -689,52 +694,12 @@ Page({
 
   conSocket() {
     var that = this
-    wx.onSocketMessage(function (res) {
-      var msg = JSON.parse(res.data);
-      if (msg.type == 'connect_success') {
-        console.log('链接成功mapIndex')    //页面id
-        that.setData({
-          client_id: msg.data.client_id
-        })
-        wx.request({
-          method: "POST",
-          url: app.globalData.localHref + '/ThApi/Chat/getTotalUnread.html',
-          data: {
-            token: that.data.userToken,
-            client_id: that.data.client_id
-          },
-          header: {
-            "Content-Type": "application/x-www-form-urlencoded"
-          },
-          success: function (res) {
-            if (res.data.retCode == 1) {
-              that.setData({
-                messageNum: res.data.unread_total
-              })
-            } else {
-              // wx.showToast({
-              //   title: res.data.msg,
-              //   icon: 'none',
-              //   duration: 2000
-              // })
-            }
-          },
-          fail: function () {
-            alert(res.msg)
-          }
-        })
-        //获取聊天接口
-
-      } else {
-        var res = JSON.parse(res.data)
-        console.log(res, "新消息走这里新消息页面")
-        let num = that.data.unread_msg_count + 1
-        that.setData({
-          unread_msg_count : num
-        })
-        
-      }
-    });
+    console.log('123321')
+    app.globalData.localSocket.onMessage(function (res) {
+      // 用于在其他页面监听 websocket 返回的消息
+      console.log(res, '123321')
+      
+    })
   },
 
 

+ 100 - 112
pages/sessionList/sessionList.js

@@ -38,7 +38,6 @@ Page({
   onShow(){
     var that = this
     console.log('showle')
-    wx.closeSocket({})
     this.connectSocket()
   },
   all(e) {
@@ -67,130 +66,119 @@ Page({
   //socket链接
   connectSocket() {
     var that = this;
-    var nowurl = this.route 
-    // wx.showLoading({
-    //   title: '加载中',
-    // })
-    console.log('789789789')
-    wx.onSocketMessage(function (res) {
-      var msg = JSON.parse(res.data);
-      if (msg.type == 'connect_success') {
-        console.log("Socket成功session")
-        var clientID = msg.data.client_id
-        wx.request({
-          method: "POST",
-          url: app.globalData.localHref + '/ThApi/Chat/chatList.html',
-          data: {
-            token: that.data.userToken,
-            client_id: clientID,
-          },
-          header: {
-            "Content-Type": "application/x-www-form-urlencoded"
-          },
-          success: function (res) {
-            console.log(res,'res')
-            if (res.data.retCode == 1) {
-              // setTimeout(function () {
-              //   wx.hideLoading()
-              // }, 1000)
-              let nowyear = new Date().getFullYear() //获得现在的年
-              let nowMonth = new Date().getMonth() + 1 //获得现在的月
-              let nowDay = new Date().getDate() //获得现在的天
-              //补0
-              if (nowMonth < 10) {
-                nowMonth = "0" + nowMonth
-              }
-              if (nowDay < 10) {
-                nowDay = "0" + nowDay
-              }
-              let nowyearTime = new Date(nowyear + "/01/01 00:00").getTime() //获得当前年第一天的毫秒数
-              let nowDayTime = new Date(nowyear + "/" + nowMonth + "/" + nowDay + " 00:00").getTime() //获得当前月第一天的毫秒数
-              for (let i = 0; i < res.data.data.length; i++) {
-                let postCreTime = res.data.data[i].createtime * 1000//创建的毫秒数
-                let last_msg_time = res.data.data[i].last_msg_time * 1000//发帖的毫秒数
-
-                if (postCreTime <= nowyearTime) {
-                  res.data.data[i].createtime = time.formatTimeTwo(res.data.data[i].createtime, 'Y/M/D')
-                } else if (postCreTime <= nowDayTime) {
-                  res.data.data[i].createtime = time.formatTimeTwo(res.data.data[i].createtime, 'M/D')
-                } else {
-                  res.data.data[i].createtime = time.formatTimeTwo(res.data.data[i].createtime, 'h:m')
-                }
-
-                if (last_msg_time <= nowyearTime) {
-                  res.data.data[i].last_msg_time = time.formatTimeTwo(res.data.data[i].last_msg_time, 'Y/M/D').substr(2)
-                } else if (last_msg_time <= nowDayTime) {
-                  res.data.data[i].last_msg_time = time.formatTimeTwo(res.data.data[i].last_msg_time, 'M/D')
-                } else {
-                  res.data.data[i].last_msg_time = time.formatTimeTwo(res.data.data[i].last_msg_time, 'h:m')
-                }
-
-              }
-
-              that.setData({
-                info: res.data.data
-              })
-              console.log(res.data.data,'tata')
-            }else{
-              wx.showToast({
-                title: res.data.msg,
-                icon: 'none',
-                duration: 2000
-              })
-            }
-            
 
-          },
-          fail: function () {
-            console.log(res.msg)
+    wx.request({
+      method: "POST",
+      url: app.globalData.localHref + '/ThApi/Chat/chatList.html',
+      data: {
+        token: that.data.userToken,
+      },
+      header: {
+        "Content-Type": "application/x-www-form-urlencoded"
+      },
+      success: function (res) {
+        console.log(res, 'res')
+        if (res.data.retCode == 1) {
+          // setTimeout(function () {
+          //   wx.hideLoading()
+          // }, 1000)
+          let nowyear = new Date().getFullYear() //获得现在的年
+          let nowMonth = new Date().getMonth() + 1 //获得现在的月
+          let nowDay = new Date().getDate() //获得现在的天
+          //补0
+          if (nowMonth < 10) {
+            nowMonth = "0" + nowMonth
           }
-        })
-
-      } else {
-        var res = JSON.parse(res.data)
-        console.log(res.data, "新消息走这里新消息页面session")
-        console.log(that.data.info,'that.data.info')
-        for(let i=0;i<that.data.info.length;i++){
-          console.log(res.data.timestamp, '121312')
-          if(that.data.info[i].id == res.data.chat_id){
-            let nowyear = new Date().getFullYear() //获得现在的年
-            let nowMonth = new Date().getMonth() + 1 //获得现在的月
-            let nowDay = new Date().getDate() //获得现在的天
-            //补0
-            if (nowMonth < 10) {
-              nowMonth = "0" + nowMonth
-            }
-            if (nowDay < 10) {
-              nowDay = "0" + nowDay
-            }
-            let nowyearTime = new Date(nowyear + "/01/01 00:00").getTime() //获得当前年第一天的毫秒数
-            let nowDayTime = new Date(nowyear + "/" + nowMonth + "/" + nowDay + " 00:00").getTime() //获得当前月第一天的毫秒数
-            
-            let postCreTime = res.data.timestamp //判断时间距离
-            let postCreTime1 = res.data.timestamp / 1000
-            console.log(time.formatTimeTwo(postCreTime, 'Y/M/D h:m'),'770')
+          if (nowDay < 10) {
+            nowDay = "0" + nowDay
+          }
+          let nowyearTime = new Date(nowyear + "/01/01 00:00").getTime() //获得当前年第一天的毫秒数
+          let nowDayTime = new Date(nowyear + "/" + nowMonth + "/" + nowDay + " 00:00").getTime() //获得当前月第一天的毫秒数
+          for (let i = 0; i < res.data.data.length; i++) {
+            let postCreTime = res.data.data[i].createtime * 1000//创建的毫秒数
+            let last_msg_time = res.data.data[i].last_msg_time * 1000//发帖的毫秒数
+
             if (postCreTime <= nowyearTime) {
-              res.data.timestamp = time.formatTimeTwo(postCreTime1, 'Y/M/D').substr(2)
+              res.data.data[i].createtime = time.formatTimeTwo(res.data.data[i].createtime, 'Y/M/D')
             } else if (postCreTime <= nowDayTime) {
-              res.data.timestamp = time.formatTimeTwo(postCreTime1, 'M/D')
+              res.data.data[i].createtime = time.formatTimeTwo(res.data.data[i].createtime, 'M/D')
             } else {
-              res.data.timestamp = time.formatTimeTwo(postCreTime1, 'h:m')
+              res.data.data[i].createtime = time.formatTimeTwo(res.data.data[i].createtime, 'h:m')
             }
 
+            if (last_msg_time <= nowyearTime) {
+              res.data.data[i].last_msg_time = time.formatTimeTwo(res.data.data[i].last_msg_time, 'Y/M/D').substr(2)
+            } else if (last_msg_time <= nowDayTime) {
+              res.data.data[i].last_msg_time = time.formatTimeTwo(res.data.data[i].last_msg_time, 'M/D')
+            } else {
+              res.data.data[i].last_msg_time = time.formatTimeTwo(res.data.data[i].last_msg_time, 'h:m')
+            }
 
-            let count = 'info[' + i + '].unread_count'
-            let content = 'info[' + i + '].content'
-            let timestamp = 'info[' + i + '].last_msg_time'
-            that.setData({
-              [count]: parseInt(that.data.info[i].unread_count)  + res.data.unread_count,
-              [timestamp]: res.data.timestamp,
-              [content]:res.data.content
-            })
           }
+
+          that.setData({
+            info: res.data.data
+          })
+          console.log(res.data.data, 'tata')
+        } else {
+          wx.showToast({
+            title: res.data.msg,
+            icon: 'none',
+            duration: 2000
+          })
         }
 
+
+      },
+      fail: function () {
+        console.log(res.msg)
       }
     })
+
+    app.globalData.callback = function (res) {
+      var res = JSON.parse(res.data)
+      console.log(res.data, "新消息走这里新消息页面sessionaaa")
+      for (let i = 0; i < that.data.info.length; i++) {
+        console.log(res.data.timestamp, '121312')
+        if (that.data.info[i].id == res.data.th_chat_id) {
+          let nowyear = new Date().getFullYear() //获得现在的年
+          let nowMonth = new Date().getMonth() + 1 //获得现在的月
+          let nowDay = new Date().getDate() //获得现在的天
+          //补0
+          if (nowMonth < 10) {
+            nowMonth = "0" + nowMonth
+          }
+          if (nowDay < 10) {
+            nowDay = "0" + nowDay
+          }
+          let nowyearTime = new Date(nowyear + "/01/01 00:00").getTime() //获得当前年第一天的毫秒数
+          let nowDayTime = new Date(nowyear + "/" + nowMonth + "/" + nowDay + " 00:00").getTime() //获得当前月第一天的毫秒数
+
+          let postCreTime = res.data.timestamp //判断时间距离
+          let postCreTime1 = res.data.timestamp / 1000
+          console.log(time.formatTimeTwo(postCreTime, 'Y/M/D h:m'), '770')
+          if (postCreTime <= nowyearTime) {
+            res.data.timestamp = time.formatTimeTwo(postCreTime1, 'Y/M/D').substr(2)
+          } else if (postCreTime <= nowDayTime) {
+            res.data.timestamp = time.formatTimeTwo(postCreTime1, 'M/D')
+          } else {
+            res.data.timestamp = time.formatTimeTwo(postCreTime1, 'h:m')
+          }
+
+
+          let count = 'info[' + i + '].unread_count'
+          let content = 'info[' + i + '].content'
+          let timestamp = 'info[' + i + '].last_msg_time'
+          that.setData({
+            [count]: parseInt(that.data.info[i].unread_count) + 1,
+            [timestamp]: res.data.timestamp,
+            [content]: res.data.content
+          })
+          
+          console.log(that.data.info,'info')
+        }
+      }
+    }
   },
   //查看新消息
   lookInfo(e){

+ 68 - 78
pages/userChat/userChat.js

@@ -190,90 +190,80 @@ Page({
   //链接socket
   conSocket() {
     var that = this
-    wx.onSocketMessage(function (res) {
-      var msg = JSON.parse(res.data);
-      if (msg.type == 'connect_success') {
-        console.log('链接成功userchat')    //页面id
-        that.setData({
-          client_id: msg.data.client_id
-        })
-        wx.request({
-          method: "POST",
-          url: app.globalData.localHref + '/ThApi/Chat/msgList.html',
-          data: {
-            token: that.data.userToken,
-            client_id: that.data.client_id,
-            chat_id: that.data.chat_id
-          },
-          header: {
-            "Content-Type": "application/x-www-form-urlencoded"
-          },
-          success: function (res) {
-            if(res.data.retCode == 1){
-              let daoxu = res.data.list.reverse()
-              if (that.data.chat_type == 0 && daoxu){
-                
-                for(let i=0;i<daoxu.length;i++){
-                  let a = daoxu[i].content
-                  console.log(a, 'daoxu456')
-                  let b = that.getHttpUrlArray(a)
-                  console.log(b, 'bb456')
-                  console.log(that.getHttpUrlArray(daoxu[i].content), 'daoxu1')
-                  if (that.getHttpUrlArray(daoxu[i].content) != null){
-                    console.log('778899')
-                    let url = that.getHttpUrlArray(daoxu[i].content).url
-                    let first = that.getHttpUrlArray(daoxu[i].content).first
-                    let last = that.getHttpUrlArray(daoxu[i].content).last
-                    daoxu[i].content = daoxu[i].content.slice(0, first) + daoxu[i].content.slice(last)
-                    daoxu[i].url = url
-                  }
-                }
+
+    wx.request({
+      method: "POST",
+      url: app.globalData.localHref + '/ThApi/Chat/msgList.html',
+      data: {
+        token: that.data.userToken,
+        chat_id: that.data.chat_id
+      },
+      header: {
+        "Content-Type": "application/x-www-form-urlencoded"
+      },
+      success: function (res) {
+        if (res.data.retCode == 1) {
+          let daoxu = res.data.list.reverse()
+          if (that.data.chat_type == 0 && daoxu) {
+
+            for (let i = 0; i < daoxu.length; i++) {
+              let a = daoxu[i].content
+              console.log(a, 'daoxu456')
+              let b = that.getHttpUrlArray(a)
+              console.log(b, 'bb456')
+              console.log(that.getHttpUrlArray(daoxu[i].content), 'daoxu1')
+              if (that.getHttpUrlArray(daoxu[i].content) != null) {
+                console.log('778899')
+                let url = that.getHttpUrlArray(daoxu[i].content).url
+                let first = that.getHttpUrlArray(daoxu[i].content).first
+                let last = that.getHttpUrlArray(daoxu[i].content).last
+                daoxu[i].content = daoxu[i].content.slice(0, first) + daoxu[i].content.slice(last)
+                daoxu[i].url = url
               }
-              console.log(daoxu,'daoxu')
-              that.setData({
-                messageArray: daoxu,
-                login_user_id: res.data.login_user_id,
-                page_count: res.data.page_info.page_count,
-              })
-              
-              wx.createSelectorQuery().select('#contant').boundingClientRect(function (rect) {
-                // 使页面滚动到底部
-                wx.pageScrollTo({
-                  scrollTop: rect.bottom,
-                  duration:0
-                })
-              }).exec()
-            }else{
-              wx.showToast({
-                title: res.data.msg,
-                icon: 'none',
-                duration: 2000
-              })
             }
-          },
-          fail: function () {
-            alert(res.msg)
           }
-        })
-        //获取聊天接口
+          console.log(daoxu, 'daoxu')
+          that.setData({
+            messageArray: daoxu,
+            login_user_id: res.data.login_user_id,
+            page_count: res.data.page_info.page_count,
+          })
 
-      } else {
-        var res = JSON.parse(res.data)
-        console.log(res, "新消息走这里新消息页面")
-        
-        let list = that.data.messageArray.concat([res.data])
-        that.setData({
-          messageArray: list
-        })
-        wx.createSelectorQuery().select('#contant').boundingClientRect(function (rect) {
-          // 使页面滚动到底部
-          wx.pageScrollTo({
-            scrollTop: rect.bottom,
-            duration: 0
+          wx.createSelectorQuery().select('#contant').boundingClientRect(function (rect) {
+            // 使页面滚动到底部
+            wx.pageScrollTo({
+              scrollTop: rect.bottom,
+              duration: 0
+            })
+          }).exec()
+        } else {
+          wx.showToast({
+            title: res.data.msg,
+            icon: 'none',
+            duration: 2000
           })
-        }).exec()
+        }
+      },
+      fail: function () {
+        alert(res.msg)
       }
-    });
+    })
+    app.globalData.callback = function (res) {
+      var res = JSON.parse(res.data)
+      console.log(res, "新消息走这里新消息页面")
+
+      let list = that.data.messageArray.concat([res.data])
+      that.setData({
+        messageArray: list
+      })
+      wx.createSelectorQuery().select('#contant').boundingClientRect(function (rect) {
+        // 使页面滚动到底部
+        wx.pageScrollTo({
+          scrollTop: rect.bottom,
+          duration: 0
+        })
+      }).exec()
+    }
   },
 
   //跳转活动页

+ 5 - 0
utils/socket.js

@@ -0,0 +1,5 @@
+
+
+module.exports = {
+  concatSocket: concatSocket
+}