123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- // 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,
- 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;
- // console.log()
- //建立连接
- wx.connectSocket({
- url: `ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`,//本地
- 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.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
- })
- },
- // 聊一聊
- 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.businessCommunicationCustomerVO.customerId;
- let lastChatRecordCode = item.lastChatRecordCode;
- let msgData = this.data.msgData;
- msgData.forEach((el, inx) => {
- if (el.businessCommunicationCustomerVO.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;
- }
- },
- /**
- * 用户点击右上角分享给朋友
- */
- onShareAppMessage() {
- return {
- title: "分享你一个需求交流平台!推荐你一起来关注~",
- imageUrl: '/images/home/ShareApp-index.png',
- path: 'pages/index/index'
- }
- },
- })
|