业务交流通
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // pages/release/release.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const $request = require('../../utils/request.js');
  5. const $util = require('../../utils/util.js');
  6. Page({
  7. data: {
  8. currentIndex: 2,
  9. pageLoading: true,
  10. customerId: "",
  11. // 前往关注公众号
  12. topShow: true,
  13. msgData: [],
  14. page: 1,
  15. size: 10,
  16. total: 0,
  17. // 是否加载数据,true加载,false不加载
  18. onRefresh: true,
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad() {
  24. },
  25. onShow() {
  26. if (!app.globalData.customerId) {
  27. app.goLogin();
  28. return
  29. }
  30. const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null;
  31. if (businessCommunicationCustomer) {
  32. this.setData({
  33. customerId: businessCommunicationCustomer.customerId
  34. })
  35. }
  36. // 初始化websocket
  37. this.initWebSocket();
  38. // 初始化数据
  39. this.initData();
  40. },
  41. // websocket初始化
  42. initWebSocket: function () {
  43. var _this = this;
  44. let { customerId } = _this.data;
  45. // console.log()
  46. //建立连接
  47. wx.connectSocket({
  48. url: `ws://192.168.18.138/webSocket/{"userno":${customerId},"messageModule":"010"}`,//本地
  49. success: function () {
  50. console.log('消息页-websocket连接成功~')
  51. },
  52. fail: function () {
  53. console.log('消息页-websocket连接失败~')
  54. },
  55. })
  56. //连接成功
  57. wx.onSocketOpen(function () {
  58. console.log('消息页-连接成功,真正的成功', 'onSocketOpen');
  59. })
  60. // 接收服务器的消息事件
  61. wx.onSocketMessage(function (res) {
  62. // 接收到的消息{date,message,type} type类型为 1 是对方的消息 为 0 是自己的消息
  63. console.log(res, '消息页----收到新消息')
  64. // _this.initData();
  65. let _data = JSON.parse(res.data);
  66. _data.chatRecord = JSON.parse(_data.chatRecord);
  67. let msgData = _this.data.msgData;
  68. msgData.forEach(el => {
  69. // 如果当前消息的 customerId = 发送人的ID
  70. if (el.businessCommunicationCustomerVO.customerId == _data.chatRecord.sender) {
  71. el.lastContent = _data.chatRecord.content;
  72. el.lastContentDate = $util.formatTime(new Date(_data.chatRecord.sendTime));
  73. el.lastContentDate = el.lastContentDate.replaceAll('/', '-');
  74. el.size += 1;
  75. }
  76. })
  77. _this.setData({
  78. msgData
  79. })
  80. })
  81. // 监听连接关闭
  82. wx.onSocketClose(function () {
  83. console.log('监听 WebSocket 连接关闭事件')
  84. })
  85. },
  86. // 关闭顶部关注
  87. closeTopShow() {
  88. this.setData({
  89. topShow: false
  90. })
  91. },
  92. // 聊一聊
  93. goToChat(e) {
  94. let item = e.currentTarget.dataset.item;
  95. wx.navigateTo({
  96. url: "/pages/msgModule/wechat2/wechat2",
  97. success: function (res) {
  98. // 通过eventChannel向被打开页面传送数据
  99. res.eventChannel.emit('customerid', { customerid: item.businessCommunicationCustomerVO.customerId, chatHeads: item.businessCommunicationCustomerVO.chatHeads })
  100. }
  101. })
  102. },
  103. initData() {
  104. this.setData({
  105. page: 1,
  106. size: 10,
  107. msgData: [],
  108. pageLoading: true,
  109. })
  110. this.getMsgData();
  111. },
  112. getMsgData() {
  113. let { page, size } = this.data;
  114. $request.get('/businessCommunicationDemand/getChatRecordTable.action', { page, size }).then(res => {
  115. let tempListData = this.data.msgData;
  116. if (res.status == 0) {
  117. let datas = res.data;
  118. // 先push数据
  119. tempListData.push(...datas.chatRecordTable);
  120. tempListData.forEach(el => {
  121. el.lastContentDate = $util.formatTime(new Date(el.lastContentDate));
  122. el.lastContentDate = el.lastContentDate.replaceAll('/', '-');
  123. if (typeof el.type == 'string') {
  124. el.type = el.type.split(',');
  125. }
  126. })
  127. // 设置总数
  128. this.setData({
  129. msgData: tempListData,
  130. total: datas.total,
  131. })
  132. // 如果数据大于了返回的总数
  133. if (tempListData.length >= this.data.total) {
  134. // 停止累加数据
  135. this.setData({
  136. onRefresh: false,
  137. itemLoading: false,
  138. })
  139. } else {
  140. this.setData({
  141. onRefresh: true,
  142. itemLoading: false,
  143. page: page + 1
  144. })
  145. }
  146. console.log('-------------消息 beg-------------------')
  147. console.log(this.data.msgData);
  148. console.log('-------------消息 end-------------------')
  149. }
  150. this.setData({
  151. pageLoading: false,
  152. })
  153. }).catch(error => {
  154. console.log(error, 'error appletLogin')
  155. })
  156. },
  157. /**
  158. * 页面上拉触底事件的处理函数
  159. */
  160. onReachBottom: function () {
  161. if (this.data.onRefresh) {
  162. this.setData({
  163. itemLoading: true
  164. })
  165. this.getListData();
  166. }
  167. },
  168. // 去关注
  169. followApplets() {
  170. wx.navigateTo({
  171. url: "/pages/outLink/outLink?followApplets=true", //跳转页面的路径,可带参数 ?隔开,不同参数用 & 分隔;相对路径,不需要.wxml后缀
  172. success: function () { }, //成功后的回调;
  173. fail: function () { }, //失败后的回调;
  174. complete: function () { } //结束后的回调(成功,失败都会执行)
  175. })
  176. },
  177. /**
  178. * 生命周期函数--监听页面隐藏
  179. */
  180. onHide: function () {
  181. wx.closeSocket();
  182. },
  183. /**
  184. * 生命周期函数--监听页面卸载
  185. */
  186. onUnload: function () {
  187. wx.closeSocket();
  188. },
  189. /**
  190. * 用户点击右上角分享给朋友
  191. */
  192. onShareAppMessage() {
  193. return {
  194. title: "分享你一个需求交流平台!推荐你一起来关注~",
  195. imageUrl: '/images/home/ShareApp-index.png',
  196. path: 'pages/index/index'
  197. }
  198. },
  199. })