Commit e019d3ac by Liu

fix:工具默认展示第0个,去掉制度活化硬性选中

parent eea71f34
...@@ -131,30 +131,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -131,30 +131,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
await waitForToken() await waitForToken()
// 从路由中获取 userRoles 参数 // 从路由中获取 userRoles 参数
const userRoles = getUserRolesForApi() const userRoles = getUserRolesForApi()
// 检查缓存(包含 userRoles 信息,有效期 5 分钟)
const cacheKey = `toolList_${JSON.stringify(userRoles)}`
const cached = sessionStorage.getItem(cacheKey)
if (cached) {
try {
const { toolList, timestamp } = JSON.parse(cached)
if (Date.now() - timestamp < 5 * 60 * 1000) {
setToolList(toolList)
return
}
}
catch {
// 缓存解析失败,继续调用接口
}
}
// 调用真实 API 获取工具列表 // 调用真实 API 获取工具列表
const res = await fetchToolList({ userRoles }) const res = await fetchToolList({ userRoles })
if (res?.data && Array.isArray(res.data) && res.data.length > 0) { if (res?.data && Array.isArray(res.data) && res.data.length > 0) {
setToolList(res.data) setToolList(res.data)
// 缓存工具列表(包含 userRoles 和时间戳) // 存入第0个工具信息到缓存
sessionStorage.setItem(cacheKey, JSON.stringify({ const defaultTool = res.data[0]
toolList: res.data, safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
timestamp: Date.now(), safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}))
} }
} }
catch (error) { catch (error) {
...@@ -175,6 +160,12 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -175,6 +160,12 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
} }
setSelectedToolId(null) setSelectedToolId(null)
setIsToolBtnActive(true) setIsToolBtnActive(true)
// 存入默认高亮按钮信息到缓存
if (toolList.length > 0) {
const defaultTool = toolList[0]
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
return return
} }
...@@ -184,6 +175,12 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -184,6 +175,12 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
setSearchParams(newSearchParams, { replace: true }) setSearchParams(newSearchParams, { replace: true })
setSelectedToolId(null) setSelectedToolId(null)
setIsToolBtnActive(true) setIsToolBtnActive(true)
// 存入默认高亮按钮信息到缓存
if (toolList.length > 0) {
const defaultTool = toolList[0]
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
return return
} }
...@@ -193,6 +190,14 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -193,6 +190,14 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
if (currentToolId && hasPersistentToolSource) { if (currentToolId && hasPersistentToolSource) {
setSelectedToolId(currentToolId) setSelectedToolId(currentToolId)
setIsToolBtnActive(false) setIsToolBtnActive(false)
// 存入当前选中工具信息到缓存
if (toolList.length > 0) {
const currentTool = toolList.find(tool => String(tool.toolId) === String(currentToolId))
if (currentTool) {
safeSessionStorageSetItem('currentToolId', currentTool.toolId)
safeSessionStorageSetItem('currentToolName', currentTool.toolName)
}
}
return return
} }
...@@ -206,12 +211,26 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -206,12 +211,26 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
if (toolIdFromUrl) { if (toolIdFromUrl) {
setSelectedToolId(toolIdFromUrl) setSelectedToolId(toolIdFromUrl)
setIsToolBtnActive(false) setIsToolBtnActive(false)
// 存入当前选中工具信息到缓存
if (toolList.length > 0) {
const currentTool = toolList.find(tool => String(tool.toolId) === String(toolIdFromUrl))
if (currentTool) {
safeSessionStorageSetItem('currentToolId', currentTool.toolId)
safeSessionStorageSetItem('currentToolName', currentTool.toolName)
}
}
} }
else { else {
setSelectedToolId(null) setSelectedToolId(null)
setIsToolBtnActive(true) setIsToolBtnActive(true)
// 存入默认高亮按钮信息到缓存
if (toolList.length > 0) {
const defaultTool = toolList[0]
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
} }
}, [currentToolId, sessionToolId, toolIdFromUrl, searchParams, setSearchParams, fromCollect]) }, [currentToolId, sessionToolId, toolIdFromUrl, searchParams, setSearchParams, fromCollect, toolList])
// 监听 sessionStorage 中的 currentToolId(历史点击时写入)来辅助高亮逻辑 // 监听 sessionStorage 中的 currentToolId(历史点击时写入)来辅助高亮逻辑
useEffect(() => { useEffect(() => {
...@@ -287,8 +306,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -287,8 +306,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
toolId = selectedToolId toolId = selectedToolId
} }
else { else {
// 制度活化:不传递 toolId // 制度活化:使用第0个工具的 toolId
toolId = undefined toolId = toolList[0]?.toolId
} }
onSubmit?.(content.trim(), toolId) onSubmit?.(content.trim(), toolId)
setContent('') setContent('')
...@@ -317,12 +336,14 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -317,12 +336,14 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
document.execCommand('insertText', false, text) document.execCommand('insertText', false, text)
} }
// 处理制度活化按钮点击:先创建新会话 // 处理默认工具按钮点击:先创建新会话
const handleGeneralClick = async () => { const handleGeneralClick = async () => {
if (!checkAuth()) if (!checkAuth())
return return
// 获取默认工具信息
const defaultTool = toolList.length > 0 ? toolList[0] : { toolId: '', toolName: '默认工具' }
// 通知上层开始 loading // 通知上层开始 loading
onToolClick?.(true, undefined, '制度活化', false, undefined, true) onToolClick?.(true, undefined, defaultTool.toolName, false, undefined, true)
try { try {
// 重置提交按钮状态 // 重置提交按钮状态
dispatch(setIsAsking(false)) dispatch(setIsAsking(false))
...@@ -332,11 +353,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -332,11 +353,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
setIsToolBtnActive(true) setIsToolBtnActive(true)
setSelectedToolId(null) setSelectedToolId(null)
safeSessionStorageRemoveItem('showToolQuestion') safeSessionStorageRemoveItem('showToolQuestion')
safeSessionStorageRemoveItem('currentToolId') // 存入默认高亮按钮信息到缓存
setSessionToolId(null) if (toolList.length > 0) {
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
setSessionToolId(defaultTool.toolId)
}
setShowToolQuestion(false) setShowToolQuestion(false)
// 先通知上层更新欢迎语(即便后续接口异常也能生效) // 先通知上层更新欢迎语(即便后续接口异常也能生效)
onToolClick?.(true, undefined, '制度活化', false) onToolClick?.(true, undefined, defaultTool.toolName, false)
// 清空路由中的 toolId 参数 // 清空路由中的 toolId 参数
if (toolIdFromUrl) { if (toolIdFromUrl) {
const newSearchParams = new URLSearchParams(searchParams) const newSearchParams = new URLSearchParams(searchParams)
...@@ -348,33 +373,33 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -348,33 +373,33 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 等待 token 就绪后再调用接口 // 等待 token 就绪后再调用接口
await waitForToken() await waitForToken()
const requestData = { const requestData = {
busiId: '', busiId: defaultTool.toolId,
} }
const res = await fetchSessionConversationId(requestData) const res = await fetchSessionConversationId(requestData)
if (res?.data?.conversationId) { if (res?.data?.conversationId) {
const conversationId = res.data.conversationId const conversationId = res.data.conversationId
// 获取到会话ID后,通知上层更新欢迎语并传递 conversationId // 获取到会话ID后,通知上层更新欢迎语并传递 conversationId
onToolClick?.(true, undefined, '制度活化', false, conversationId) onToolClick?.(true, undefined, defaultTool.toolName, false, conversationId)
// 在 navigate 之前设置标记,避免 Chat 组件的 useEffect 重复调用接口 // 在 navigate 之前设置标记,避免 Chat 组件的 useEffect 重复调用接口
sessionStorage.setItem('toolHistoryLoading', conversationId) sessionStorage.setItem('toolHistoryLoading', conversationId)
// 更新路由到新的会话ID // 更新路由到新的会话ID
navigate(`/chat/${conversationId}`, { navigate(`/chat/${conversationId}`, {
replace: true, replace: true,
state: { state: {
toolId: null, toolId: defaultTool.toolId,
skipHistoryLoad: true, // 标记跳过历史记录加载,因为 ChatEditor 会处理 skipHistoryLoad: true, // 标记跳过历史记录加载,因为 ChatEditor 会处理
}, },
}) })
// 使用获取到的会话ID调用历史会话 // 使用获取到的会话ID调用历史会话
await waitForToken() await waitForToken()
const qaRes = await fetchUserQaRecordPage(conversationId, '') const qaRes = await fetchUserQaRecordPage(conversationId, defaultTool.toolId)
console.log('qaRes chatEditor11111', qaRes) console.log('qaRes chatEditor11111', qaRes)
// 通过自定义事件将历史记录数据传递给父组件进行渲染 // 通过自定义事件将历史记录数据传递给父组件进行渲染
window.dispatchEvent(new CustomEvent('toolHistoryLoaded', { window.dispatchEvent(new CustomEvent('toolHistoryLoaded', {
detail: { detail: {
conversationId, conversationId,
toolId: '', toolId: defaultTool.toolId,
toolName: '制度活化', toolName: defaultTool.toolName,
qaRecords: qaRes?.data || [], qaRecords: qaRes?.data || [],
}, },
})) }))
...@@ -389,7 +414,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -389,7 +414,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
} }
finally { finally {
// 通知上层结束 loading // 通知上层结束 loading
onToolClick?.(true, undefined, '制度活化', false, undefined, false) onToolClick?.(true, undefined, defaultTool.toolName, false, undefined, false)
} }
} }
...@@ -411,7 +436,9 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -411,7 +436,9 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
dispatch(setCurrentToolId(tool.toolId)) dispatch(setCurrentToolId(tool.toolId))
setSelectedToolId(tool.toolId) setSelectedToolId(tool.toolId)
setIsToolBtnActive(false) setIsToolBtnActive(false)
// 存入当前选中工具信息到缓存
safeSessionStorageSetItem('currentToolId', tool.toolId) safeSessionStorageSetItem('currentToolId', tool.toolId)
safeSessionStorageSetItem('currentToolName', tool.toolName)
setSessionToolId(tool.toolId) setSessionToolId(tool.toolId)
// 先调用 fetchSessionConversationId 获取会话ID // 先调用 fetchSessionConversationId 获取会话ID
...@@ -611,9 +638,9 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -611,9 +638,9 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const toolIdStr = String(tool.toolId) const toolIdStr = String(tool.toolId)
const isSelectedByState = selectedToolId && toolIdStr === String(selectedToolId) const isSelectedByState = selectedToolId && toolIdStr === String(selectedToolId)
const isSelectedBySession = !selectedToolId && sessionToolId && toolIdStr === String(sessionToolId) const isSelectedBySession = !selectedToolId && sessionToolId && toolIdStr === String(sessionToolId)
// 制度活化高亮:路由内没有 toolId 或 toolId 为空时默认高亮,点击后也要高亮 // 默认高亮第0个工具:路由内没有 toolId 或 toolId 为空时默认高亮,点击后也要高亮
const isGeneralMode = tool.toolName === '制度活化' && isToolBtnActive && !selectedToolId && !sessionToolId && !toolIdFromUrl const isDefaultHighlight = index === 0 && isToolBtnActive && !selectedToolId && !sessionToolId && !toolIdFromUrl
const isSelected = isSelectedByState || isSelectedBySession || isGeneralMode const isSelected = isSelectedByState || isSelectedBySession || isDefaultHighlight
const baseBtnClass const baseBtnClass
= 'w-auto h-[32px] px-3 rounded-full shadow-none text-[12px] flex items-center gap-2 transition-all duration-200 border' = 'w-auto h-[32px] px-3 rounded-full shadow-none text-[12px] flex items-center gap-2 transition-all duration-200 border'
...@@ -627,7 +654,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -627,7 +654,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 高亮状态直接返回,避免重复触发 // 高亮状态直接返回,避免重复触发
if (isSelected) if (isSelected)
return return
if (tool.toolName === '制度活化') if (index === 0)
await handleGeneralClick() await handleGeneralClick()
else else
await handleToolClick(tool) await handleToolClick(tool)
......
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