bugfix: 处理流式接口token过期时的提示

This commit is contained in:
zhangyahui
2025-11-03 15:46:16 +08:00
parent af21c31e61
commit 5acd523d1b

View File

@@ -84,7 +84,7 @@ const handleSendMessage = (message: string): void => {
type: 'text',
content: message,
timestamp: new Date().toISOString(),
sessionId: (userInfoStore.state.userInfo as any).id,
sessionId: Math.floor(Date.now() / 1000).toString(),
self: true
}
messageList.value.push(userMessage)
@@ -95,9 +95,11 @@ const handleSendMessage = (message: string): void => {
const abort = new AbortController()
const handleFetchMessage = (message: string) => {
const sessionId = Math.floor(Date.now() / 1000).toString()
const params = {
message: message,
sessionId: (userInfoStore.state.userInfo as any).id,
sessionId: sessionId,
gender: 'male'
}
@@ -107,7 +109,7 @@ const handleFetchMessage = (message: string) => {
type: 'text',
content: '',
timestamp: new Date().toISOString(),
sessionId: (userInfoStore.state.userInfo as any).id
sessionId: sessionId
}
// 添加到消息列表
@@ -136,16 +138,51 @@ const handleFetchMessage = (message: string) => {
credentials: 'include'
})
.then(async (response) => {
console.log('response',response)
// 检查响应内容类型,判断是否为流式响应
const contentType = response.headers.get('content-type') || ''
const isStreamResponse = contentType.includes('text/event-stream') || contentType.includes('stream')
if (!response.ok) {
// 非流式错误响应,使用 text() 读取错误信息
const errorText = await response.text()
console.error('请求错误:', errorText)
showToast({
message: `failed to fetch: ${response.status}`,
position: 'top',
icon: 'none'
})
throw new Error(`发起对话错误--- ${response.status}`)
throw new Error(`发起对话错误--- ${response.status}: ${errorText}`)
}
// 不是流式响应,使用 text()读取错误信息
if (!isStreamResponse) {
const text = await response.text()
try {
const errorData = JSON.parse(text)
if (errorData.message || errorData.error) {
showToast({
message: errorData.message || errorData.error || '请求失败',
position: 'top',
icon: 'none'
})
}
} catch (e) {
// 如果不是 JSON直接显示文本内容
showToast({
message: text || '请求失败',
position: 'top',
icon: 'none'
})
throw new Error(text || '请求失败')
}
isStreaming.value = false
currentStreamingMessage.value = null
return
}
// 流式响应处理
let contentBody = ''
let buffer = ''
const reader = response.body?.getReader()