bug修复
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import request from '@/utils/request';
|
||||
|
||||
export function publishDiary(data: any) {
|
||||
return request.post('/diary-post/publish', data);
|
||||
return request.post('/diaryPost/publish', data);
|
||||
}
|
||||
|
||||
export function getUserDiaries(userId: string, page = 1, size = 10) {
|
||||
return request.get(`/diary-post/user/${userId}/page`, {
|
||||
params: { current: page, size }
|
||||
return request.get('/diaryPost/page', {
|
||||
params: { current: page, size, userId }
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ export interface MessageResponse {
|
||||
isRead: number
|
||||
aiReply?: string
|
||||
emotionAnalysis?: string
|
||||
messageOrder?: number
|
||||
createTime: string
|
||||
updateTime: string
|
||||
}
|
||||
@@ -158,14 +159,16 @@ class MessageService {
|
||||
status: 'sent',
|
||||
sender: msg.sender as 'user' | 'ai' | 'system',
|
||||
isRead: msg.isRead,
|
||||
role: msg.sender === 'user' ? 'user' : 'assistant' // 用于UI显示
|
||||
role: msg.sender === 'user' ? 'user' : 'assistant', // 用于UI显示
|
||||
messageOrder: msg.messageOrder // 消息顺序 - 用于确保消息展示顺序
|
||||
}
|
||||
|
||||
console.log('✅ 转换后的消息详情:', {
|
||||
原始sender: msg.sender,
|
||||
转换后role: chatMessage.role,
|
||||
转换后type: chatMessage.type,
|
||||
时间: msg.createTime + ' -> ' + timestamp
|
||||
时间: msg.createTime + ' -> ' + timestamp,
|
||||
消息顺序: msg.messageOrder
|
||||
})
|
||||
console.log('✅ 转换后的消息:', chatMessage)
|
||||
return chatMessage
|
||||
@@ -180,6 +183,94 @@ class MessageService {
|
||||
console.log('✅ 批量转换完成,结果:', chatMessages)
|
||||
return chatMessages
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析时间戳为毫秒数 - 统一处理各种时间格式
|
||||
* 支持格式:
|
||||
* - "2025-07-26 22:09:10" (后端格式)
|
||||
* - "2025-07-26T22:09:10" (ISO格式)
|
||||
* - "2025-07-26T22:09:10.000Z" (ISO完整格式)
|
||||
* - Date对象
|
||||
*/
|
||||
static parseTimestamp(timestamp: string | Date | number): number {
|
||||
if (typeof timestamp === 'number') {
|
||||
return timestamp
|
||||
}
|
||||
|
||||
if (timestamp instanceof Date) {
|
||||
return timestamp.getTime()
|
||||
}
|
||||
|
||||
if (typeof timestamp === 'string') {
|
||||
// 处理 "2025-07-26 22:09:10" 格式
|
||||
if (timestamp.includes(' ') && !timestamp.includes('T')) {
|
||||
const isoString = timestamp.replace(' ', 'T')
|
||||
return new Date(isoString).getTime()
|
||||
}
|
||||
// 处理其他格式
|
||||
return new Date(timestamp).getTime()
|
||||
}
|
||||
|
||||
// 默认返回当前时间
|
||||
return new Date().getTime()
|
||||
}
|
||||
|
||||
/**
|
||||
* 对消息进行排序和去重
|
||||
* 优先使用 messageOrder 排序,如果没有则使用时间戳排序
|
||||
* 确保消息按顺序排列,相同顺序的消息保持原有顺序
|
||||
*/
|
||||
static sortAndDeduplicateMessages(messages: ChatMessage[]): ChatMessage[] {
|
||||
console.log('🔄 开始排序和去重消息,原始数量:', messages.length)
|
||||
|
||||
// 创建消息ID的Set用于去重
|
||||
const seenIds = new Set<string>()
|
||||
const uniqueMessages: ChatMessage[] = []
|
||||
|
||||
// 先去重
|
||||
for (const msg of messages) {
|
||||
if (!seenIds.has(msg.id)) {
|
||||
seenIds.add(msg.id)
|
||||
uniqueMessages.push(msg)
|
||||
} else {
|
||||
console.warn('⚠️ 发现重复消息,ID:', msg.id)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('✅ 去重完成,去重后数量:', uniqueMessages.length)
|
||||
|
||||
// 按消息顺序排序(优先使用 messageOrder,如果没有则使用时间戳)
|
||||
uniqueMessages.sort((a, b) => {
|
||||
// 优先使用 messageOrder 排序
|
||||
if (a.messageOrder !== undefined && b.messageOrder !== undefined) {
|
||||
if (a.messageOrder !== b.messageOrder) {
|
||||
return a.messageOrder - b.messageOrder
|
||||
}
|
||||
}
|
||||
|
||||
// 如果 messageOrder 相同或不存在,使用时间戳排序
|
||||
const timeA = this.parseTimestamp(a.timestamp)
|
||||
const timeB = this.parseTimestamp(b.timestamp)
|
||||
|
||||
if (timeA !== timeB) {
|
||||
return timeA - timeB
|
||||
}
|
||||
|
||||
// 时间相同时,保持原有顺序(稳定排序)
|
||||
// 通过比较消息ID的字典序来保持一致性
|
||||
return a.id.localeCompare(b.id)
|
||||
})
|
||||
|
||||
console.log('✅ 排序完成,排序后消息:', uniqueMessages.map(m => ({
|
||||
id: m.id,
|
||||
type: m.type,
|
||||
order: m.messageOrder,
|
||||
time: m.timestamp,
|
||||
content: m.content.substring(0, 20) + '...'
|
||||
})))
|
||||
|
||||
return uniqueMessages
|
||||
}
|
||||
}
|
||||
|
||||
export default MessageService
|
||||
|
||||
Reference in New Issue
Block a user