优化处理

This commit is contained in:
2025-10-26 16:59:50 +08:00
parent fdac026720
commit 2e243c7671
45 changed files with 346 additions and 3757 deletions
+20 -19
View File
@@ -52,7 +52,8 @@ export class ChatApiService {
async createSession(userId: string, title?: string): Promise<ChatSession> {
try {
console.log('📝 创建会话API调用:', { userId, title })
const response = await http.post<ConversationResponse>('/conversation', {
// backend-single: POST /conversation/create
const response = await http.post<ConversationResponse>('/conversation/create', {
userId,
title: title || `对话${Date.now()}`
})
@@ -88,25 +89,23 @@ export class ChatApiService {
async getUserSessions(userId: string): Promise<ChatSession[]> {
try {
console.log('📂 获取用户会话API调用:', { userId })
const response = await http.get<ConversationResponse[]>(`/conversation/user/${userId}`)
// backend-single: GET /conversation/page?userId=xxx
const response = await http.get<any>('/conversation/page', { params: { userId, current: 1, size: 100 } })
console.log('📂 获取用户会话API响应:', response)
// 处理HTTP响应的data字段
const data = (response as any).data || response
// 处理HTTP响应的data字段PageResult
const pageData = (response as any).data || response
const records = pageData.records || []
// 后端返回ConversationResponse数组,需要转换为ChatSession格式
if (Array.isArray(data)) {
return data.map((conv: any) => ({
id: conv.id,
title: conv.title,
userId: conv.userId || conv.user_id, // 兼容不同的字段名
createTime: conv.createTime || conv.create_time,
updateTime: conv.updateTime || conv.update_time,
messageCount: conv.messageCount || conv.message_count || 0
}))
}
return []
// 转换为ChatSession数组
return records.map((conv: any) => ({
id: conv.id,
title: conv.title,
userId: conv.userId || conv.user_id,
createTime: conv.createTime || conv.create_time,
updateTime: conv.updateTime || conv.update_time,
messageCount: conv.messageCount || conv.message_count || 0
}))
} catch (error) {
console.error('❌ 获取用户会话失败:', error)
return []
@@ -133,7 +132,8 @@ export class ChatApiService {
async deleteSession(sessionId: string): Promise<void> {
try {
console.log('🗑️ 删除会话API调用:', { sessionId })
await http.delete(`/conversation/${sessionId}`)
// backend-single: DELETE /conversation/delete?id=xxx
await http.delete('/conversation/delete', { params: { id: sessionId } })
console.log('✅ 删除会话成功')
} catch (error) {
console.error('❌ 删除会话失败:', error)
@@ -147,7 +147,8 @@ export class ChatApiService {
async updateSessionTitle(sessionId: string, title: string): Promise<void> {
try {
console.log('✏️ 更新会话标题API调用:', { sessionId, title })
await http.put(`/conversation/${sessionId}`, { title })
// backend-single: PUT /conversation/update 传id和title
await http.put('/conversation/update', { id: sessionId, title })
console.log('✅ 更新会话标题成功')
} catch (error) {
console.error('❌ 更新会话标题失败:', error)
+18 -9
View File
@@ -57,7 +57,8 @@ export const messageApi = {
// 获取用户消息分页
getUserMessages: async (current: number = 1, size: number = 20) => {
console.log('📨 调用getUserMessages API:', { current, size })
const response = await http.get(`/message/user/page`, { params: { current, size } })
// backend-single: GET /message/page (后端根据token识别用户)
const response = await http.get(`/message/page`, { params: { current, size } })
console.log('📨 getUserMessages API响应:', response)
return response
},
@@ -65,23 +66,31 @@ export const messageApi = {
// 搜索用户消息
searchUserMessages: async (keyword: string, limit: number = 50) => {
console.log('🔍 调用searchUserMessages API:', { keyword, limit })
const response = await http.post(`/message/user/search`, { keyword, limit })
console.log('🔍 searchUserMessages API响应:', response)
return response
// backend-single: POST /message/search
const resp = await http.post(`/message/search`, { keyword, limit })
console.log('🔍 searchUserMessages API响应:', resp)
// 统一返回数组,兼容控制器返回 PageResult 结构
const data: any = (resp as any).data || resp
const records = data.records || data
return Array.isArray(records) ? records : []
},
// 获取用户最近的聊天记录 - 修复:使用POST请求匹配后端接口
// 获取用户最近的聊天记录 - 返回数组,兼容后端 PageResult 结构
getRecentMessages: async (limit: number = 10) => {
console.log('📝 调用getRecentMessages API:', { limit })
const response = await http.post(`/message/user/recent`, { limit })
console.log('📝 getRecentMessages API响应:', response)
return response
// backend-single: POST /message/recent
const resp = await http.post(`/message/recent`, { limit })
console.log('📝 getRecentMessages API响应:', resp)
const data: any = (resp as any).data || resp
const records = data.records || data
return Array.isArray(records) ? records : []
},
// 获取消息详情
getMessageById: async (id: string) => {
console.log('📄 调用getMessageById API:', { id })
const response = await http.get(`/message/${id}`)
// backend-single: GET /message/detail?id=xxx
const response = await http.get(`/message/detail`, { params: { id } })
console.log('📄 getMessageById API响应:', response)
return response
}
+3 -3
View File
@@ -207,9 +207,9 @@ export class StompWebSocketService {
console.log('📤 准备发送的聊天请求:', chatRequest)
try {
// 发送到后端的/app/chat.send端点
// 发送到后端的/app/chat/send端点(对应 @MessageMapping("/chat") + @MessageMapping("/send")
this.client.publish({
destination: '/app/chat.send',
destination: '/app/chat/send',
body: JSON.stringify(chatRequest)
})
console.log('✅ STOMP聊天消息发送成功:', chatRequest)
@@ -324,7 +324,7 @@ export class StompWebSocketService {
try {
this.client.publish({
destination: '/app/chat.connect',
destination: '/app/chat/connect',
body: JSON.stringify(connectRequest)
})
console.log('✅ STOMP连接消息发送成功:', connectRequest)