| */ | */ | ||||
| methods: { | methods: { | ||||
| tabChange(e) { | tabChange(e) { | ||||
| wx.closeSocket(); | |||||
| if (e.detail.item.text == "发布需求" || e.detail.item.text == "消息") { | if (e.detail.item.text == "发布需求" || e.detail.item.text == "消息") { | ||||
| if (!app.globalData.customerId) { | if (!app.globalData.customerId) { | ||||
| app.goLogin(); | app.goLogin(); |
| * 生命周期函数--监听页面加载 | * 生命周期函数--监听页面加载 | ||||
| */ | */ | ||||
| onLoad() { | onLoad() { | ||||
| }, | |||||
| onShow() { | |||||
| const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null; | const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null; | ||||
| if (businessCommunicationCustomer) { | if (businessCommunicationCustomer) { | ||||
| this.setData({ | this.setData({ | ||||
| } | } | ||||
| // 初始化websocket | // 初始化websocket | ||||
| this.initWebSocket(); | this.initWebSocket(); | ||||
| }, | |||||
| onShow() { | |||||
| // 初始化数据 | // 初始化数据 | ||||
| this.initData(); | this.initData(); | ||||
| }, | }, | ||||
| wx.onSocketMessage(function (res) { | wx.onSocketMessage(function (res) { | ||||
| // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息 | // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息 | ||||
| console.log(res,'消息页----收到新消息') | console.log(res,'消息页----收到新消息') | ||||
| _this.initData(); | |||||
| // _this.initData(); | |||||
| let _data = JSON.parse(res.data); | |||||
| _data.chatRecord = JSON.parse(_data.chatRecord); | |||||
| let msgData = _this.data.msgData; | |||||
| msgData.forEach(el=>{ | |||||
| // 如果当前消息的 customerId = 发送人的ID | |||||
| if(el.businessCommunicationCustomerVO.customerId == _data.chatRecord.sender){ | |||||
| el.lastContent = _data.chatRecord.content; | |||||
| el.lastContentDate = $util.formatTime(new Date(_data.chatRecord.sendTime)); | |||||
| el.lastContentDate = el.lastContentDate.replaceAll('/', '-'); | |||||
| } | |||||
| el.size + 1; | |||||
| }) | |||||
| _this.setData({ | |||||
| msgData | |||||
| }) | |||||
| }) | }) | ||||
| // 监听连接关闭 | // 监听连接关闭 | ||||
| this.getListData(); | this.getListData(); | ||||
| } | } | ||||
| }, | }, | ||||
| onHide(){ | |||||
| wx.closeSocket(); | |||||
| }, | |||||
| }) | }) |
| <view class="msg-li-item" wx:for="{{msgData}}" wx:key="index" bindtap="goToChat" data-item="{{ item }}"> | <view class="msg-li-item" wx:for="{{msgData}}" wx:key="index" bindtap="goToChat" data-item="{{ item }}"> | ||||
| <view class="chatheads-view fl"> | <view class="chatheads-view fl"> | ||||
| <image class='other-head-img' src='{{item.businessCommunicationCustomerVO.chatHeads }}'></image> | <image class='other-head-img' src='{{item.businessCommunicationCustomerVO.chatHeads }}'></image> | ||||
| <view class="view-info" wx:if="{{ item.size > 0 }}">{{ item.size > 99 ? '99+' : item.size }}</view> | |||||
| </view> | </view> | ||||
| <view class="msg-content fl"> | <view class="msg-content fl"> | ||||
| <view class="msg-nickName fl yichu">{{ item.businessCommunicationCustomerVO.nickName }}</view> | <view class="msg-nickName fl yichu">{{ item.businessCommunicationCustomerVO.nickName }}</view> |
| margin: 0 20rpx; | margin: 0 20rpx; | ||||
| border-radius: 50%; | border-radius: 50%; | ||||
| margin-top: 7rpx; | margin-top: 7rpx; | ||||
| position: relative; | |||||
| } | } | ||||
| .msg-li-item .chatheads-view .other-head-img{ | .msg-li-item .chatheads-view .other-head-img{ | ||||
| width: 100%; | width: 100%; | ||||
| } | } | ||||
| .msg-li-item .msg-content .msg-last-content { | .msg-li-item .msg-content .msg-last-content { | ||||
| max-width: calc(100% - 270rpx); | max-width: calc(100% - 270rpx); | ||||
| } | |||||
| .msg-li-item .chatheads-view .view-info { | |||||
| position: absolute; | |||||
| right: -8rpx; | |||||
| top: -8rpx; | |||||
| background: red; | |||||
| color: #fff; | |||||
| font-size: 12px; | |||||
| border-radius: 50%; | |||||
| box-sizing: border-box; | |||||
| min-width: 30rpx; | |||||
| height: 30rpx; | |||||
| line-height: 30rpx; | |||||
| text-align: center; | |||||
| } | } |
| * 生命周期函数--监听页面隐藏 | * 生命周期函数--监听页面隐藏 | ||||
| */ | */ | ||||
| onHide: function () { | onHide: function () { | ||||
| wx.closeSocket(); | |||||
| }, | }, | ||||
| /** | /** |