业务交流通
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 8.7KB

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