123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- // pages/chat-list/chat-list.js
- import ImApi from './../../apis/im'
- import { fotmatDate } from './../../utils/date'
- /**
- * 会话列表页面
- */
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- conversations: []
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- },
- toChat(e) {
- let app = getApp()
- getApp().autoSubcrebe && getApp().autoSubcrebe()
- let item = e.currentTarget.dataset.item;
- app.globalData.unViewMsg = app.globalData.unViewMsg - item.unReadNum
- delete item.latestMsg;
- delete item.unread;
- delete item.content;
- const { conversations } = this.data
- const i = conversations.find(i => i.id === item.id)
- i.unread = 0
- this.setData({
- conversations
- })
- wx.navigateTo({
- url: `../chat/chat?toId=${item.id}&toName=${item.name}`
- });
- },
- /**
- * 生命周期函数--监听页面显示
- */
- async onShow() {
- await getApp().getContact()
- this.setData({
- conversations: getApp().globalData.conversations
- })
- this.listener = (msg) => {
- wx.nextTick(() => {
- this.setData({
- conversations: getApp().globalData.conversations
- })
- })
- }
- getApp().getIMHandler().setOnReceiveMessageListener({
- listener: this.listener
- });
- },
- onHide () {
- getApp().getIMHandler().removeOnReceiveMessageListener({
- listener: this.listener
- })
- },
- async getNewMessage (msg) {
- const { conversations } = this.data
- let item = conversations.find(item => item.id === msg.fromId)
- if (item) {
- item.latestMsgContent = msg.content
- item.unread ? item.unread++ : item.unread = 1
- this.setData({
- conversations: this.dealConversations(conversations)
- })
- } else {
- await getApp().getContact()
- this.setData({
- conversations: getApp().globalData.conversations
- })
- }
- },
- getContact () {
- ImApi.getContacts().then(res => {
- const friends = res.data.friends
- const { conversations } = this.data
-
- this.setData({
- conversations: this.dealConversations(friends.map(item => {
- const con = conversations.find(con => con.id === item.id)
- if (con) {
- item = Object.assign(con, item)
- }
- item.latestMsgTime = f
- return item
- }))
- })
- })
- },
- dealConversations (conversations) {
- return conversations.map(item => {
- let content = item.latestMsgContent
- try {
- let parseContent = JSON.parse(content)
- if (parseContent.house_name) {
- content = `【语音云带看】${JSON.parse(content).house_name}`
- } else if (parseContent.duration) {
- content = '【语音】'
- } else if (parseContent.content) {
- content = '【图片】'
- }
-
- } catch (err) {
- }
- item.latestMsgContent = content
-
- return item
- }).sort((a,b) => new Date(b.latestMsgTime) - new Date(a.latestMsgTime))
- },
- getConversationsItem(item) {
- let {latestMsg, ...msg} = item;
- return Object.assign(msg, JSON.parse(latestMsg));
- }
- });
|