fix: 修复API认证问题,统一使用request工具

- 修复JWT拦截器配置,添加情绪记录和消息API到公开接口列表
- 统一前端API调用,使用services/api.ts中的request工具替代直接fetch
- 确保所有API请求都能正确携带认证token
- 修复401未授权错误问题
This commit is contained in:
2025-07-25 05:55:55 +08:00
parent 86c2df4784
commit c09cbc3f01
6 changed files with 88 additions and 394 deletions
+22 -59
View File
@@ -287,6 +287,7 @@
} from '@ant-design/icons-vue'
import { useChatStore } from '@/stores'
import { formatTime } from '@/utils'
import { messageApi, emotionSummaryApi } from '@/services/api'
import type { Dayjs } from 'dayjs'
const chatStore = useChatStore()
@@ -391,27 +392,14 @@
const userId = chatStore.currentSession?.userId || 'default_user'
// 调用后端API生成情绪记录
const response = await fetch(`/api/emotion-summary/generate/${userId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})
const result = await emotionSummaryApi.generateEmotionSummary(userId)
const result = await response.json()
// 显示成功消息
const emotionRecord = result.emotionRecord
const summary = result.summary
if (result.success) {
// 显示成功消息
const emotionRecord = result.data.emotionRecord
const summary = result.data.summary
// 可以显示一个模态框或通知来展示情绪记录结果
showEmotionSummaryResult(result.data)
} else {
console.error('生成情绪记录失败:', result.message)
// 显示错误提示
alert(result.message || '生成情绪记录失败,请稍后再试')
}
// 可以显示一个模态框或通知来展示情绪记录结果
showEmotionSummaryResult(result)
} catch (error) {
console.error('生成情绪记录时发生错误:', error)
@@ -461,35 +449,22 @@
// 获取当前用户ID(这里需要根据实际的用户管理方式获取)
const userId = chatStore.currentSession?.userId || 'default_user'
const response = await fetch(`/message/user/${userId}/page?current=${page}&size=${historyPagination.value.pageSize}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
const pageData = await messageApi.getUserMessages(userId, page, historyPagination.value.pageSize)
const result = await response.json()
if (result.success) {
const pageData = result.data
if (page === 1) {
historyMessages.value = pageData.records || []
} else {
historyMessages.value.push(...(pageData.records || []))
}
historyPagination.value = {
current: pageData.current || 1,
pageSize: pageData.size || 20,
total: pageData.total || 0
}
console.log('历史记录加载成功:', historyMessages.value.length, '条')
if (page === 1) {
historyMessages.value = pageData.records || []
} else {
console.error('加载历史记录失败:', result.message)
historyMessages.value.push(...(pageData.records || []))
}
historyPagination.value = {
current: pageData.current || 1,
pageSize: pageData.size || 20,
total: pageData.total || 0
}
console.log('历史记录加载成功:', historyMessages.value.length, '条')
} catch (error) {
console.error('加载历史记录时发生错误:', error)
} finally {
@@ -509,21 +484,9 @@
const userId = chatStore.currentSession?.userId || 'default_user'
const response = await fetch(`/message/user/${userId}/search?keyword=${encodeURIComponent(searchKeyword.value)}&limit=100`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
const result = await response.json()
if (result.success) {
historyMessages.value = result.data || []
console.log('搜索历史记录成功:', historyMessages.value.length, '条')
} else {
console.error('搜索历史记录失败:', result.message)
}
const messages = await messageApi.searchUserMessages(userId, searchKeyword.value, 100)
historyMessages.value = messages || []
console.log('搜索历史记录成功:', historyMessages.value.length, '条')
} catch (error) {
console.error('搜索历史记录时发生错误:', error)