Commit 49f99eb4 by Liu

feat: 清除记录接口及参数

parent 28c2d926
...@@ -25,8 +25,9 @@ export function fetchTacticsQaRecordPage(conversationId: string) { ...@@ -25,8 +25,9 @@ export function fetchTacticsQaRecordPage(conversationId: string) {
/** /**
* 删除问答功能会话 * 删除问答功能会话
*/ */
export function fetchDeleteTacticsConversation(conversationIdList: string[]) { export function fetchDeleteTacticsConversation(conversationId: string, recordIdList: string[]) {
return http.post('/conversation/api/conversation/mobile/v1/delete_user_conversation', { return http.post('/conversation/api/conversation/mobile/v1/delete_user_qa', {
conversationIdList, conversationId,
recordIdList,
}) })
} }
...@@ -11,7 +11,7 @@ import { ChatAnswerBox } from './components/ChatItem/ChatAnswerBox' ...@@ -11,7 +11,7 @@ import { ChatAnswerBox } from './components/ChatItem/ChatAnswerBox'
import { trimSpacesOnly } from './components/ChatItem/markdownFormatter' import { trimSpacesOnly } from './components/ChatItem/markdownFormatter'
import { ChatEditor } from '@/components/ChatEditor' import { ChatEditor } from '@/components/ChatEditor'
import type { ChatRecord } from '@/types/chat' import type { ChatRecord } from '@/types/chat'
import { fetchUserQaRecordPage } from '@/api/conversation' import { fetchSessionConversationId, fetchUserQaRecordPage } from '@/api/conversation'
import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat' import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat'
import { fetchToolList } from '@/api/home' import { fetchToolList } from '@/api/home'
import { clearCurrentToolId, clearShouldSendQuestion, fetchConversations, setCurrentToolId } from '@/store/conversationSlice' import { clearCurrentToolId, clearShouldSendQuestion, fetchConversations, setCurrentToolId } from '@/store/conversationSlice'
...@@ -49,6 +49,12 @@ export const Chat: React.FC = () => { ...@@ -49,6 +49,12 @@ export const Chat: React.FC = () => {
const toolIdFromStateRef = useRef<string | null | undefined>(undefined) const toolIdFromStateRef = useRef<string | null | undefined>(undefined)
// 标记是否已通过 toolHistoryLoaded 事件加载数据,避免重复调用接口 // 标记是否已通过 toolHistoryLoaded 事件加载数据,避免重复调用接口
const toolHistoryLoadedRef = useRef<{ conversationId: string | null }>({ conversationId: null }) const toolHistoryLoadedRef = useRef<{ conversationId: string | null }>({ conversationId: null })
// 标记是否已处理从收藏返回的逻辑,避免重复调用
const fromCollectProcessedRef = useRef<boolean>(false)
// 保存从收藏返回的标记,避免 location.state 被清除后丢失
const fromCollectRef = useRef<boolean>(false)
// 记录正在处理的 conversationId,避免重复调用
const processingConversationIdRef = useRef<string | null>(null)
// 当外部系统直接以 /chat/:id 链接进入(没有 location.state,且 URL 中也没有 toolId)时, // 当外部系统直接以 /chat/:id 链接进入(没有 location.state,且 URL 中也没有 toolId)时,
// 视为一次新的会话入口:重置为制度活化,清除历史遗留的工具模式状态 // 视为一次新的会话入口:重置为制度活化,清除历史遗留的工具模式状态
...@@ -294,10 +300,39 @@ export const Chat: React.FC = () => { ...@@ -294,10 +300,39 @@ export const Chat: React.FC = () => {
/** 获取qa记录 */ /** 获取qa记录 */
const getUserQaRecordPage = useCallback(async (conversationId: string, toolId?: string) => { const getUserQaRecordPage = useCallback(async (conversationId: string, toolId?: string) => {
// 检查是否正在处理相同的 conversationId,避免重复调用
if (processingConversationIdRef.current === conversationId) {
console.log('[Chat] 正在处理相同的 conversationId,跳过重复调用:', conversationId)
return
}
setIsLoading(true) setIsLoading(true)
// 标记正在处理
processingConversationIdRef.current = conversationId
try { try {
// 检测是否从收藏页返回 // 检测是否从收藏页返回(优先使用 ref 中保存的值,避免 location.state 被清除后丢失)
const fromCollect = location.state?.fromCollect const fromCollect = fromCollectRef.current || Boolean(location.state?.fromCollect)
let finalConversationId = conversationId
// 如果从收藏页返回,先调用 get_user_conversation_session 获取新的会话ID
if (fromCollect && !fromCollectProcessedRef.current) {
fromCollectProcessedRef.current = true
const sessionToolId = safeSessionStorageGetItem('currentToolId') || ''
try {
await waitForToken()
const sessionRes = await fetchSessionConversationId({ busiId: sessionToolId })
if (sessionRes?.data?.conversationId) {
finalConversationId = sessionRes.data.conversationId
console.log('[Chat] 从收藏返回 - 获取到新会话ID:', finalConversationId)
// 如果获取到新会话ID,设置标记避免路由变化时重复调用
toolHistoryLoadedRef.current.conversationId = finalConversationId
}
}
catch (error) {
console.error('[Chat] 从收藏返回 - 获取会话ID失败:', error)
// 如果获取失败,继续使用原来的 conversationId
}
}
// 如果从收藏页返回,触发事件通知 HomeNew 刷新问题列表 // 如果从收藏页返回,触发事件通知 HomeNew 刷新问题列表
if (fromCollect) { if (fromCollect) {
...@@ -315,7 +350,7 @@ export const Chat: React.FC = () => { ...@@ -315,7 +350,7 @@ export const Chat: React.FC = () => {
}, 100) }, 100)
} }
await waitForToken() await waitForToken()
const res = await fetchUserQaRecordPage(conversationId, toolId || '') const res = await fetchUserQaRecordPage(finalConversationId, toolId || safeSessionStorageGetItem('currentToolId') || '')
console.log('qaRes chatEditor2222222', res) console.log('qaRes chatEditor2222222', res)
const qaRecords = res.data || [] const qaRecords = res.data || []
const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(qaRecords)] const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(qaRecords)]
...@@ -341,7 +376,7 @@ export const Chat: React.FC = () => { ...@@ -341,7 +376,7 @@ export const Chat: React.FC = () => {
const hasQaRecords = qaRecords.length > 0 const hasQaRecords = qaRecords.length > 0
// 如果 qaRecords 中没有 toolId,尝试从 conversations 中查找当前会话的 toolId(会话级别) // 如果 qaRecords 中没有 toolId,尝试从 conversations 中查找当前会话的 toolId(会话级别)
const conversationToolId = latestToolId || (conversations.find(conv => conv.conversationId === conversationId)?.toolId?.trim?.()) const conversationToolId = latestToolId || (conversations.find(conv => conv.conversationId === finalConversationId)?.toolId?.trim?.())
// 优先使用从 location.state 传递的 toolId(如果存在),这是从历史记录点击传递过来的 // 优先使用从 location.state 传递的 toolId(如果存在),这是从历史记录点击传递过来的
const toolIdFromState = toolIdFromStateRef.current !== undefined const toolIdFromState = toolIdFromStateRef.current !== undefined
...@@ -417,6 +452,10 @@ export const Chat: React.FC = () => { ...@@ -417,6 +452,10 @@ export const Chat: React.FC = () => {
} }
finally { finally {
setIsLoading(false) setIsLoading(false)
// 清除处理标记
if (processingConversationIdRef.current === conversationId) {
processingConversationIdRef.current = null
}
} }
}, [dispatch, currentToolId, conversations, location.state]) }, [dispatch, currentToolId, conversations, location.state])
...@@ -445,13 +484,19 @@ export const Chat: React.FC = () => { ...@@ -445,13 +484,19 @@ export const Chat: React.FC = () => {
currentIdRef.current = id currentIdRef.current = id
lastSentQuestionRef.current = '' // 重置标记 lastSentQuestionRef.current = '' // 重置标记
// 保存 fromCollect 标记到 ref,避免 location.state 被清除后丢失
fromCollectRef.current = Boolean(location.state?.fromCollect)
// 重置 fromCollect 处理标记,确保每次路由变化时都能正确处理
fromCollectProcessedRef.current = false
// 清除处理标记,允许新的 conversationId 处理
processingConversationIdRef.current = null
// 等待 token 就绪后再调用接口 // 等待 token 就绪后再调用接口
waitForToken().then(() => { waitForToken().then(() => {
getUserQaRecordPage(id) getUserQaRecordPage(id)
}) })
} }
}, [id]) }, [id, location.state])
// 处理shouldSendQuestion的变化 - 自动发送问题 // 处理shouldSendQuestion的变化 - 自动发送问题
useEffect(() => { useEffect(() => {
......
...@@ -14,9 +14,9 @@ import DeleteIcon from '@/assets/svg/delete.svg?react' ...@@ -14,9 +14,9 @@ import DeleteIcon from '@/assets/svg/delete.svg?react'
import RefreshIcon from '@/assets/svg/refresh.svg?react' import RefreshIcon from '@/assets/svg/refresh.svg?react'
import { TacticsChatEditor } from '@/components/TacticsChatEditor' import { TacticsChatEditor } from '@/components/TacticsChatEditor'
import type { ChatRecord } from '@/types/chat' import type { ChatRecord } from '@/types/chat'
import { fetchTacticsQaRecordPage } from '@/api/tactics' import { fetchDeleteTacticsConversation, fetchTacticsQaRecordPage } from '@/api/tactics'
import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat' import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat'
import { clearTacticsNavigationFlag, clearTacticsShouldSendQuestion, createTacticsConversation, deleteTacticsConversations, fetchTacticsConversations } from '@/store/tacticsSlice' import { clearTacticsNavigationFlag, clearTacticsShouldSendQuestion, createTacticsConversation, fetchTacticsConversations } from '@/store/tacticsSlice'
import type { RootState } from '@/store' import type { RootState } from '@/store'
import { useAppDispatch, useAppSelector } from '@/store/hook' import { useAppDispatch, useAppSelector } from '@/store/hook'
import ScrollBtoIcon from '@/assets/svg/scrollBto.svg?react' import ScrollBtoIcon from '@/assets/svg/scrollBto.svg?react'
...@@ -616,7 +616,26 @@ export const TacticsChat: React.FC = () => { ...@@ -616,7 +616,26 @@ export const TacticsChat: React.FC = () => {
return return
setShowClearConfirm(false) setShowClearConfirm(false)
try { try {
await dispatch(deleteTacticsConversations([currentIdRef.current])).unwrap() await waitForToken()
// 1. 获取问答记录
const res = await fetchTacticsQaRecordPage(currentIdRef.current)
const qaRecords = res.data || []
// 2. 提取所有 recordId
const recordIdList: string[] = []
qaRecords.forEach((record) => {
if (record.answerList && record.answerList.length > 0) {
record.answerList.forEach((answer) => {
if (answer.recordId) {
recordIdList.push(answer.recordId)
}
})
}
})
// 3. 调用删除接口
await fetchDeleteTacticsConversation(currentIdRef.current, recordIdList)
// 停止正在进行的请求 // 停止正在进行的请求
if (abortControllerRef.current) { if (abortControllerRef.current) {
abortControllerRef.current.abort() abortControllerRef.current.abort()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment