业务交流通
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. // _this.initData();
  72. let _data = JSON.parse(res.data);
  73. _data.chatRecord = JSON.parse(_data.chatRecord);
  74. console.log(_data, '消息页----收到新消息')
  75. let msgData = _this.data.msgData;
  76. // 初始化新消息
  77. let isNewMsg = true;
  78. // 循环数据,检测发送人ID是否存在,如果不存在就是新消息。
  79. msgData.forEach(el => {
  80. if (el.businessCommunicationCustomerVO.customerId == _data.sendno) {
  81. isNewMsg = false;
  82. }
  83. })
  84. console.log(`是已存在列表框的还是不存在列表框`, isNewMsg)
  85. // 如果是新消息
  86. if (isNewMsg) {
  87. let obj = {};
  88. obj.lastContent = _data.chatRecord.chatRecord.content;
  89. obj.lastContentDate = $util.formatTime(new Date(_data.chatRecord.chatRecord.sendTime));
  90. obj.lastContentDate = obj.lastContentDate.replaceAll('/', '-');
  91. obj.size = 1;
  92. obj.customer = _data.chatRecord.senderCustomer;
  93. obj.businessCommunicationCustomerVO = _data.chatRecord.senderBusinessCommunicationCustomer;
  94. obj.messageType = _data.chatRecord.messageType;
  95. if (obj.customer) {
  96. if (obj.customer.paidByMonth) {
  97. obj.businessCommunicationCustomerVO.nickName = obj.customer.nickName;
  98. }
  99. }
  100. msgData.unshift(obj);
  101. } else {
  102. msgData.forEach(el => {
  103. // 如果当前消息的 customerId = 发送人的ID
  104. if (el.businessCommunicationCustomerVO.customerId == _data.chatRecord.chatRecord.sender) {
  105. el.lastContent = _data.chatRecord.chatRecord.content;
  106. el.lastContentDate = $util.formatTime(new Date(_data.chatRecord.chatRecord.sendTime));
  107. el.lastContentDate = el.lastContentDate.replaceAll('/', '-');
  108. el.size += 1;
  109. }
  110. if (el.customer) {
  111. if (el.customer.paidByMonth) {
  112. el.businessCommunicationCustomerVO.nickName = el.customer.nickName
  113. }
  114. }
  115. })
  116. }
  117. _this.setData({
  118. msgData
  119. })
  120. })
  121. // 监听连接关闭
  122. wx.onSocketClose(function () {
  123. console.log('监听 WebSocket 连接关闭事件')
  124. })
  125. },
  126. // 关闭顶部关注
  127. closeTopShow() {
  128. this.setData({
  129. topShow: false
  130. })
  131. },
  132. // 聊一聊
  133. goToChat(e) {
  134. let item = e.currentTarget.dataset.item;
  135. wx.navigateTo({
  136. url: "/pages/msgModule/wechat2/wechat2",
  137. success: function (res) {
  138. // 通过eventChannel向被打开页面传送数据
  139. res.eventChannel.emit('customerid', { customerid: item.businessCommunicationCustomerVO.customerId, chatHeads: item.businessCommunicationCustomerVO.chatHeads })
  140. }
  141. })
  142. },
  143. initData() {
  144. this.setData({
  145. page: 1,
  146. size: 10,
  147. msgData: [],
  148. pageLoading: true,
  149. })
  150. this.getMsgData();
  151. },
  152. getMsgData() {
  153. // let { page, size } = this.data;
  154. $request.get('/businessCommunicationDemand/getChatRecordTable.action').then(res => {
  155. let tempListData = this.data.msgData;
  156. if (res.status == 0) {
  157. let datas = res.data;
  158. // 先push数据
  159. tempListData.push(...datas.chatRecordTable);
  160. tempListData.forEach(el => {
  161. el.lastContentDate = $util.formatTime(new Date(el.lastContentDate));
  162. el.lastContentDate = el.lastContentDate.replaceAll('/', '-');
  163. if (typeof el.type == 'string') {
  164. el.type = el.type.split(',');
  165. }
  166. if (el.customer) {
  167. if (el.customer.paidByMonth) {
  168. el.businessCommunicationCustomerVO.nickName = el.customer.nickName
  169. }
  170. }
  171. })
  172. // 设置总数
  173. this.setData({
  174. msgData: tempListData,
  175. total: datas.total,
  176. })
  177. }
  178. this.setData({
  179. pageLoading: false,
  180. })
  181. }).catch(error => {
  182. console.log(error, 'error appletLogin')
  183. })
  184. },
  185. // 左滑删除
  186. slideButtonTap(e) {
  187. // wx.showLoading({
  188. // title: '操作中',
  189. // mask: true
  190. // })
  191. let item = e.currentTarget.dataset.item;
  192. let customerId = item.businessCommunicationCustomerVO.customerId;
  193. let lastChatRecordCode = item.lastChatRecordCode;
  194. let msgData = this.data.msgData;
  195. msgData.forEach((el, inx) => {
  196. if (el.businessCommunicationCustomerVO.customerId == customerId) {
  197. msgData.splice(inx, 1);
  198. }
  199. })
  200. this.setData({
  201. msgData: msgData
  202. })
  203. $request.post('/businessCommunicationDemand/delChatRecordTable/' + customerId + '/' + lastChatRecordCode + '.action').then(res => {
  204. // if (res.status == 0) {
  205. // let msgData = this.data.msgData;
  206. // msgData.forEach((el, inx) => {
  207. // if (el.businessCommunicationCustomerVO.customerId == customerId) {
  208. // msgData.splice(inx, 1);
  209. // }
  210. // })
  211. // this.setData({
  212. // msgData: msgData
  213. // })
  214. // }
  215. // wx.hideLoading()
  216. }).catch(err => {
  217. // wx.hideLoading()
  218. console.log(err);
  219. })
  220. },
  221. /**
  222. * 页面上拉触底事件的处理函数
  223. */
  224. onReachBottom: function () {
  225. // if (this.data.onRefresh) {
  226. // this.setData({
  227. // itemLoading: true
  228. // })
  229. // this.getListData();
  230. // }
  231. },
  232. // 去关注
  233. followApplets() {
  234. wx.navigateTo({
  235. url: "/pages/outLink/outLink?followApplets=true", //跳转页面的路径,可带参数 ?隔开,不同参数用 & 分隔;相对路径,不需要.wxml后缀
  236. success: function () { }, //成功后的回调;
  237. fail: function () { }, //失败后的回调;
  238. complete: function () { } //结束后的回调(成功,失败都会执行)
  239. })
  240. },
  241. /**
  242. * 生命周期函数--监听页面隐藏
  243. */
  244. onHide: function () {
  245. wx.closeSocket();
  246. // 检测到全局的 websocket 是连接的 关闭
  247. if (app.globalData.isOnSocketOpen) {
  248. app.globalData.isOnSocketOpen = false;
  249. }
  250. },
  251. /**
  252. * 生命周期函数--监听页面卸载
  253. */
  254. onUnload: function () {
  255. wx.closeSocket();
  256. // 检测到全局的 websocket 是连接的 关闭
  257. if (app.globalData.isOnSocketOpen) {
  258. app.globalData.isOnSocketOpen = false;
  259. }
  260. },
  261. /**
  262. * 用户点击右上角分享给朋友
  263. */
  264. onShareAppMessage() {
  265. return {
  266. title: "分享你一个需求交流平台!推荐你一起来关注~",
  267. imageUrl: '/images/home/ShareApp-index.png',
  268. path: 'pages/index/index'
  269. }
  270. },
  271. })