chat-list.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // pages/chat-list/chat-list.js
  2. import ImApi from './../../apis/im'
  3. import { fotmatDate } from './../../utils/date'
  4. /**
  5. * 会话列表页面
  6. */
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. conversations: []
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad(options) {
  18. },
  19. toChat(e) {
  20. let app = getApp()
  21. getApp().autoSubcrebe && getApp().autoSubcrebe()
  22. let item = e.currentTarget.dataset.item;
  23. app.globalData.unViewMsg = app.globalData.unViewMsg - item.unReadNum
  24. delete item.latestMsg;
  25. delete item.unread;
  26. delete item.content;
  27. const { conversations } = this.data
  28. const i = conversations.find(i => i.id === item.id)
  29. i.unread = 0
  30. this.setData({
  31. conversations
  32. })
  33. wx.navigateTo({
  34. url: `../chat/chat?toId=${item.id}&toName=${item.name}`
  35. });
  36. },
  37. /**
  38. * 生命周期函数--监听页面显示
  39. */
  40. async onShow() {
  41. await getApp().getContact()
  42. this.setData({
  43. conversations: getApp().globalData.conversations
  44. })
  45. this.listener = (msg) => {
  46. wx.nextTick(() => {
  47. this.setData({
  48. conversations: getApp().globalData.conversations
  49. })
  50. })
  51. }
  52. getApp().getIMHandler().setOnReceiveMessageListener({
  53. listener: this.listener
  54. });
  55. },
  56. onHide () {
  57. getApp().getIMHandler().removeOnReceiveMessageListener({
  58. listener: this.listener
  59. })
  60. },
  61. async getNewMessage (msg) {
  62. const { conversations } = this.data
  63. let item = conversations.find(item => item.id === msg.fromId)
  64. if (item) {
  65. item.latestMsgContent = msg.content
  66. item.unread ? item.unread++ : item.unread = 1
  67. this.setData({
  68. conversations: this.dealConversations(conversations)
  69. })
  70. } else {
  71. await getApp().getContact()
  72. this.setData({
  73. conversations: getApp().globalData.conversations
  74. })
  75. }
  76. },
  77. getContact () {
  78. ImApi.getContacts().then(res => {
  79. const friends = res.data.friends
  80. const { conversations } = this.data
  81. this.setData({
  82. conversations: this.dealConversations(friends.map(item => {
  83. const con = conversations.find(con => con.id === item.id)
  84. if (con) {
  85. item = Object.assign(con, item)
  86. }
  87. item.latestMsgTime = f
  88. return item
  89. }))
  90. })
  91. })
  92. },
  93. dealConversations (conversations) {
  94. return conversations.map(item => {
  95. let content = item.latestMsgContent
  96. try {
  97. let parseContent = JSON.parse(content)
  98. if (parseContent.house_name) {
  99. content = `【语音云带看】${JSON.parse(content).house_name}`
  100. } else if (parseContent.duration) {
  101. content = '【语音】'
  102. } else if (parseContent.content) {
  103. content = '【图片】'
  104. }
  105. } catch (err) {
  106. }
  107. item.latestMsgContent = content
  108. return item
  109. }).sort((a,b) => new Date(b.latestMsgTime) - new Date(a.latestMsgTime))
  110. },
  111. getConversationsItem(item) {
  112. let {latestMsg, ...msg} = item;
  113. return Object.assign(msg, JSON.parse(latestMsg));
  114. }
  115. });