chatIndex.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. this.getChatList();
  55. this.openListen();
  56. },
  57. onHide: function() {
  58. // 页面隐藏
  59. websocket.listenBadge()
  60. },
  61. onUnload: function() {
  62. // 页面关闭
  63. },
  64. getChatList: function() {
  65. let that = this;
  66. util.request(api.ChatIndex, {
  67. size: this.data.size,
  68. offsetTime: this.data.offsetTime
  69. }).then(function(res) {
  70. if (res.errno === 0) {
  71. console.log(res.data);
  72. that.setData({
  73. chatList: that.data.chatList.concat(res.data),
  74. });
  75. } else {
  76. console.log(res)
  77. }
  78. })
  79. },
  80. navForm: function(e) {
  81. var chatId = e.currentTarget.dataset.id
  82. var index = e.currentTarget.dataset.index
  83. var chatList = this.data.chatList
  84. //减少tapbar的badge
  85. var lessBadge = chatList[index].unreadCount
  86. websocket.lessBadge(lessBadge)
  87. //减少列表用户的badge
  88. chatList[index].unreadCount = 0
  89. this.setData({
  90. chatList: chatList
  91. })
  92. wx.navigateTo({
  93. url: '/pages/chat/chatForm/chatForm?id=' + chatId,
  94. })
  95. },
  96. onPullDownRefresh: function() {
  97. console.log("上拉刷新")
  98. this.setData({
  99. chatList: [],
  100. offsetTime: null,
  101. size: 10,
  102. })
  103. this.onShow()
  104. setTimeout(function callback() {
  105. wx.stopPullDownRefresh()
  106. }, 500)
  107. },
  108. onReachBottom: function() {
  109. console.log("拉到底")
  110. let chatList = this.data.chatList;
  111. let offsetTime = chatList[chatList.length - 1].offsetTime;
  112. this.setData({
  113. offsetTime: offsetTime,
  114. })
  115. this.getChatList()
  116. },
  117. })