Commit 1710e03d by Liu

fix:历史会话ID和常见问题传参逻辑

parent 8c98a5a1
......@@ -132,10 +132,24 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
search: location.search,
hideTools,
})
const userRoles = getUserRolesForApi()
const cacheKey = `toolList_${JSON.stringify(userRoles)}`
const cached = safeSessionStorageGetItem(cacheKey)
if (cached) {
try {
const { toolList: cachedToolList, timestamp } = JSON.parse(cached)
if (Date.now() - Number(timestamp || 0) < 5 * 60 * 1000 && Array.isArray(cachedToolList) && cachedToolList.length > 0) {
setToolList(cachedToolList)
return
}
}
catch {
// 缓存解析失败则继续走接口
}
}
// 等待 token 就绪后再调用接口
await waitForToken()
// 从路由中获取 userRoles 参数
const userRoles = getUserRolesForApi()
console.log('[get_tool_list][ChatEditor] request', { userRoles })
// 调用真实 API 获取工具列表
......@@ -146,6 +160,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
})
if (res?.data && Array.isArray(res.data) && res.data.length > 0) {
setToolList(res.data)
safeSessionStorageSetItem(cacheKey, JSON.stringify({ toolList: res.data, timestamp: Date.now() }))
// 仅在“没有任何持久来源”时,才写入默认工具,避免覆盖已有的会话 toolId
const storedToolId = safeSessionStorageGetItem('currentToolId')
const hasAnyToolSource = Boolean(storedToolId || currentToolId || toolIdFromUrl)
......
......@@ -9,12 +9,12 @@ import HomeIcon2 from '@/assets/homeIcon2.png'
import SmartIce from '@/assets/smart-ice.png'
import { fetchConversations, setCurrentConversation, setCurrentToolId, setNavigationFlag } from '@/store/conversationSlice'
import { useAppDispatch, useAppSelector } from '@/store/hook'
import { fetchEfficiencyQuestionList } from '@/api/home'
import { fetchEfficiencyQuestionList, fetchToolList } from '@/api/home'
import SdreamLoading from '@/components/SdreamLoading'
import { fetchLoginByToken, fetchLoginByUid } from '@/api/common'
import { fetchSessionConversationId } from '@/api/conversation'
import { fetchCheckTokenApi } from '@/api/chat'
import { getUserRolesFromRoute, safeLocalStorageGetItem, safeLocalStorageSetItem, safeSessionStorageGetItem, safeSessionStorageSetItem, waitForToken } from '@/lib/utils'
import { getUserRolesForApi, getUserRolesFromRoute, safeLocalStorageGetItem, safeLocalStorageSetItem, safeSessionStorageGetItem, safeSessionStorageSetItem, waitForToken } from '@/lib/utils'
// 从 localStorage 获取 token 的辅助函数
function getTokenFromStorage(): string | null {
......@@ -86,6 +86,49 @@ export const Home: React.FC = () => {
defaultValue: '',
})
const ensureToolIdReady = useCallback(async (): Promise<string> => {
const storedToolId = safeSessionStorageGetItem('currentToolId') || ''
if (storedToolId) {
return storedToolId
}
const userRoles = getUserRolesForApi()
const cacheKey = `toolList_${JSON.stringify(userRoles)}`
let toolList: any[] = []
const cached = safeSessionStorageGetItem(cacheKey)
if (cached) {
try {
const parsed = JSON.parse(cached)
if (Date.now() - Number(parsed?.timestamp || 0) < 5 * 60 * 1000 && Array.isArray(parsed?.toolList)) {
toolList = parsed.toolList
}
}
catch {
toolList = []
}
}
if (toolList.length === 0) {
const res = await fetchToolList({ userRoles })
if (Array.isArray(res?.data)) {
toolList = res.data
safeSessionStorageSetItem(cacheKey, JSON.stringify({ toolList, timestamp: Date.now() }))
}
}
const defaultTool = toolList[0]
const defaultToolId = String(defaultTool?.toolId || '')
if (defaultToolId) {
safeSessionStorageSetItem('currentToolId', defaultToolId)
if (defaultTool?.toolName) {
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
dispatch(setCurrentToolId(defaultToolId))
}
return defaultToolId
}, [dispatch])
// 根据 toolId 获取对应的 recordType
const getRecordTypeByToolId = (toolId: string | null | undefined): string => {
if (!toolId) {
......@@ -109,8 +152,12 @@ export const Home: React.FC = () => {
try {
// 等待 token 就绪后再调用接口
await waitForToken()
// 仅透传业务方附加参数,busiId/toolId 统一在接口层按 sessionStorage 实时解析
const res = await fetchSessionConversationId(data || {})
// 首次进入时先确保有默认 toolId,再调用依赖 toolId 的业务接口
const toolId = await ensureToolIdReady()
const res = await fetchSessionConversationId({
...(data || {}),
...(toolId ? { toolId, busiId: toolId } : {}),
})
if (res?.data?.conversationId) {
const conversationId = res.data.conversationId
dispatch(setCurrentConversation(conversationId))
......
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