业务交流通
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

index.js 10KB

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