chatIndex.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. //存在与目前list中
  18. let chatList = this.data.chatList
  19. for (var i in chatList) {
  20. if (chatList[i].lastChat.chatId == res.chatId) {
  21. var target = chatList[i]
  22. var newChatList = []
  23. target.unreadCount++;
  24. target.u1ToU2 = res.senderId < res.receiverId ? true : false
  25. target.lastChat.messageType = res.messageType
  26. target.lastChat.messageBody = res.messageBody
  27. target.lastChat.sendTime = res.sendTime
  28. chatList.splice(i, 1);
  29. console.log("splice")
  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),
  76. });
  77. } else {
  78. console.log(res)
  79. }
  80. })
  81. },
  82. navForm: function(e) {
  83. var chatId = e.currentTarget.dataset.id
  84. var index = e.currentTarget.dataset.index
  85. var chatList = this.data.chatList
  86. //减少tapbar的badge
  87. var lessBadge = chatList[index].unreadCount
  88. websocket.lessBadge(lessBadge)
  89. //减少列表用户的badge
  90. chatList[index].unreadCount = 0
  91. this.setData({
  92. chatList: chatList
  93. })
  94. wx.navigateTo({
  95. url: '/pages/chat/chatForm/chatForm?id=' + chatId,
  96. })
  97. },
  98. onPullDownRefresh: function() {
  99. console.log("上拉刷新")
  100. this.setData({
  101. chatList: [],
  102. offsetTime: null,
  103. size: 10,
  104. })
  105. this.onShow()
  106. setTimeout(function callback() {
  107. wx.stopPullDownRefresh()
  108. }, 500)
  109. },
  110. onReachBottom: function() {
  111. console.log("拉到底")
  112. let chatList = this.data.chatList;
  113. let offsetTime = chatList[chatList.length - 1].offsetTime;
  114. this.setData({
  115. offsetTime: offsetTime,
  116. })
  117. this.getChatList()
  118. },
  119. })