业务交流通
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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://` + app.webSocketUrl + `/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. if(el.customer){
  80. if(el.customer.paidByMonth){
  81. el.businessCommunicationCustomerVO.nickName = el.customer.nickName
  82. }
  83. }
  84. })
  85. _this.setData({
  86. msgData
  87. })
  88. })
  89. // 监听连接关闭
  90. wx.onSocketClose(function () {
  91. console.log('监听 WebSocket 连接关闭事件')
  92. })
  93. },
  94. // 关闭顶部关注
  95. closeTopShow() {
  96. this.setData({
  97. topShow: false
  98. })
  99. },
  100. // 聊一聊
  101. goToChat(e) {
  102. let item = e.currentTarget.dataset.item;
  103. wx.navigateTo({
  104. url: "/pages/msgModule/wechat2/wechat2",
  105. success: function (res) {
  106. // 通过eventChannel向被打开页面传送数据
  107. res.eventChannel.emit('customerid', { customerid: item.businessCommunicationCustomerVO.customerId, chatHeads: item.businessCommunicationCustomerVO.chatHeads })
  108. }
  109. })
  110. },
  111. initData() {
  112. this.setData({
  113. page: 1,
  114. size: 10,
  115. msgData: [],
  116. pageLoading: true,
  117. })
  118. this.getMsgData();
  119. },
  120. getMsgData() {
  121. // let { page, size } = this.data;
  122. $request.get('/businessCommunicationDemand/getChatRecordTable.action').then(res => {
  123. let tempListData = this.data.msgData;
  124. if (res.status == 0) {
  125. let datas = res.data;
  126. // 先push数据
  127. tempListData.push(...datas.chatRecordTable);
  128. tempListData.forEach(el => {
  129. el.lastContentDate = $util.formatTime(new Date(el.lastContentDate));
  130. el.lastContentDate = el.lastContentDate.replaceAll('/', '-');
  131. if (typeof el.type == 'string') {
  132. el.type = el.type.split(',');
  133. }
  134. if(el.customer){
  135. // console.log(el.customer.paidByMonth,el.customer.nickName)
  136. if(el.customer.paidByMonth){
  137. el.businessCommunicationCustomerVO.nickName = el.customer.nickName
  138. }
  139. }
  140. })
  141. // 设置总数
  142. this.setData({
  143. msgData: tempListData,
  144. total: datas.total,
  145. })
  146. // // 如果数据大于了返回的总数
  147. // if (tempListData.length >= this.data.total) {
  148. // // 停止累加数据
  149. // this.setData({
  150. // onRefresh: false,
  151. // itemLoading: false,
  152. // })
  153. // } else {
  154. // this.setData({
  155. // onRefresh: true,
  156. // itemLoading: false,
  157. // page: page + 1
  158. // })
  159. // }
  160. console.log('-------------消息 beg-------------------')
  161. console.log(this.data.msgData);
  162. console.log('-------------消息 end-------------------')
  163. }
  164. this.setData({
  165. pageLoading: false,
  166. })
  167. }).catch(error => {
  168. console.log(error, 'error appletLogin')
  169. })
  170. },
  171. /**
  172. * 页面上拉触底事件的处理函数
  173. */
  174. onReachBottom: function () {
  175. // if (this.data.onRefresh) {
  176. // this.setData({
  177. // itemLoading: true
  178. // })
  179. // this.getListData();
  180. // }
  181. },
  182. // 去关注
  183. followApplets() {
  184. wx.navigateTo({
  185. url: "/pages/outLink/outLink?followApplets=true", //跳转页面的路径,可带参数 ?隔开,不同参数用 & 分隔;相对路径,不需要.wxml后缀
  186. success: function () { }, //成功后的回调;
  187. fail: function () { }, //失败后的回调;
  188. complete: function () { } //结束后的回调(成功,失败都会执行)
  189. })
  190. },
  191. /**
  192. * 生命周期函数--监听页面隐藏
  193. */
  194. onHide: function () {
  195. wx.closeSocket();
  196. // 检测到全局的 websocket 是连接的 关闭
  197. if(app.globalData.isOnSocketOpen){
  198. app.globalData.isOnSocketOpen = false;
  199. }
  200. },
  201. /**
  202. * 生命周期函数--监听页面卸载
  203. */
  204. onUnload: function () {
  205. wx.closeSocket();
  206. // 检测到全局的 websocket 是连接的 关闭
  207. if(app.globalData.isOnSocketOpen){
  208. app.globalData.isOnSocketOpen = false;
  209. }
  210. },
  211. /**
  212. * 用户点击右上角分享给朋友
  213. */
  214. onShareAppMessage() {
  215. return {
  216. title: "分享你一个需求交流平台!推荐你一起来关注~",
  217. imageUrl: '/images/home/ShareApp-index.png',
  218. path: 'pages/index/index'
  219. }
  220. },
  221. })