| @@ -58,7 +58,7 @@ Component({ | |||
| }) | |||
| } | |||
| // 如果 websocket 没有连接,连接 | |||
| if (!app.globalData.isOnSocketOpen) { | |||
| if (!app.globalData.isOnSocketOpen && this.data.customerId) { | |||
| this.initWebSocket(); | |||
| } | |||
| // let _this = this; | |||
| @@ -97,17 +97,23 @@ Component({ | |||
| initWebSocket() { | |||
| let _this = this; | |||
| let customerId = this.data.customerId; | |||
| let url = encodeURI(`ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`); | |||
| console.log(url); | |||
| // let url = `ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`; | |||
| //建立连接 | |||
| wx.connectSocket({ | |||
| url: `ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`,//本地 | |||
| url,//本地 | |||
| success: function () { | |||
| console.log('消息页-websocket接口调通~还没真正的成功!') | |||
| console.log('tabbar-websocket接口调通~还没真正的成功!') | |||
| }, | |||
| fail: function () { | |||
| console.log('消息页-websocket连接失败~') | |||
| console.log('tabbar-websocket连接失败~') | |||
| }, | |||
| }) | |||
| wx.onSocketError(res=>{ | |||
| console.log(`打印异常`) | |||
| console.log(res); | |||
| }) | |||
| //连接成功 | |||
| wx.onSocketOpen(function () { | |||
| console.log('tabbar-连接成功,真正的成功', 'onSocketOpen'); | |||
| @@ -50,10 +50,10 @@ Page({ | |||
| initWebSocket: function () { | |||
| var _this = this; | |||
| let { customerId } = _this.data; | |||
| // console.log() | |||
| let url = encodeURI(`ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`); | |||
| //建立连接 | |||
| wx.connectSocket({ | |||
| url: `ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`,//本地 | |||
| url,//本地 | |||
| success: function () { | |||
| console.log('消息页-接口调通~还没真正的成功!') | |||
| }, | |||
| @@ -94,7 +94,7 @@ Page({ | |||
| obj.size = 1; | |||
| obj.customer = _data.chatRecord.senderCustomer; | |||
| obj.businessCommunicationCustomerVO = _data.chatRecord.senderBusinessCommunicationCustomer; | |||
| obj.messageType = _data.chatRecord.messageType; | |||
| obj.messageType = _data.chatRecord.chatRecord.messageType; | |||
| if (obj.customer) { | |||
| if (obj.customer.paidByMonth) { | |||
| obj.businessCommunicationCustomerVO.nickName = obj.customer.nickName; | |||
| @@ -116,9 +116,10 @@ Page({ | |||
| initWebSocket: function () { | |||
| var _this = this; | |||
| let { receiveMemberId, sendMemberId } = _this.data; | |||
| let url = encodeURI(`ws://` + app.webSocketUrl + `/webSocket/{"userno":${receiveMemberId},"messageModule":"010"}`); | |||
| //建立连接 | |||
| wx.connectSocket({ | |||
| url: `ws://` + app.webSocketUrl + `/webSocket/{"userno":${receiveMemberId},"messageModule":"010"}`,//本地 | |||
| url,//本地 | |||
| success: function () { | |||
| console.log('websocket接口调通~还没真正的成功!') | |||
| }, | |||
| @@ -136,7 +137,11 @@ Page({ | |||
| // duration: 2000 | |||
| // }) | |||
| }) | |||
| wx.onSocketError(res=>{ | |||
| console.log(`打印异常`) | |||
| console.log(res); | |||
| }) | |||
| // 接收服务器的消息事件 | |||
| wx.onSocketMessage(function (res) { | |||
| // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息 | |||
| @@ -144,21 +149,20 @@ Page({ | |||
| list = _this.data.newsList; | |||
| let _data = JSON.parse(res.data); | |||
| _data.chatRecord = JSON.parse(_data.chatRecord); | |||
| let msgData = {}; | |||
| // type 0 自己 1 对方 | |||
| if (_data.chatRecord.sender != _this.data.receiveMemberId) { | |||
| if (_data.chatRecord.chatRecord.sender != _this.data.receiveMemberId) { | |||
| msgData.type = 1; | |||
| } else { | |||
| msgData.type = 0; | |||
| } | |||
| msgData.isRead = true; | |||
| msgData.message = _data.chatRecord.content; | |||
| msgData.messageType = _data.chatRecord.messageType; | |||
| msgData.message = _data.chatRecord.chatRecord.content; | |||
| msgData.messageType = _data.chatRecord.chatRecord.messageType; | |||
| if (msgData.messageType == 1) { | |||
| msgData.object = _data.chatRecord.object; | |||
| msgData.object = _data.chatRecord.chatRecord.object; | |||
| } | |||
| msgData.date = utils.formatTime(new Date(_data.chatRecord.sendTime)); | |||
| msgData.date = utils.formatTime(new Date(_data.chatRecord.chatRecord.sendTime)); | |||
| msgData.date = msgData.date.replaceAll('/', '-'); | |||
| list.push(msgData); | |||
| _this.setData({ | |||
| @@ -168,7 +172,6 @@ Page({ | |||
| _this.scrollBottom() | |||
| }, | |||
| ) | |||
| // 监听连接关闭 | |||
| wx.onSocketClose(function () { | |||
| console.log('监听 WebSocket 连接关闭事件') | |||