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