Commit 584fe21f by Liu

fix:新建会话时机

parent fb2b017f
...@@ -7,12 +7,13 @@ import styles from './Home.module.less' ...@@ -7,12 +7,13 @@ import styles from './Home.module.less'
import { QuestionList } from './components/QuestionList' import { QuestionList } from './components/QuestionList'
import HomeIcon2 from '@/assets/homeIcon2.png' import HomeIcon2 from '@/assets/homeIcon2.png'
import SmartIce from '@/assets/smart-ice.png' import SmartIce from '@/assets/smart-ice.png'
import { clearCurrentToolId, createConversation, fetchConversations, setCurrentToolId } from '@/store/conversationSlice' import { clearCurrentToolId, createConversation, fetchConversations, setCurrentConversation, setCurrentToolId, setNavigationFlag } from '@/store/conversationSlice'
import { useAppDispatch } from '@/store/hook' import { useAppDispatch } from '@/store/hook'
import { fetchEfficiencyQuestionList } from '@/api/home' import { fetchEfficiencyQuestionList } from '@/api/home'
import SdreamLoading from '@/components/SdreamLoading' import SdreamLoading from '@/components/SdreamLoading'
import { fetchLoginByToken, fetchLoginByUid } from '@/api/common' import { fetchLoginByToken, fetchLoginByUid } from '@/api/common'
import { getUserRolesFromRoute, safeSessionStorageGetItem, safeSessionStorageRemoveItem } from '@/lib/utils' import { getUserRolesFromRoute, safeLocalStorageGetItem, safeSessionStorageGetItem, safeSessionStorageRemoveItem, safeSessionStorageSetItem } from '@/lib/utils'
import type { Conversation } from '@/types/conversation'
function getAnimationProps(delay: number) { function getAnimationProps(delay: number) {
return { return {
...@@ -58,10 +59,38 @@ export const Home: React.FC = () => { ...@@ -58,10 +59,38 @@ export const Home: React.FC = () => {
defaultValue: '', defaultValue: '',
}) })
const initConversation = () => { const initConversation = async () => {
const fromCollect = location.state?.fromCollect const fromCollect = location.state?.fromCollect
// 只有在访问首页时才创建新对话,如果已经在聊天页面则不创建 // 只有在访问首页时才处理会话,如果已经在聊天页面则不处理
if (!fromCollect && (location.pathname === '/' || location.pathname === '/home')) { if (!fromCollect && (location.pathname === '/' || location.pathname === '/home')) {
// 从 localStorage 读取上次的 conversationId
const savedConversationId = safeLocalStorageGetItem('currentConversationId')
if (savedConversationId) {
// 如果 localStorage 中有 conversationId,恢复它而不是创建新会话
dispatch(setCurrentConversation(savedConversationId))
// 设置导航标志,触发自动导航到聊天页面
dispatch(setNavigationFlag(true))
// 获取会话列表(用于后续验证和同步 toolId)
const fetchResult = await dispatch(fetchConversations())
const conversationsList = fetchResult.payload as Conversation[]
// 同步会话的 toolId(如果存在)
const restoredConversation = conversationsList?.find((conv: Conversation) => conv.conversationId === savedConversationId)
if (restoredConversation?.toolId) {
dispatch(setCurrentToolId(restoredConversation.toolId))
// 同步到 sessionStorage,确保 ChatEditor 等组件能正确识别 toolId
safeSessionStorageSetItem('currentToolId', restoredConversation.toolId)
}
else {
dispatch(clearCurrentToolId())
// 清除 sessionStorage 中的 toolId
safeSessionStorageRemoveItem('currentToolId')
}
}
else {
// 如果 localStorage 中没有 conversationId,创建新会话
dispatch(fetchConversations())
dispatch( dispatch(
createConversation({ createConversation({
conversationData: {}, conversationData: {},
...@@ -70,6 +99,7 @@ export const Home: React.FC = () => { ...@@ -70,6 +99,7 @@ export const Home: React.FC = () => {
}), }),
) )
} }
}
// 清除状态以避免下次影响 // 清除状态以避免下次影响
if (location.state?.fromCollect) { if (location.state?.fromCollect) {
// 使用 replace 替换当前历史记录,清除 state // 使用 replace 替换当前历史记录,清除 state
......
...@@ -5,6 +5,7 @@ import { clearCurrentTacticsConversation, setCurrentTacticsConversation } from ' ...@@ -5,6 +5,7 @@ import { clearCurrentTacticsConversation, setCurrentTacticsConversation } from '
import { useAppDispatch, useAppSelector } from '@/store/hook' import { useAppDispatch, useAppSelector } from '@/store/hook'
import { setIsAsking } from '@/store/chatSlice' import { setIsAsking } from '@/store/chatSlice'
import type { RootState } from '@/store' import type { RootState } from '@/store'
import { safeLocalStorageGetItem } from '@/lib/utils'
export function withRouteChangeHandler(WrappedComponent: React.ComponentType) { export function withRouteChangeHandler(WrappedComponent: React.ComponentType) {
let beforeLocationPathName = '' let beforeLocationPathName = ''
...@@ -35,8 +36,13 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) { ...@@ -35,8 +36,13 @@ export function withRouteChangeHandler(WrappedComponent: React.ComponentType) {
} }
if (location.pathname === '/') { if (location.pathname === '/') {
// 如果 localStorage 中有 conversationId,不清除,让恢复逻辑处理
// 这样可以保留上一次的历史记录
const savedConversationId = safeLocalStorageGetItem('currentConversationId')
if (!savedConversationId) {
dispatch(clearCurrentConversation()) dispatch(clearCurrentConversation())
} }
}
else if (location.pathname.startsWith('/chat/')) { else if (location.pathname.startsWith('/chat/')) {
const conversationId = location.pathname.split('/')[2] const conversationId = location.pathname.split('/')[2]
......
...@@ -100,6 +100,9 @@ const conversationSlice = createSlice({ ...@@ -100,6 +100,9 @@ const conversationSlice = createSlice({
clearNavigationFlag: (state) => { clearNavigationFlag: (state) => {
state.shouldNavigateToNewConversation = false state.shouldNavigateToNewConversation = false
}, },
setNavigationFlag: (state, action: PayloadAction<boolean>) => {
state.shouldNavigateToNewConversation = action.payload
},
setCurrentToolId: (state, action: PayloadAction<string | undefined>) => { setCurrentToolId: (state, action: PayloadAction<string | undefined>) => {
state.currentToolId = action.payload state.currentToolId = action.payload
}, },
...@@ -151,6 +154,6 @@ const conversationSlice = createSlice({ ...@@ -151,6 +154,6 @@ const conversationSlice = createSlice({
}, },
}) })
export const { setCurrentConversation, clearCurrentConversation, clearNavigationFlag, clearShouldSendQuestion, setShouldSendQuestion, setCurrentToolId, clearCurrentToolId } = conversationSlice.actions export const { setCurrentConversation, clearCurrentConversation, clearNavigationFlag, setNavigationFlag, clearShouldSendQuestion, setShouldSendQuestion, setCurrentToolId, clearCurrentToolId } = conversationSlice.actions
export default conversationSlice.reducer export default conversationSlice.reducer
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