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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
50 deletions
+72
-50
src/pages/Home/HomeNew.tsx
+72
-50
No files found.
src/pages/Home/HomeNew.tsx
View file @
2514136e
...
@@ -42,6 +42,9 @@ function getAnimationProps(delay: number) {
...
@@ -42,6 +42,9 @@ function getAnimationProps(delay: number) {
}
}
}
}
// 使用 sessionStorage 作为标志,防止跨组件重复执行 SSO 登录
const
SSO_LOGIN_FLAG
=
'sso_login_fetched'
export
const
Home
:
React
.
FC
=
()
=>
{
export
const
Home
:
React
.
FC
=
()
=>
{
const
viteOutputObj
=
import
.
meta
.
env
.
VITE_OUTPUT_OBJ
||
'open'
const
viteOutputObj
=
import
.
meta
.
env
.
VITE_OUTPUT_OBJ
||
'open'
const
[
isLoading
,
_setIsLoading
]
=
useState
(
false
)
const
[
isLoading
,
_setIsLoading
]
=
useState
(
false
)
...
@@ -241,26 +244,31 @@ export const Home: React.FC = () => {
...
@@ -241,26 +244,31 @@ export const Home: React.FC = () => {
},
[
_handleToolClick
,
isToolBtnActive
,
shouldChangeStyle
,
dispatch
])
},
[
_handleToolClick
,
isToolBtnActive
,
shouldChangeStyle
,
dispatch
])
const
login
=
useCallback
(
async
()
=>
{
const
login
=
useCallback
(
async
()
=>
{
// 防止重复调用
// 防止重复调用(使用 sessionStorage,跨组件共享)
if
(
sessionStorage
.
getItem
(
SSO_LOGIN_FLAG
)
===
'true'
)
{
return
}
// 防止重复调用(组件级别保护)
if
(
hasFetched
.
current
)
{
if
(
hasFetched
.
current
)
{
return
return
}
}
hasFetched
.
current
=
true
const
url
=
new
URL
(
window
.
location
.
href
)
const
url
=
new
URL
(
window
.
location
.
href
)
// 获取查询参数
// 获取查询参数
const
searchParams
=
new
URLSearchParams
(
url
.
search
)
const
searchParams
=
new
URLSearchParams
(
url
.
search
)
const
from
=
searchParams
.
get
(
'from'
)
const
_loginCode
=
searchParams
.
get
(
'loginCode'
)
const
_loginCode
=
searchParams
.
get
(
'loginCode'
)
let
res
=
{}
as
any
if
(
viteOutputObj
===
'inner'
)
{
// 如果有 loginCode,执行 SSO 登录(包括 from=tactics 的情况)
if
(
_loginCode
)
{
if
(
_loginCode
&&
viteOutputObj
===
'inner'
)
{
// 每次进入页面调用 sso_login 时,先清空 sessionStorage 中的 currentToolId
// 立即设置标志,防止重复执行
// 避免关闭标签页后再次打开时使用上次的历史 toolId
sessionStorage
.
setItem
(
SSO_LOGIN_FLAG
,
'true'
)
safeSessionStorageRemoveItem
(
'currentToolId'
)
hasFetched
.
current
=
true
res
=
await
fetchLoginByToken
(
_loginCode
)
// 每次进入页面调用 sso_login 时,先清空 sessionStorage 中的 currentToolId
// 避免关闭标签页后再次打开时使用上次的历史 toolId
safeSessionStorageRemoveItem
(
'currentToolId'
)
try
{
const
res
=
await
fetchLoginByToken
(
_loginCode
)
if
(
res
.
data
)
{
if
(
res
.
data
)
{
// 登录成功后先打印完整的原始链接(删除之前)
// eslint-disable-next-line no-console
console
.
log
(
'20251211登录成功,删除前完整链接:'
,
window
.
location
.
href
)
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch
(
clearCurrentToolId
())
dispatch
(
clearCurrentToolId
())
safeSessionStorageRemoveItem
(
'currentToolId'
)
safeSessionStorageRemoveItem
(
'currentToolId'
)
...
@@ -270,9 +278,56 @@ export const Home: React.FC = () => {
...
@@ -270,9 +278,56 @@ export const Home: React.FC = () => {
// 使用 replace 避免产生新的历史记录
// 使用 replace 避免产生新的历史记录
window
.
history
.
replaceState
({},
''
,
currentUrl
.
toString
())
window
.
history
.
replaceState
({},
''
,
currentUrl
.
toString
())
}
}
// 删除后打印链接
setToken
(
res
.
data
.
token
)
// eslint-disable-next-line no-console
// 主动触发 storage 事件,确保其他组件能监听到变化
console
.
log
(
'20251211登录成功,删除后完整链接:'
,
window
.
location
.
href
)
window
.
dispatchEvent
(
new
StorageEvent
(
'storage'
,
{
key
:
'__TOKEN__'
,
oldValue
:
token
,
newValue
:
res
.
data
.
token
,
url
:
window
.
location
.
href
,
storageArea
:
localStorage
,
}),
)
// 如果是 from=tactics,只执行登录,不执行其他业务逻辑(由 TacticsHome 处理)
if
(
from
!==
'tactics'
)
{
// 触发自定义事件,通知 ChatEditor 强制重置为制度活化
window
.
dispatchEvent
(
new
CustomEvent
(
'forceResetToGeneralMode'
))
getSessionConversationId
()
dispatch
(
fetchConversations
())
// 2. 拉取常见问题等业务数据
_handleToolClick
(
false
,
''
,
true
)
}
}
}
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
{
// 模拟登录 可以用来测试
const
res
=
await
fetchLoginByUid
(
'123123'
)
if
(
res
.
data
)
{
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch
(
clearCurrentToolId
())
safeSessionStorageRemoveItem
(
'currentToolId'
)
const
currentUrl
=
new
URL
(
window
.
location
.
href
)
if
(
currentUrl
.
searchParams
.
has
(
'toolId'
))
{
currentUrl
.
searchParams
.
delete
(
'toolId'
)
window
.
history
.
replaceState
({},
''
,
currentUrl
.
toString
())
}
setToken
(
res
.
data
.
token
)
setToken
(
res
.
data
.
token
)
// 主动触发 storage 事件,确保其他组件能监听到变化
// 主动触发 storage 事件,确保其他组件能监听到变化
window
.
dispatchEvent
(
window
.
dispatchEvent
(
...
@@ -292,41 +347,6 @@ export const Home: React.FC = () => {
...
@@ -292,41 +347,6 @@ export const Home: React.FC = () => {
_handleToolClick
(
false
,
''
,
true
)
_handleToolClick
(
false
,
''
,
true
)
}
}
}
}
else
{
getSessionConversationId
()
dispatch
(
fetchConversations
())
}
}
else
{
// 模拟登录 可以用来测试
res
=
await
fetchLoginByUid
(
'123123'
)
if
(
res
.
data
)
{
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch
(
clearCurrentToolId
())
safeSessionStorageRemoveItem
(
'currentToolId'
)
const
currentUrl
=
new
URL
(
window
.
location
.
href
)
if
(
currentUrl
.
searchParams
.
has
(
'toolId'
))
{
currentUrl
.
searchParams
.
delete
(
'toolId'
)
window
.
history
.
replaceState
({},
''
,
currentUrl
.
toString
())
}
setToken
(
res
.
data
.
token
)
// 主动触发 storage 事件,确保其他组件能监听到变化
window
.
dispatchEvent
(
new
StorageEvent
(
'storage'
,
{
key
:
'__TOKEN__'
,
oldValue
:
token
,
newValue
:
res
.
data
.
token
,
url
:
window
.
location
.
href
,
storageArea
:
localStorage
,
}),
)
// 触发自定义事件,通知 ChatEditor 强制重置为制度活化
window
.
dispatchEvent
(
new
CustomEvent
(
'forceResetToGeneralMode'
))
getSessionConversationId
()
dispatch
(
fetchConversations
())
// 2. 拉取常见问题等业务数据
_handleToolClick
(
false
,
''
,
true
)
}
}
}
},
[
setToken
,
dispatch
])
},
[
setToken
,
dispatch
])
...
@@ -337,6 +357,8 @@ export const Home: React.FC = () => {
...
@@ -337,6 +357,8 @@ export const Home: React.FC = () => {
useEffect
(()
=>
{
useEffect
(()
=>
{
login
()
login
()
// eslint-disable-next-line no-console
console
.
log
(
'login useEffect11111'
,
token
)
},
[])
// 依赖数组为空,只在组件挂载时执行一次
},
[])
// 依赖数组为空,只在组件挂载时执行一次
useEffect
(()
=>
{
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