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
+45
View File
@@ -113,4 +113,49 @@ export const uploadFile = (file: File, onProgress?: (progress: number) => void):
}).then(res => res.data.data.url)
}
// 消息相关API
export const messageApi = {
// 获取用户消息分页
getUserMessages: (userId: string, current: number = 1, size: number = 20) =>
request.get(`/message/user/${userId}/page`, { params: { current, size } }),
// 搜索用户消息
searchUserMessages: (userId: string, keyword: string, limit: number = 50) =>
request.get(`/message/user/${userId}/search`, { params: { keyword, limit } }),
// 获取消息详情
getMessageById: (id: string) =>
request.get(`/message/${id}`)
}
// 情绪记录相关API
export const emotionRecordApi = {
// 获取用户情绪记录分页
getUserEmotionRecords: (userId: string, current: number = 1, size: number = 10) =>
request.get(`/emotion-records/user/${userId}`, { params: { current, size } }),
// 获取用户最近情绪记录
getUserRecentEmotionRecords: (userId: string, limit: number = 5) =>
request.get(`/emotion-records/user/${userId}/recent`, { params: { limit } }),
// 获取情绪记录详情
getEmotionRecordById: (id: string) =>
request.get(`/emotion-records/${id}`),
// 删除情绪记录
deleteEmotionRecord: (id: string) =>
request.delete(`/emotion-records/${id}`)
}
// 情绪总结相关API
export const emotionSummaryApi = {
// 生成情绪记录总结
generateEmotionSummary: (userId: string) =>
request.post(`/emotion-summary/generate/${userId}`),
// 获取情绪记录总结状态
getEmotionSummaryStatus: (userId: string) =>
request.get(`/emotion-summary/status/${userId}`)
}
export default api
+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)
+17 -43
View File
@@ -180,6 +180,7 @@
import { Empty, message } from 'ant-design-vue'
import { useDiaryStore } from '@/stores'
import { formatTime } from '@/utils'
import { emotionRecordApi } from '@/services/api'
import type { DiaryEntry } from '@/types'
const diaryStore = useDiaryStore()
@@ -282,36 +283,22 @@
// 获取当前用户ID(这里需要根据实际的用户管理方式获取)
const userId = 'default_user' // 这里应该从用户状态中获取
const response = await fetch(`/api/emotion-records/user/${userId}?current=${page}&size=${pagination.value.pageSize}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
const pageData = await emotionRecordApi.getUserEmotionRecords(userId, page, pagination.value.pageSize)
const result = await response.json()
if (result.success) {
const pageData = result.data
if (append) {
emotionRecords.value.push(...(pageData.records || []))
} else {
emotionRecords.value = pageData.records || []
}
pagination.value = {
current: pageData.current || 1,
pageSize: pageData.size || 10,
total: pageData.total || 0
}
console.log('情绪记录加载成功:', emotionRecords.value.length, '条')
if (append) {
emotionRecords.value.push(...(pageData.records || []))
} else {
console.error('加载情绪记录失败:', result.message)
message.error(result.message || '加载情绪记录失败')
emotionRecords.value = pageData.records || []
}
pagination.value = {
current: pageData.current || 1,
pageSize: pageData.size || 10,
total: pageData.total || 0
}
console.log('情绪记录加载成功:', emotionRecords.value.length, '条')
} catch (error) {
console.error('加载情绪记录时发生错误:', error)
message.error('加载情绪记录失败,请检查网络连接')
@@ -330,23 +317,10 @@
// 删除情绪记录
const deleteEmotionRecord = async (id: string) => {
try {
const response = await fetch(`/api/emotion-records/${id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
})
const result = await response.json()
if (result.success) {
message.success('情绪记录删除成功')
// 重新加载第一页
await loadEmotionRecords(1)
} else {
message.error(result.message || '删除失败')
}
await emotionRecordApi.deleteEmotionRecord(id)
message.success('情绪记录删除成功')
// 重新加载第一页
await loadEmotionRecords(1)
} catch (error) {
console.error('删除情绪记录时发生错误:', error)
message.error('删除失败,请重试')