// 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: true, msgData: [], page: 1, size: 10, total: 0, // 是否加载数据,true加载,false不加载 onRefresh: true, }, /** * 生命周期函数--监听页面加载 */ 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; // console.log() //建立连接 wx.connectSocket({ url: `ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`,//本地 success: function () { console.log('消息页-websocket连接成功~') }, fail: function () { console.log('消息页-websocket连接失败~') }, }) //连接成功 wx.onSocketOpen(function () { console.log('消息页-连接成功,真正的成功', 'onSocketOpen'); }) // 接收服务器的消息事件 wx.onSocketMessage(function (res) { // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息 console.log(res, '消息页----收到新消息') // _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; } 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 }) }, // 聊一聊 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){ // console.log(el.customer.paidByMonth,el.customer.nickName) if(el.customer.paidByMonth){ el.businessCommunicationCustomerVO.nickName = el.customer.nickName } } }) // 设置总数 this.setData({ msgData: tempListData, total: datas.total, }) // // 如果数据大于了返回的总数 // if (tempListData.length >= this.data.total) { // // 停止累加数据 // this.setData({ // onRefresh: false, // itemLoading: false, // }) // } else { // this.setData({ // onRefresh: true, // itemLoading: false, // page: page + 1 // }) // } console.log('-------------消息 beg-------------------') console.log(this.data.msgData); console.log('-------------消息 end-------------------') } this.setData({ pageLoading: false, }) }).catch(error => { console.log(error, 'error appletLogin') }) }, /** * 页面上拉触底事件的处理函数 */ 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; } }, /** * 用户点击右上角分享给朋友 */ onShareAppMessage() { return { title: "分享你一个需求交流平台!推荐你一起来关注~", imageUrl: '/images/home/ShareApp-index.png', path: 'pages/index/index' } }, })