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
5725d3c7
Commit
5725d3c7
authored
Aug 18, 2025
by
weiw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 接入sso登录
parent
9965a18a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
51 deletions
+81
-51
src/api/common.ts
+10
-0
src/layouts/MainLayout/MainLayout.tsx
+7
-9
src/pages/Home/HomeNew.tsx
+63
-42
src/routes/AppRoutes.tsx
+1
-0
No files found.
src/api/common.ts
View file @
5725d3c7
...
...
@@ -11,6 +11,16 @@ export function fetchLoginByUid(uid: string) {
}
/**
* 第三方登录
* @returns
*/
export
function
fetchLoginByToken
(
loginCode
:
any
)
{
return
http
.
post
(
'/user/api/login/mobile/v1/sso_login'
,
{
loginCode
,
})
}
/**
* 获取协议列表
* @returns
*/
...
...
src/layouts/MainLayout/MainLayout.tsx
View file @
5725d3c7
...
...
@@ -8,8 +8,6 @@ import { HistoryBar } from '../HistoryBar/HistoryBar'
import
styles
from
'./MainLayout.module.less'
import
{
useAuth
}
from
'@/auth/AuthContext'
import
{
LoginModal
}
from
'@/components/LoginModal'
import
{
useAppDispatch
}
from
'@/store/hook'
import
{
fetchConversations
}
from
'@/store/conversationSlice'
import
MingcuteArrowsRightFill
from
'@/assets/svg/MingcuteArrowsRightFill.svg?react'
import
{
isMobile
}
from
'@/utils'
...
...
@@ -42,8 +40,8 @@ export const MainLayout: React.FC<MainLayoutProps> = ({ children }) => {
const
{
showLoginModal
,
toggleLoginModal
}
=
useAuth
()
const
[
isHistoryVisible
,
setHistoryVisible
]
=
useState
(
false
)
const
location
=
useLocation
()
const
dispatch
=
useAppDispatch
()
const
token
=
window
.
localStorage
.
getItem
(
'__TOKEN__'
)
//
const dispatch = useAppDispatch()
//
const token = window.localStorage.getItem('__TOKEN__')
const
[
navBarVisibleLocal
]
=
useSessionStorageState
<
string
|
undefined
>
(
'__NAV_BAR_VISIBLE_LOCAL__'
,
{
...
...
@@ -52,11 +50,11 @@ export const MainLayout: React.FC<MainLayoutProps> = ({ children }) => {
},
)
useEffect
(()
=>
{
if
(
token
)
{
dispatch
(
fetchConversations
())
}
},
[
dispatch
])
//
useEffect(() => {
//
if (token) {
//
dispatch(fetchConversations())
//
}
//
}, [dispatch])
useEffect
(()
=>
{
if
(
location
.
pathname
===
'/tools'
||
location
.
pathname
===
'/collect'
)
{
...
...
src/pages/Home/HomeNew.tsx
View file @
5725d3c7
import
type
React
from
'react'
import
{
useCallback
,
useEffect
,
useState
}
from
'react'
import
{
useCallback
,
useEffect
,
use
Ref
,
use
State
}
from
'react'
import
{
motion
}
from
'framer-motion'
import
{
Outlet
,
useLocation
}
from
'react-router-dom'
import
{
useLocalStorageState
}
from
'ahooks'
...
...
@@ -7,11 +7,11 @@ import styles from './Home.module.less'
import
{
QuestionList
}
from
'./components/QuestionList'
import
HomeIcon1
from
'@/assets/homeIcon1.png'
import
HomeIcon2
from
'@/assets/homeIcon2.png'
import
{
createConversation
}
from
'@/store/conversationSlice'
import
{
createConversation
,
fetchConversations
}
from
'@/store/conversationSlice'
import
{
useAppDispatch
}
from
'@/store/hook'
import
{
fetchQuestionList
}
from
'@/api/home'
import
SdreamLoading
from
'@/components/SdreamLoading'
import
{
fetchLoginBy
Uid
}
from
'@/api/common'
import
{
fetchLoginBy
Token
}
from
'@/api/common'
function
getAnimationProps
(
delay
:
number
)
{
return
{
...
...
@@ -43,45 +43,17 @@ export const Home: React.FC = () => {
const
[
isLoading
,
setIsLoading
]
=
useState
(
false
)
const
dispatch
=
useAppDispatch
()
const
location
=
useLocation
()
const
hasFetched
=
useRef
(
false
)
// 使用 useState
const
[
productQuestions
,
setProductQuestions
]
=
useState
<
any
>
({
content
:
[]
})
const
[
otherQuestions
,
setOtherQuestions
]
=
useState
<
any
>
({
content
:
[]
})
const
[,
setToken
]
=
useLocalStorageState
<
string
|
undefined
>
(
'__TOKEN__'
,
{
defaultValue
:
''
,
},
)
const
login
=
async
()
=>
{
const
res
=
await
fetchLoginByUid
(
'123123'
)
if
(
res
.
data
)
{
setToken
(
res
.
data
.
token
)
}
}
useEffect
(()
=>
{
const
fromCollect
=
location
.
state
?.
fromCollect
// 在组件挂载时执行dispatch,但只执行一次
if
(
!
fromCollect
)
{
dispatch
(
createConversation
({
conversationData
:
{},
shouldNavigate
:
true
,
shouldSendQuestion
:
''
,
}),
)
}
// 清除状态以避免下次影响
if
(
location
.
state
?.
fromCollect
)
{
// 使用 replace 替换当前历史记录,清除 state
window
.
history
.
replaceState
({},
document
.
title
,
window
.
location
.
pathname
)
}
},
[])
// const location = useLocation()
const
[
token
,
setToken
]
=
useLocalStorageState
<
string
|
undefined
>
(
'__TOKEN__'
,
{
defaultValue
:
''
,
})
const
[,
setIsLoggedIn
]
=
useState
(
!!
token
)
const
[,
setShowLoginModal
]
=
useState
(
false
)
const
[,
setShowLoginTip
]
=
useState
(
!
token
)
// 检查当前路径是否包含 "/chat/"
// const showOutlet = location.pathname.includes('/chat/')
...
...
@@ -91,7 +63,7 @@ export const Home: React.FC = () => {
// conversationData: {},
// shouldNavigate: true,
// shouldSendQuestion: question,
// }),
//
}),
// )
// }
...
...
@@ -126,10 +98,59 @@ export const Home: React.FC = () => {
}
},
[])
const
initConversation
=
()
=>
{
const
fromCollect
=
location
.
state
?.
fromCollect
// 在组件挂载时执行dispatch,但只执行一次
if
(
!
fromCollect
)
{
dispatch
(
createConversation
({
conversationData
:
{},
shouldNavigate
:
true
,
shouldSendQuestion
:
''
,
}),
)
}
// 清除状态以避免下次影响
if
(
location
.
state
?.
fromCollect
)
{
// 使用 replace 替换当前历史记录,清除 state
window
.
history
.
replaceState
({},
document
.
title
,
window
.
location
.
pathname
)
}
}
const
login
=
useCallback
(
async
()
=>
{
// 防止重复调用
if
(
hasFetched
.
current
)
{
return
}
hasFetched
.
current
=
true
const
url
=
new
URL
(
window
.
location
.
href
)
// 获取查询参数
const
searchParams
=
new
URLSearchParams
(
url
.
search
)
const
token
=
searchParams
.
get
(
'token'
)
const
res
=
await
fetchLoginByToken
(
token
)
if
(
res
.
data
)
{
setToken
(
res
.
data
.
token
)
setIsLoggedIn
(
!!
token
)
setShowLoginTip
(
!
token
)
setShowLoginModal
(
false
)
await
getQuestionList
()
initConversation
()
dispatch
(
fetchConversations
())
}
},
[
setToken
])
useEffect
(()
=>
{
login
()
getQuestionList
()
},
[])
if
(
!
token
)
{
login
()
}
else
{
getQuestionList
()
initConversation
()
dispatch
(
fetchConversations
())
}
},
[
login
,
getQuestionList
])
return
(
<
div
className=
{
styles
.
homePage
}
>
...
...
src/routes/AppRoutes.tsx
View file @
5725d3c7
...
...
@@ -13,6 +13,7 @@ const AppRoutesComponent: React.FC = () => {
<
Route
path=
"/"
element=
{
<
Home
/>
}
>
<
Route
path=
"/chat/:id"
element=
{
<
Chat
/>
}
/>
</
Route
>
<
Route
path=
"/home"
element=
{
<
Home
/>
}
></
Route
>
<
Route
path=
"/collect"
element=
{
<
Collect
/>
}
/>
<
Route
path=
"/tools"
element=
{
<
Tools
/>
}
/>
<
Route
path=
"/protocol/:id"
element=
{
<
Protocol
/>
}
/>
...
...
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