chatIndex.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var websocket = require('../../../services/websocket.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. chatList: [],
  8. offsetTime: null,
  9. size: 10,
  10. },
  11. onLoad: function(options) {
  12. // 页面初始化 options为页面跳转所带来的参数
  13. },
  14. onReady: function() {
  15. // 页面渲染完成
  16. },
  17. onShow: function() {
  18. // 页面显示
  19. let now = new Date();
  20. this.setData({
  21. offsetTime: now.toISOString(),
  22. chatList: []
  23. })
  24. if (wx.getStorageSync('token')) {
  25. this.getChatList();
  26. this.openListen();
  27. }
  28. },
  29. onHide: function() {
  30. // 页面隐藏
  31. websocket.listenBadge()
  32. },
  33. onUnload: function() {
  34. // 页面关闭
  35. },
  36. getChatList: function() {
  37. let that = this;
  38. util.request(api.ChatIndex, {
  39. size: this.data.size,
  40. offsetTime: this.data.offsetTime
  41. }).then(function(res) {
  42. if (res.errno === 0) {
  43. console.log(res.data);
  44. that.setData({
  45. chatList: that.data.chatList.concat(res.data.chats),
  46. offsetTime: res.data.offsetTime
  47. });
  48. } else {
  49. console.log(res)
  50. }
  51. })
  52. },
  53. navForm: function(e) {
  54. var chatId = e.currentTarget.dataset.id
  55. var index = e.currentTarget.dataset.index
  56. var chatList = this.data.chatList
  57. //减少tapbar的badge
  58. var lessBadge = chatList[index].unreadCount
  59. websocket.lessBadge(lessBadge)
  60. //减少列表用户的badge
  61. chatList[index].unreadCount = 0
  62. this.setData({
  63. chatList: chatList
  64. })
  65. wx.navigateTo({
  66. url: '/pages/chat/chatForm/chatForm?id=' + chatId,
  67. })
  68. },
  69. openListen: function() {
  70. let that = this
  71. websocket.listenChatIndex().then(res => {
  72. console.log("chatIndex监听到消息:" + res)
  73. //ws监听到新消息,加入当前列表中
  74. let chatList = this.data.chatList
  75. for (var i in chatList) {
  76. //存在与目前list中
  77. if (chatList[i].lastChat.chatId == res.chatId) {
  78. var target = chatList[i]
  79. var newChatList = []
  80. target.unreadCount++;
  81. target.u1ToU2 = res.senderId < res.receiverId ? true : false
  82. target.lastChat.messageType = res.messageType
  83. target.lastChat.messageBody = res.messageBody
  84. target.lastChat.sendTime = res.sendTime
  85. chatList.splice(i, 1);
  86. console.log(chatList)
  87. newChatList.push(target)
  88. newChatList = newChatList.concat(chatList)
  89. that.setData({
  90. chatList: newChatList
  91. })
  92. that.openListen()
  93. return
  94. }
  95. }
  96. //不存在, 后端可以专门写个api
  97. that.onShow()
  98. })
  99. },
  100. onPullDownRefresh: function() {
  101. console.log("上拉刷新")
  102. this.setData({
  103. chatList: [],
  104. offsetTime: null,
  105. size: 10,
  106. })
  107. this.onShow()
  108. setTimeout(function callback() {
  109. wx.stopPullDownRefresh()
  110. }, 500)
  111. },
  112. onReachBottom: function() {
  113. console.log("拉到底")
  114. this.getChatList()
  115. },
  116. })