chore:智能客服的历史时间回复正常

This commit is contained in:
2026-04-16 14:03:37 +08:00
parent 524280d816
commit ffbc15929d

View File

@@ -571,18 +571,29 @@ async loadChatHistoryIfExist() {
// 注意:getConversationHistory 返回的数据结构与 getChatHistory 预期的不同
// 需要根据实际返回的 response.data.messages 来映射
console.log('【调试】后端返回的历史消息数据:', response.messages);
this.messages = response.messages.map(msg => {
console.log('【调试】单条消息完整数据:', JSON.stringify(msg));
console.log('【调试】单条消息时间字段:');
console.log(' - created_at:', msg.created_at, typeof msg.created_at);
console.log(' - create_time:', msg.create_time, typeof msg.create_time);
console.log(' - time:', msg.time, typeof msg.time);
console.log(' - Date.now():', Date.now());
let content = '';
if (msg.role === 'user') {
content = msg.query || (typeof msg.inputs === 'string' ? msg.inputs : JSON.stringify(msg.inputs || '')) || '';
} else {
content = msg.answer || '';
}
// 只使用后端返回的真实时间,不使用假时间
const realTimestamp = msg.created_at || msg.create_time || msg.time;
console.log('【调试】最终使用的时间戳:', realTimestamp);
return {
id: msg.message_id || (Date.now() + Math.random() * 10000),
role: msg.role === 'user' ? 'user' : 'assistant',
content: content,
timestamp: msg.created_at * 1000, // 后端时间戳通常是秒,前端需要毫秒
timestamp: realTimestamp,
actions: msg.role !== 'user' ? [
{ id: 1, text: '有帮助', type: 'like' },
{ id: 2, text: '没帮助', type: 'dislike' }
@@ -1041,10 +1052,59 @@ async loadChatHistoryIfExist() {
// 格式化时间
formatTime(timestamp) {
const date = new Date(timestamp)
const hours = date.getHours().toString().padStart(2, '0')
const minutes = date.getMinutes().toString().padStart(2, '0')
return hours + ':' + minutes
console.log('【调试】formatTime 接收到的时间戳:', timestamp, typeof timestamp);
// 如果没有时间戳,就不显示时间
if (!timestamp) {
console.warn('【调试】没有时间戳');
return '';
}
let date;
// 如果是字符串格式,例如 "2026-04-16 03:56:20"
if (typeof timestamp === 'string') {
// 手动解析格式 "YYYY-MM-DD HH:mm:ss"
const parts = timestamp.match(/(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})/);
if (parts) {
const [, year, month, day, hour, minute, second] = parts;
// 先解析成本地时间,然后加上8小时时区差
date = new Date(parseInt(year), parseInt(month) - 1, parseInt(day), parseInt(hour), parseInt(minute), parseInt(second));
// 加上8小时
date.setHours(date.getHours() + 8);
} else {
// 其他字符串格式直接解析
date = new Date(timestamp);
}
}
// 如果是数字格式
else {
let timeValue = timestamp;
// 如果时间戳小于10000000000,说明是秒,需要乘以1000转成毫秒
if (typeof timeValue === 'number' && timeValue < 10000000000) {
timeValue = timeValue * 1000;
}
date = new Date(timeValue);
}
console.log('【调试】最终 Date 对象:', date);
console.log('【调试】本地时间:', date.toLocaleString());
console.log('【调试】小时:', date.getHours());
console.log('【调试】分钟:', date.getMinutes());
// 如果时间无效,就不显示时间
if (isNaN(date.getTime())) {
console.warn('【调试】时间无效:', timestamp);
return '';
}
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const result = hours + ':' + minutes;
console.log('【调试】最终显示时间:', result);
return result;
},
// 格式化文件大小
@@ -1109,13 +1169,19 @@ async loadChatHistoryIfExist() {
});
if (response.success && Array.isArray(response.messages) && response.messages.length > 0) {
const historyMessages = response.messages.map(msg => ({
id: msg.id || `history-${Date.now()}-${Math.random().toString(36).slice(2)}`,
role: msg.role === 'user' ? 'user' : 'ai',
type: 'text',
content: msg.content,
timestamp: msg.create_time * 1000
}));
console.log('【调试】加载更多历史消息数据:', response.messages);
const historyMessages = response.messages.map(msg => {
console.log('【调试】加载更多单条消息数据:', msg);
// 只使用后端返回的真实时间,不使用假时间
const realTimestamp = msg.create_time || msg.created_at || msg.time;
return {
id: msg.id || `history-${Date.now()}-${Math.random().toString(36).slice(2)}`,
role: msg.role === 'user' ? 'user' : 'ai',
type: 'text',
content: msg.content,
timestamp: realTimestamp
};
});
// 1. 记录新增消息数量
const newMsgCount = historyMessages.length;