业务交流通
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 6.6KB

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