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
3b632092
Commit
3b632092
authored
Jan 07, 2026
by
Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 首次进入时调用接口逻辑
parent
de73cb84
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
7 deletions
+40
-7
eslint.config.mjs
+1
-0
src/pages/Chat/Chat.tsx
+3
-1
src/pages/Home/HomeNew.tsx
+36
-6
No files found.
eslint.config.mjs
View file @
3b632092
...
@@ -9,5 +9,6 @@ export default antfu({
...
@@ -9,5 +9,6 @@ export default antfu({
'n/prefer-global/process': ['off'], // 关闭process报错
'n/prefer-global/process': ['off'], // 关闭process报错
'react-hooks/exhaustive-deps': ['off'],
'react-hooks/exhaustive-deps': ['off'],
'ban-ts-comment': ['off'],
'ban-ts-comment': ['off'],
'no-console': 'off', // 允许使用 console
},
},
})
})
src/pages/Chat/Chat.tsx
View file @
3b632092
...
@@ -543,8 +543,10 @@ export const Chat: React.FC = () => {
...
@@ -543,8 +543,10 @@ export const Chat: React.FC = () => {
fromCollect
,
fromCollect
,
locationStateFromCollect
:
location
.
state
?.
fromCollect
,
locationStateFromCollect
:
location
.
state
?.
fromCollect
,
sessionStorageFromCollect
:
sessionStorage
.
getItem
(
'fromCollect'
),
sessionStorageFromCollect
:
sessionStorage
.
getItem
(
'fromCollect'
),
allItemsLength
:
allItems
.
length
,
})
})
if
(
!
fromCollect
)
{
// 如果 allItems 为空,说明是首次加载,需要加载历史记录
if
(
!
fromCollect
&&
allItems
.
length
>
0
)
{
console
.
log
(
'[Chat] 路由不变且非收藏页返回,跳过历史记录加载,只更新 toolId'
)
console
.
log
(
'[Chat] 路由不变且非收藏页返回,跳过历史记录加载,只更新 toolId'
)
// 不是从收藏页返回,只是 location.state 变化(比如点击常见问题但路由不变),
// 不是从收藏页返回,只是 location.state 变化(比如点击常见问题但路由不变),
// 不应该重新加载历史记录,只需要更新 toolId 相关状态
// 不应该重新加载历史记录,只需要更新 toolId 相关状态
...
...
src/pages/Home/HomeNew.tsx
View file @
3b632092
...
@@ -288,7 +288,12 @@ export const Home: React.FC = () => {
...
@@ -288,7 +288,12 @@ export const Home: React.FC = () => {
catch
(
error
)
{
catch
(
error
)
{
// localStorage 被阻止时,使用安全函数降级存储
// localStorage 被阻止时,使用安全函数降级存储
console
.
warn
(
'setToken 失败,使用降级方案:'
,
error
)
console
.
warn
(
'setToken 失败,使用降级方案:'
,
error
)
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
try
{
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
}
catch
(
storageError
)
{
console
.
error
(
'降级存储也失败:'
,
storageError
)
}
}
}
if
(
res
.
data
.
userName
)
{
if
(
res
.
data
.
userName
)
{
try
{
try
{
...
@@ -296,7 +301,12 @@ export const Home: React.FC = () => {
...
@@ -296,7 +301,12 @@ export const Home: React.FC = () => {
}
}
catch
{
catch
{
// userName 存储失败时使用降级方案
// userName 存储失败时使用降级方案
safeLocalStorageSetItem
(
'__USER_NAME__'
,
JSON
.
stringify
(
res
.
data
.
userName
))
try
{
safeLocalStorageSetItem
(
'__USER_NAME__'
,
JSON
.
stringify
(
res
.
data
.
userName
))
}
catch
(
storageError
)
{
console
.
error
(
'userName 降级存储失败:'
,
storageError
)
}
}
}
}
}
// 确保 token 已写入 localStorage(useLocalStorageState 是同步的,但为了保险起见,使用微任务确保写入完成)
// 确保 token 已写入 localStorage(useLocalStorageState 是同步的,但为了保险起见,使用微任务确保写入完成)
...
@@ -305,7 +315,12 @@ export const Home: React.FC = () => {
...
@@ -305,7 +315,12 @@ export const Home: React.FC = () => {
const
verifyToken
=
getTokenFromStorage
()
const
verifyToken
=
getTokenFromStorage
()
if
(
!
verifyToken
)
{
if
(
!
verifyToken
)
{
// 如果 useLocalStorageState 写入失败,使用安全函数再次尝试
// 如果 useLocalStorageState 写入失败,使用安全函数再次尝试
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
try
{
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
}
catch
(
storageError
)
{
console
.
error
(
'token 验证后降级存储失败:'
,
storageError
)
}
}
}
window
.
dispatchEvent
(
window
.
dispatchEvent
(
new
StorageEvent
(
'storage'
,
{
new
StorageEvent
(
'storage'
,
{
...
@@ -385,7 +400,12 @@ export const Home: React.FC = () => {
...
@@ -385,7 +400,12 @@ export const Home: React.FC = () => {
catch
(
error
)
{
catch
(
error
)
{
// localStorage 被阻止时,使用安全函数降级存储
// localStorage 被阻止时,使用安全函数降级存储
console
.
warn
(
'setToken 失败,使用降级方案:'
,
error
)
console
.
warn
(
'setToken 失败,使用降级方案:'
,
error
)
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
try
{
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
}
catch
(
storageError
)
{
console
.
error
(
'降级存储也失败:'
,
storageError
)
}
}
}
if
(
res
.
data
.
userName
)
{
if
(
res
.
data
.
userName
)
{
try
{
try
{
...
@@ -393,7 +413,12 @@ export const Home: React.FC = () => {
...
@@ -393,7 +413,12 @@ export const Home: React.FC = () => {
}
}
catch
{
catch
{
// userName 存储失败时使用降级方案
// userName 存储失败时使用降级方案
safeLocalStorageSetItem
(
'__USER_NAME__'
,
JSON
.
stringify
(
res
.
data
.
userName
))
try
{
safeLocalStorageSetItem
(
'__USER_NAME__'
,
JSON
.
stringify
(
res
.
data
.
userName
))
}
catch
(
storageError
)
{
console
.
error
(
'userName 降级存储失败:'
,
storageError
)
}
}
}
}
}
// 确保 token 已写入 localStorage(useLocalStorageState 是同步的,但为了保险起见,使用微任务确保写入完成)
// 确保 token 已写入 localStorage(useLocalStorageState 是同步的,但为了保险起见,使用微任务确保写入完成)
...
@@ -402,7 +427,12 @@ export const Home: React.FC = () => {
...
@@ -402,7 +427,12 @@ export const Home: React.FC = () => {
const
verifyToken
=
getTokenFromStorage
()
const
verifyToken
=
getTokenFromStorage
()
if
(
!
verifyToken
)
{
if
(
!
verifyToken
)
{
// 如果 useLocalStorageState 写入失败,使用安全函数再次尝试
// 如果 useLocalStorageState 写入失败,使用安全函数再次尝试
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
try
{
safeLocalStorageSetItem
(
'__TOKEN__'
,
JSON
.
stringify
(
res
.
data
.
token
))
}
catch
(
storageError
)
{
console
.
error
(
'token 验证后降级存储失败:'
,
storageError
)
}
}
}
// 主动触发 storage 事件,确保其他组件能监听到变化
// 主动触发 storage 事件,确保其他组件能监听到变化
window
.
dispatchEvent
(
window
.
dispatchEvent
(
...
...
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