Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sdream-ai-fe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
侯明涛
sdream-ai-fe
Commits
2514136e
Commit
2514136e
authored
Jan 04, 2026
by
Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:sso
parent
0526f0f1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
15 deletions
+37
-15
src/pages/Home/HomeNew.tsx
+37
-15
No files found.
src/pages/Home/HomeNew.tsx
View file @
2514136e
...
...
@@ -42,6 +42,9 @@ function getAnimationProps(delay: number) {
}
}
// 使用 sessionStorage 作为标志,防止跨组件重复执行 SSO 登录
const
SSO_LOGIN_FLAG
=
'sso_login_fetched'
export
const
Home
:
React
.
FC
=
()
=>
{
const
viteOutputObj
=
import
.
meta
.
env
.
VITE_OUTPUT_OBJ
||
'open'
const
[
isLoading
,
_setIsLoading
]
=
useState
(
false
)
...
...
@@ -241,26 +244,31 @@ export const Home: React.FC = () => {
},
[
_handleToolClick
,
isToolBtnActive
,
shouldChangeStyle
,
dispatch
])
const
login
=
useCallback
(
async
()
=>
{
// 防止重复调用
// 防止重复调用(使用 sessionStorage,跨组件共享)
if
(
sessionStorage
.
getItem
(
SSO_LOGIN_FLAG
)
===
'true'
)
{
return
}
// 防止重复调用(组件级别保护)
if
(
hasFetched
.
current
)
{
return
}
hasFetched
.
current
=
true
const
url
=
new
URL
(
window
.
location
.
href
)
// 获取查询参数
const
searchParams
=
new
URLSearchParams
(
url
.
search
)
const
from
=
searchParams
.
get
(
'from'
)
const
_loginCode
=
searchParams
.
get
(
'loginCode'
)
let
res
=
{}
as
any
if
(
viteOutputObj
===
'inner'
)
{
if
(
_loginCode
)
{
// 如果有 loginCode,执行 SSO 登录(包括 from=tactics 的情况)
if
(
_loginCode
&&
viteOutputObj
===
'inner'
)
{
// 立即设置标志,防止重复执行
sessionStorage
.
setItem
(
SSO_LOGIN_FLAG
,
'true'
)
hasFetched
.
current
=
true
// 每次进入页面调用 sso_login 时,先清空 sessionStorage 中的 currentToolId
// 避免关闭标签页后再次打开时使用上次的历史 toolId
safeSessionStorageRemoveItem
(
'currentToolId'
)
res
=
await
fetchLoginByToken
(
_loginCode
)
try
{
const
res
=
await
fetchLoginByToken
(
_loginCode
)
if
(
res
.
data
)
{
// 登录成功后先打印完整的原始链接(删除之前)
// eslint-disable-next-line no-console
console
.
log
(
'20251211登录成功,删除前完整链接:'
,
window
.
location
.
href
)
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch
(
clearCurrentToolId
())
safeSessionStorageRemoveItem
(
'currentToolId'
)
...
...
@@ -270,9 +278,6 @@ export const Home: React.FC = () => {
// 使用 replace 避免产生新的历史记录
window
.
history
.
replaceState
({},
''
,
currentUrl
.
toString
())
}
// 删除后打印链接
// eslint-disable-next-line no-console
console
.
log
(
'20251211登录成功,删除后完整链接:'
,
window
.
location
.
href
)
setToken
(
res
.
data
.
token
)
// 主动触发 storage 事件,确保其他组件能监听到变化
window
.
dispatchEvent
(
...
...
@@ -284,6 +289,8 @@ export const Home: React.FC = () => {
storageArea
:
localStorage
,
}),
)
// 如果是 from=tactics,只执行登录,不执行其他业务逻辑(由 TacticsHome 处理)
if
(
from
!==
'tactics'
)
{
// 触发自定义事件,通知 ChatEditor 强制重置为制度活化
window
.
dispatchEvent
(
new
CustomEvent
(
'forceResetToGeneralMode'
))
getSessionConversationId
()
...
...
@@ -292,14 +299,26 @@ export const Home: React.FC = () => {
_handleToolClick
(
false
,
''
,
true
)
}
}
else
{
}
catch
(
error
)
{
console
.
error
(
'SSO 登录失败:'
,
error
)
// 登录失败时清除标志,允许重试
sessionStorage
.
removeItem
(
SSO_LOGIN_FLAG
)
hasFetched
.
current
=
false
}
return
}
// 如果没有 loginCode,执行其他逻辑(非 from=tactics 的情况)
if
(
from
!==
'tactics'
)
{
hasFetched
.
current
=
true
if
(
viteOutputObj
===
'inner'
)
{
getSessionConversationId
()
dispatch
(
fetchConversations
())
}
}
else
{
// 模拟登录 可以用来测试
res
=
await
fetchLoginByUid
(
'123123'
)
const
res
=
await
fetchLoginByUid
(
'123123'
)
if
(
res
.
data
)
{
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch
(
clearCurrentToolId
())
...
...
@@ -328,6 +347,7 @@ export const Home: React.FC = () => {
_handleToolClick
(
false
,
''
,
true
)
}
}
}
},
[
setToken
,
dispatch
])
// 监听路由参数变化,提取 userRoles(确保路由参数被正确解析)
...
...
@@ -337,6 +357,8 @@ export const Home: React.FC = () => {
useEffect
(()
=>
{
login
()
// eslint-disable-next-line no-console
console
.
log
(
'login useEffect11111'
,
token
)
},
[])
// 依赖数组为空,只在组件挂载时执行一次
useEffect
(()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment