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

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