Commit 7a18d17c by Liu

fix:登录成功后再调用业务逻辑

parent 984a54ba
......@@ -7,15 +7,14 @@ import styles from './Home.module.less'
import { QuestionList } from './components/QuestionList'
import HomeIcon2 from '@/assets/homeIcon2.png'
import SmartIce from '@/assets/smart-ice.png'
import { clearCurrentToolId, createConversation, fetchConversations, setCurrentConversation, setCurrentToolId, setNavigationFlag } from '@/store/conversationSlice'
import { clearCurrentToolId, fetchConversations, setCurrentConversation, setCurrentToolId, setNavigationFlag } from '@/store/conversationSlice'
import { useAppDispatch } from '@/store/hook'
import { fetchEfficiencyQuestionList } 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, safeSessionStorageGetItem, safeSessionStorageRemoveItem, safeSessionStorageSetItem } from '@/lib/utils'
import type { Conversation } from '@/types/conversation'
import { getUserRolesFromRoute, safeLocalStorageGetItem, safeSessionStorageGetItem, safeSessionStorageRemoveItem } from '@/lib/utils'
// 从 localStorage 获取 token 的辅助函数
function getTokenFromStorage(): string | null {
......@@ -60,8 +59,7 @@ function getAnimationProps(delay: number) {
}
}
// 使用 sessionStorage 作为标志,防止跨组件重复执行 SSO 登录
const SSO_LOGIN_FLAG = 'sso_login_fetched'
// 已移除 sessionStorage 标志,改为仅使用组件级保护,确保每次打开链接都能调用 SSO 登录
export const Home: React.FC = () => {
const viteOutputObj = import.meta.env.VITE_OUTPUT_OBJ || 'open'
......@@ -86,54 +84,6 @@ export const Home: React.FC = () => {
defaultValue: '',
})
const _initConversation = async () => {
const fromCollect = location.state?.fromCollect
// 只有在访问首页时才处理会话,如果已经在聊天页面则不处理
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(
createConversation({
conversationData: {},
shouldNavigate: true,
shouldSendQuestion: '',
}),
)
}
}
// 清除状态以避免下次影响
if (location.state?.fromCollect) {
// 使用 replace 替换当前历史记录,清除 state
window.history.replaceState({}, document.title, window.location.pathname)
}
}
// 获取会话ID并加载历史记录
const getSessionConversationId = async (data?: any) => {
try {
......@@ -266,12 +216,7 @@ export const Home: React.FC = () => {
}, [_handleToolClick, isToolBtnActive, shouldChangeStyle, dispatch])
const login = useCallback(async () => {
// 防止重复调用(使用 sessionStorage,跨组件共享)
if (sessionStorage.getItem(SSO_LOGIN_FLAG) === 'true') {
console.log('homeNew sso111111')
return
}
// 防止重复调用(组件级别保护)
// 防止重复调用(组件级别保护,每次组件挂载时会重置,确保每次打开链接都能调用)
if (hasFetched.current) {
console.log('homeNew sso222222')
return
......@@ -285,8 +230,7 @@ export const Home: React.FC = () => {
// 如果有 loginCode,执行 SSO 登录(包括 from=tactics 的情况)
if (_loginCode && viteOutputObj === 'inner') {
console.log('homeNew sso333333')
// 立即设置标志,防止重复执行
sessionStorage.setItem(SSO_LOGIN_FLAG, 'true')
// 立即设置标志,防止重复执行(仅组件级别,每次组件挂载时会重置)
hasFetched.current = true
// 每次进入页面调用 sso_login 时,先清空 sessionStorage 中的 currentToolId
// 避免关闭标签页后再次打开时使用上次的历史 toolId
......@@ -339,7 +283,6 @@ export const Home: React.FC = () => {
catch (error) {
console.error('SSO 登录失败:', error)
// 登录失败时清除标志,允许重试
sessionStorage.removeItem(SSO_LOGIN_FLAG)
hasFetched.current = false
}
return
......@@ -371,6 +314,7 @@ export const Home: React.FC = () => {
}
else {
// 模拟登录 可以用来测试
try {
const res = await fetchLoginByUid('123123')
if (res.data) {
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
......@@ -410,6 +354,12 @@ export const Home: React.FC = () => {
}
}
}
catch (error) {
console.error('Guest 登录失败:', error)
// 登录失败时清除标志,允许重试
hasFetched.current = false
}
}
}
}, [setToken, setUserName, dispatch])
......@@ -420,8 +370,6 @@ export const Home: React.FC = () => {
useEffect(() => {
login()
console.log('login useEffect11111', token)
}, []) // 依赖数组为空,只在组件挂载时执行一次
useEffect(() => {
......
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