业务交流通
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 6.3KB

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