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