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
0e49191f
Commit
0e49191f
authored
Dec 05, 2025
by
Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:chat页面增加常见问题调用
parent
ddfe6d87
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
35 deletions
+56
-35
src/lib/utils.ts
+29
-16
src/pages/Home/HomeNew.tsx
+27
-19
No files found.
src/lib/utils.ts
View file @
0e49191f
...
...
@@ -34,7 +34,11 @@ function getQueryBeforeSecondQuestion(): string {
}
}
export
function
getUserRolesFromRouteAndStore
():
string
[]
{
/**
* 从路由获取 userRoles(不存储到 localStorage)
* @returns 返回获取到的 userRoles 数组
*/
export
function
getUserRolesFromRoute
():
string
[]
{
try
{
const
sanitizedSearch
=
getQueryBeforeSecondQuestion
()
const
searchParams
=
new
URLSearchParams
(
sanitizedSearch
||
window
.
location
.
search
)
...
...
@@ -57,16 +61,6 @@ export function getUserRolesFromRouteAndStore(): string[] {
}
}
// 如果获取到了 userRoles,存储到 localStorage
if
(
userRoles
.
length
>
0
)
{
try
{
localStorage
.
setItem
(
USER_ROLES_STORAGE_KEY
,
JSON
.
stringify
(
userRoles
))
}
catch
(
error
)
{
console
.
error
(
'存储 userRoles 到 localStorage 失败:'
,
error
)
}
}
return
userRoles
}
catch
{
...
...
@@ -75,6 +69,27 @@ export function getUserRolesFromRouteAndStore(): string[] {
}
/**
* @deprecated 已废弃,请使用 getUserRolesFromRoute
* 从路由获取 userRoles 并存储到 localStorage
* @returns 返回获取到的 userRoles 数组
*/
export
function
getUserRolesFromRouteAndStore
():
string
[]
{
const
userRoles
=
getUserRolesFromRoute
()
// 如果获取到了 userRoles,存储到 localStorage(向后兼容)
if
(
userRoles
.
length
>
0
)
{
try
{
localStorage
.
setItem
(
USER_ROLES_STORAGE_KEY
,
JSON
.
stringify
(
userRoles
))
}
catch
(
error
)
{
console
.
error
(
'存储 userRoles 到 localStorage 失败:'
,
error
)
}
}
return
userRoles
}
/**
* 从 localStorage 读取 userRoles
* @returns 返回 userRoles 数组,如果没有则返回空数组
*/
...
...
@@ -95,13 +110,11 @@ export function getUserRolesFromStorage(): string[] {
}
/**
* 获取 userRoles(
先同步路由到 localStorage,然后读取
)
* 获取 userRoles(
从路由获取,确保使用最新的路由参数
)
* 这是推荐的统一方法,确保调用 fetchToolList 时能获取到正确的 userRoles
* @returns 返回 userRoles 数组,如果没有则返回空数组
*/
export
function
getUserRolesForApi
():
string
[]
{
// 先同步路由中的 userRoles 到 localStorage(如果路由中有的话)
getUserRolesFromRouteAndStore
()
// 然后从 localStorage 读取(localStorage.setItem 是同步的,所以可以立即读取)
return
getUserRolesFromStorage
()
// 直接从路由中获取 userRoles(路由参数是唯一真实来源)
return
getUserRolesFromRoute
()
}
src/pages/Home/HomeNew.tsx
View file @
0e49191f
...
...
@@ -12,6 +12,7 @@ import { useAppDispatch } from '@/store/hook'
import
{
fetchEfficiencyQuestionList
}
from
'@/api/home'
import
SdreamLoading
from
'@/components/SdreamLoading'
import
{
fetchLoginByToken
,
fetchLoginByUid
}
from
'@/api/common'
import
{
getUserRolesFromRoute
}
from
'@/lib/utils'
function
getAnimationProps
(
delay
:
number
)
{
return
{
...
...
@@ -85,26 +86,28 @@ export const Home: React.FC = () => {
}))
setIsDataLoaded
(
false
)
// 重置加载状态
try
{
const
storedToolId
=
sessionStorage
.
getItem
(
'currentToolId'
)
||
''
const
searchParams
=
new
URLSearchParams
(
location
.
search
)
// 首页初始化加载常见问题时,允许忽略路由中的 toolId,避免带入上一次的工具 ID
const
urlToolId
=
ignoreUrlToolId
?
''
:
(
searchParams
.
get
(
'toolId'
)
||
''
)
const
shouldForceClearToolId
=
!
storedToolId
&&
!
urlToolId
const
storedToolId
=
sessionStorage
.
getItem
(
'currentToolId'
)
||
''
const
searchParams
=
new
URLSearchParams
(
location
.
search
)
// 首页初始化加载常见问题时,允许忽略路由中的 toolId,避免带入上一次的工具 ID
const
urlToolId
=
ignoreUrlToolId
?
''
:
(
searchParams
.
get
(
'toolId'
)
||
''
)
const
shouldForceClearToolId
=
!
storedToolId
&&
!
urlToolId
let
finalToolId
=
toolId
||
''
let
finalToolId
=
toolId
||
''
// 场景:首页首次挂载(ignoreUrlToolId === true)且为通用模式(!isToolBtn && !toolId)
// 此时无论 sessionStorage 中是否残留上一次的 toolId,都强制使用空字符串,避免带入历史工具 ID
if
(
ignoreUrlToolId
&&
!
isToolBtn
&&
!
toolId
)
{
finalToolId
=
''
}
else
if
(
shouldForceClearToolId
&&
!
isToolBtn
)
{
finalToolId
=
''
}
else
if
(
!
finalToolId
&&
!
isToolBtn
)
{
// 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId
finalToolId
=
storedToolId
||
urlToolId
}
// 场景:首页首次挂载(ignoreUrlToolId === true)且为通用模式(!isToolBtn && !toolId)
// 此时无论 sessionStorage 中是否残留上一次的 toolId,都强制使用空字符串,避免带入历史工具 ID
if
(
ignoreUrlToolId
&&
!
isToolBtn
&&
!
toolId
)
{
finalToolId
=
''
}
else
if
(
shouldForceClearToolId
&&
!
isToolBtn
)
{
finalToolId
=
''
}
else
if
(
!
finalToolId
&&
!
isToolBtn
)
{
// 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId
finalToolId
=
storedToolId
||
urlToolId
}
// 调用真实 API 获取常见问题列表
const
res
=
await
fetchEfficiencyQuestionList
({
toolId
:
finalToolId
})
if
(
res
&&
res
.
data
&&
res
.
data
.
questions
)
{
setOtherQuestions
((
prev
:
any
)
=>
({
...
...
@@ -230,13 +233,18 @@ export const Home: React.FC = () => {
}
},
[
setToken
,
dispatch
])
// 监听路由参数变化,提取 userRoles(确保路由参数被正确解析)
useEffect
(()
=>
{
getUserRolesFromRoute
()
},
[
location
.
search
])
useEffect
(()
=>
{
login
()
// 首页首次挂载时重置为通用模式:
// 1. 清除 Redux 中的 currentToolId
dispatch
(
clearCurrentToolId
())
// 2. 清除 sessionStorage 中可能残留的 currentToolId,避免沿用上一次工具模式
sessionStorage
.
removeItem
(
'currentToolId'
)
//
sessionStorage.removeItem('currentToolId')
// 3. 首页首次挂载时强制忽略路由中的 toolId,只按通用模式拉常见问题(toolId: '')
_handleToolClick
(
false
,
''
,
true
)
},
[])
// 依赖数组为空,只在组件挂载时执行一次
...
...
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