// pages/release/release.js // 获取应用实例 const app = getApp() const $request = require('../../utils/request.js'); const $util = require('../../utils/util.js'); Page({ data: { currentIndex: 2, pageLoading: true, customerId: "", // 前往关注公众号 topShow: false, showAccount:true, msgData: [], page: 1, size: 10, total: 0, // 是否加载数据,true加载,false不加载 onRefresh: true, slideButtons: [{ type: 'warn', text: '删除', extClass: 'delete', }], }, /** * 生命周期函数--监听页面加载 */ onLoad() { }, onShow() { this.setData({ currentIndex: 2 }) const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null; if (businessCommunicationCustomer) { this.setData({ customerId: businessCommunicationCustomer.customerId }) } // 如果全局的 websocket 是连接的,需要关闭连接 调用自身的 if (app.globalData.isOnSocketOpen) { wx.closeSocket(); } // 初始化websocket this.initWebSocket(); // 初始化数据 this.initData(); }, // websocket初始化 initWebSocket: function () { var _this = this; let { customerId } = _this.data; let url = encodeURI(app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`); //建立连接 wx.connectSocket({ url,//本地 success: function () { console.log('消息页-接口调通~还没真正的成功!') }, fail: function () { console.log('消息页-websocket连接失败~') }, }) //连接成功 wx.onSocketOpen(function () { console.log('消息页-连接成功,真正的成功', 'onSocketOpen'); }) // 接收服务器的消息事件 wx.onSocketMessage(function (res) { // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息 // _this.initData(); let _data = JSON.parse(res.data); _data.chatRecord = JSON.parse(_data.chatRecord); console.log(_data, '消息页----收到新消息') let msgData = _this.data.msgData; // 初始化新消息 let isNewMsg = true; // 循环数据,检测发送人ID是否存在,如果不存在就是新消息。 msgData.forEach(el => { if (el.businessCommunicationCustomerVO.customerId == _data.sendno) { isNewMsg = false; } }) console.log(`是已存在列表框的还是不存在列表框`, isNewMsg) // 如果是新消息 if (isNewMsg) { let obj = {}; obj.lastContent = _data.chatRecord.chatRecord.content; obj.lastContentDate = $util.formatTime(new Date(_data.chatRecord.chatRecord.sendTime)); obj.lastContentDate = obj.lastContentDate.replaceAll('/', '-'); obj.size = 1; obj.customer = _data.chatRecord.senderCustomer; obj.businessCommunicationCustomerVO = _data.chatRecord.senderBusinessCommunicationCustomer; obj.messageType = _data.chatRecord.chatRecord.messageType; if (obj.customer) { if (obj.customer.paidByMonth) { obj.businessCommunicationCustomerVO.nickName = obj.customer.nickName; } } msgData.unshift(obj); } else { msgData.forEach(el => { // 如果当前消息的 customerId = 发送人的ID if (el.businessCommunicationCustomerVO.customerId == _data.chatRecord.chatRecord.sender) { el.lastContent = _data.chatRecord.chatRecord.content; el.lastContentDate = $util.formatTime(new Date(_data.chatRecord.chatRecord.sendTime)); el.lastContentDate = el.lastContentDate.replaceAll('/', '-'); el.size += 1; } if (el.customer) { if (el.customer.paidByMonth) { el.businessCommunicationCustomerVO.nickName = el.customer.nickName } } }) } _this.setData({ msgData }) }) // 监听连接关闭 wx.onSocketClose(function () { console.log('监听 WebSocket 连接关闭事件') }) }, // 关闭顶部关注 closeTopShow() { this.setData({ topShow: false, showAccount:false, }) }, // 聊一聊 goToChat(e) { let item = e.currentTarget.dataset.item; wx.navigateTo({ url: "/pages/msgModule/wechat2/wechat2", success: function (res) { // 通过eventChannel向被打开页面传送数据 res.eventChannel.emit('customerid', { customerid: item.businessCommunicationCustomerVO.customerId, chatHeads: item.businessCommunicationCustomerVO.chatHeads }) } }) }, initData() { this.setData({ page: 1, size: 10, msgData: [], pageLoading: true, }) this.getMsgData(); }, getMsgData() { // let { page, size } = this.data; $request.get('/businessCommunicationDemand/getChatRecordTable.action').then(res => { let tempListData = this.data.msgData; if (res.status == 0) { let datas = res.data; // 先push数据 tempListData.push(...datas.chatRecordTable); tempListData.forEach(el => { el.lastContentDate = $util.formatTime(new Date(el.lastContentDate)); el.lastContentDate = el.lastContentDate.replaceAll('/', '-'); if (typeof el.type == 'string') { el.type = el.type.split(','); } if (el.customer) { if (el.customer.paidByMonth) { el.businessCommunicationCustomerVO.nickName = el.customer.nickName } } }) // 设置总数 this.setData({ msgData: tempListData, total: datas.total, }) } this.setData({ pageLoading: false, }) }).catch(error => { console.log(error, 'error appletLogin') }) }, // 左滑删除 slideButtonTap(e) { // wx.showLoading({ // title: '操作中', // mask: true // }) let item = e.currentTarget.dataset.item; let customerId = item.customerId; let lastChatRecordCode = item.lastChatRecordCode; let msgData = this.data.msgData; msgData.forEach((el, inx) => { if (el.customerId == customerId) { msgData.splice(inx, 1); } }) this.setData({ msgData: msgData }) $request.post('/businessCommunicationDemand/delChatRecordTable/' + customerId + '/' + lastChatRecordCode + '.action').then(res => { // if (res.status == 0) { // let msgData = this.data.msgData; // msgData.forEach((el, inx) => { // if (el.businessCommunicationCustomerVO.customerId == customerId) { // msgData.splice(inx, 1); // } // }) // this.setData({ // msgData: msgData // }) // } // wx.hideLoading() }).catch(err => { // wx.hideLoading() console.log(err); }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { // if (this.data.onRefresh) { // this.setData({ // itemLoading: true // }) // this.getListData(); // } }, // 去关注 followApplets() { wx.navigateTo({ url: "/pages/outLink/outLink?followApplets=true", //跳转页面的路径,可带参数 ?隔开,不同参数用 & 分隔;相对路径,不需要.wxml后缀 success: function () { }, //成功后的回调; fail: function () { }, //失败后的回调; complete: function () { } //结束后的回调(成功,失败都会执行) }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { wx.closeSocket(); // 检测到全局的 websocket 是连接的 关闭 if (app.globalData.isOnSocketOpen) { app.globalData.isOnSocketOpen = false; } }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { wx.closeSocket(); // 检测到全局的 websocket 是连接的 关闭 if (app.globalData.isOnSocketOpen) { app.globalData.isOnSocketOpen = false; } }, // 公众号组件加载成功 bindload(e){ if(e.detail.status == 0){ this.setData({ topShow:true }) } }, // 公众号没有显示原因 binderror(e){ console.log(e); if(e.detail.status != 0){ this.setData({ topShow:false }) } }, /** * 用户点击右上角分享给朋友 */ onShareAppMessage() { return { title: "分享你一个需求交流平台!推荐你一起来关注~", imageUrl: '/images/home/ShareApp-index.png', path: 'pages/index/index' } }, })