Browse Source

修改聊天相关功能

pull/11/head
徐飞洋 2 months ago
parent
commit
d11ae8c9c3
  1. 16
      pages/chat-detail/index.js
  2. 116
      pages/chat/index.js

16
pages/chat-detail/index.js

@ -178,9 +178,21 @@ Page({
let goodsData = null;
try {
// 第一次解析message.content
const parsed = JSON.parse(message.content);
if (parsed.type === 'goods' && parsed.goodsData) {
goodsData = parsed.goodsData;
if (parsed.type === 'goods') {
// 第二次解析goodsData,因为它可能也是一个JSON字符串
if (typeof parsed.goodsData === 'string') {
goodsData = JSON.parse(parsed.goodsData);
} else {
goodsData = parsed.goodsData;
}
// 清理imageUrl字段中的多余反引号
if (goodsData && goodsData.imageUrl) {
goodsData.imageUrl = goodsData.imageUrl.replace(/[`\\]/g, '');
}
content = parsed.text || '';
}
} catch (e) {

116
pages/chat/index.js

@ -285,43 +285,70 @@ Page({
const viewedChats = wx.getStorageSync('viewedChats') || {};
const chatId = chatItem.manager_phone;
// 获取最新消息内容和时间
let lastMessage = null;
// 首先处理原始聊天项内容,防止直接显示JSON
if (chatItem.content) {
let content = chatItem.content;
try {
const cacheKey = chatId;
const cache = this.data.chatContentCache[cacheKey];
const now = Date.now();
// 每次刷新列表时都尝试获取最新消息,确保消息实时性
const messages = await API.getChatMessages(chatId, userPhone, { limit: 1 });
if (messages.length > 0) {
lastMessage = messages[0];
chatItem.content = lastMessage.content || '暂无消息内容';
// 同时更新时间为最新消息的时间
chatItem.time = lastMessage.created_at || null;
// 更新缓存
const newCache = {
...this.data.chatContentCache,
[cacheKey]: {
content: chatItem.content,
time: chatItem.time,
timestamp: now,
messageId: lastMessage.id || lastMessage.message_id || null,
senderPhone: lastMessage.sender_phone || null // 保存发送者手机号
}
};
this.setData({ chatContentCache: newCache });
console.log('更新聊天内容缓存:', cacheKey);
} else if (!chatItem.content) {
chatItem.content = '暂无消息内容';
const parsed = JSON.parse(content);
if (parsed.type === 'goods') {
content = '[商品卡片]';
}
chatItem.content = content;
} catch (e) {
// 不是JSON格式,使用原始内容
}
}
// 获取最新消息内容和时间
let lastMessage = null;
try {
const cacheKey = chatId;
const cache = this.data.chatContentCache[cacheKey];
const now = Date.now();
// 每次刷新列表时都尝试获取最新消息,确保消息实时性
const messages = await API.getChatMessages(chatId, userPhone, { limit: 1 });
if (messages.length > 0) {
lastMessage = messages[0];
// 先处理内容,再更新缓存和聊天项
let content = lastMessage.content || '暂无消息内容';
// 解析可能的JSON内容
try {
const parsed = JSON.parse(content);
if (parsed.type === 'goods') {
// 商品消息,显示友好的提示
content = '[商品卡片]';
}
} catch (e) {
// 不是JSON格式,使用原始内容
}
// 更新聊天项内容和时间
chatItem.content = content;
chatItem.time = lastMessage.created_at || null;
// 更新缓存,保存处理后的内容
const newCache = {
...this.data.chatContentCache,
[cacheKey]: {
content: content, // 保存处理后的内容,而不是原始JSON
time: chatItem.time,
timestamp: now,
messageId: lastMessage.id || lastMessage.message_id || null,
senderPhone: lastMessage.sender_phone || null // 保存发送者手机号
}
};
this.setData({ chatContentCache: newCache });
console.log('更新聊天内容缓存:', cacheKey);
}
} catch (error) {
console.error('获取聊天消息失败:', error);
// 如果获取失败,尝试使用缓存内容
const cacheKey = chatId;
const cache = this.data.chatContentCache[cacheKey];
if (cache && !chatItem.content) {
// 缓存中的内容已经是处理过的,直接使用
chatItem.content = cache.content;
chatItem.time = cache.time;
console.log('获取最新消息失败,使用缓存内容:', cacheKey);
@ -330,6 +357,37 @@ Page({
}
}
// 确保无论如何都处理JSON内容,防止显示原始JSON
let content = chatItem.content || '暂无消息内容';
try {
const parsed = JSON.parse(content);
if (parsed.type === 'goods') {
content = '[商品卡片]';
chatItem.content = content;
// 如果没有缓存,创建新的缓存
const cacheKey = chatId;
const cache = this.data.chatContentCache[cacheKey];
if (cacheKey && !cache) {
const now = Date.now();
const newCache = {
...this.data.chatContentCache,
[cacheKey]: {
content: content,
time: chatItem.time,
timestamp: now,
messageId: null,
senderPhone: null
}
};
this.setData({ chatContentCache: newCache });
console.log('创建新的缓存,保存处理后的内容:', cacheKey);
}
}
} catch (e) {
// 不是JSON格式,使用原始内容
}
// 格式化时间
chatItem.time = this.formatDateTime(chatItem.time || null);

Loading…
Cancel
Save