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

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