Commit 5725d3c7 by weiw

fix: 接入sso登录

parent 9965a18a
...@@ -11,6 +11,16 @@ export function fetchLoginByUid(uid: string) { ...@@ -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 * @returns
*/ */
......
...@@ -8,8 +8,6 @@ import { HistoryBar } from '../HistoryBar/HistoryBar' ...@@ -8,8 +8,6 @@ import { HistoryBar } from '../HistoryBar/HistoryBar'
import styles from './MainLayout.module.less' import styles from './MainLayout.module.less'
import { useAuth } from '@/auth/AuthContext' import { useAuth } from '@/auth/AuthContext'
import { LoginModal } from '@/components/LoginModal' import { LoginModal } from '@/components/LoginModal'
import { useAppDispatch } from '@/store/hook'
import { fetchConversations } from '@/store/conversationSlice'
import MingcuteArrowsRightFill from '@/assets/svg/MingcuteArrowsRightFill.svg?react' import MingcuteArrowsRightFill from '@/assets/svg/MingcuteArrowsRightFill.svg?react'
import { isMobile } from '@/utils' import { isMobile } from '@/utils'
...@@ -42,8 +40,8 @@ export const MainLayout: React.FC<MainLayoutProps> = ({ children }) => { ...@@ -42,8 +40,8 @@ export const MainLayout: React.FC<MainLayoutProps> = ({ children }) => {
const { showLoginModal, toggleLoginModal } = useAuth() const { showLoginModal, toggleLoginModal } = useAuth()
const [isHistoryVisible, setHistoryVisible] = useState(false) const [isHistoryVisible, setHistoryVisible] = useState(false)
const location = useLocation() const location = useLocation()
const dispatch = useAppDispatch() // const dispatch = useAppDispatch()
const token = window.localStorage.getItem('__TOKEN__') // const token = window.localStorage.getItem('__TOKEN__')
const [navBarVisibleLocal] = useSessionStorageState<string | undefined>( const [navBarVisibleLocal] = useSessionStorageState<string | undefined>(
'__NAV_BAR_VISIBLE_LOCAL__', '__NAV_BAR_VISIBLE_LOCAL__',
{ {
...@@ -52,11 +50,11 @@ export const MainLayout: React.FC<MainLayoutProps> = ({ children }) => { ...@@ -52,11 +50,11 @@ export const MainLayout: React.FC<MainLayoutProps> = ({ children }) => {
}, },
) )
useEffect(() => { // useEffect(() => {
if (token) { // if (token) {
dispatch(fetchConversations()) // dispatch(fetchConversations())
} // }
}, [dispatch]) // }, [dispatch])
useEffect(() => { useEffect(() => {
if (location.pathname === '/tools' || location.pathname === '/collect') { if (location.pathname === '/tools' || location.pathname === '/collect') {
......
import type React from 'react' import type React from 'react'
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect, useRef, useState } from 'react'
import { motion } from 'framer-motion' import { motion } from 'framer-motion'
import { Outlet, useLocation } from 'react-router-dom' import { Outlet, useLocation } from 'react-router-dom'
import { useLocalStorageState } from 'ahooks' import { useLocalStorageState } from 'ahooks'
...@@ -7,11 +7,11 @@ import styles from './Home.module.less' ...@@ -7,11 +7,11 @@ import styles from './Home.module.less'
import { QuestionList } from './components/QuestionList' import { QuestionList } from './components/QuestionList'
import HomeIcon1 from '@/assets/homeIcon1.png' import HomeIcon1 from '@/assets/homeIcon1.png'
import HomeIcon2 from '@/assets/homeIcon2.png' import HomeIcon2 from '@/assets/homeIcon2.png'
import { createConversation } from '@/store/conversationSlice' import { createConversation, fetchConversations } from '@/store/conversationSlice'
import { useAppDispatch } from '@/store/hook' import { useAppDispatch } from '@/store/hook'
import { fetchQuestionList } from '@/api/home' import { fetchQuestionList } from '@/api/home'
import SdreamLoading from '@/components/SdreamLoading' import SdreamLoading from '@/components/SdreamLoading'
import { fetchLoginByUid } from '@/api/common' import { fetchLoginByToken } from '@/api/common'
function getAnimationProps(delay: number) { function getAnimationProps(delay: number) {
return { return {
...@@ -43,45 +43,17 @@ export const Home: React.FC = () => { ...@@ -43,45 +43,17 @@ export const Home: React.FC = () => {
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const location = useLocation() const location = useLocation()
const hasFetched = useRef(false)
// 使用 useState // 使用 useState
const [productQuestions, setProductQuestions] = useState<any>({ content: [] }) const [productQuestions, setProductQuestions] = useState<any>({ content: [] })
const [otherQuestions, setOtherQuestions] = useState<any>({ content: [] }) const [otherQuestions, setOtherQuestions] = useState<any>({ content: [] })
const [, setToken] = useLocalStorageState<string | undefined>( const [token, setToken] = useLocalStorageState<string | undefined>('__TOKEN__', {
'__TOKEN__',
{
defaultValue: '', defaultValue: '',
}, })
) const [, setIsLoggedIn] = useState(!!token)
const [, setShowLoginModal] = useState(false)
const login = async () => { const [, setShowLoginTip] = useState(!token)
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()
// 检查当前路径是否包含 "/chat/" // 检查当前路径是否包含 "/chat/"
// const showOutlet = location.pathname.includes('/chat/') // const showOutlet = location.pathname.includes('/chat/')
...@@ -126,10 +98,59 @@ export const Home: React.FC = () => { ...@@ -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(() => { useEffect(() => {
if (!token) {
login() login()
}
else {
getQuestionList() getQuestionList()
}, []) initConversation()
dispatch(fetchConversations())
}
}, [login, getQuestionList])
return ( return (
<div className={styles.homePage}> <div className={styles.homePage}>
......
...@@ -13,6 +13,7 @@ const AppRoutesComponent: React.FC = () => { ...@@ -13,6 +13,7 @@ const AppRoutesComponent: React.FC = () => {
<Route path="/" element={<Home />}> <Route path="/" element={<Home />}>
<Route path="/chat/:id" element={<Chat />} /> <Route path="/chat/:id" element={<Chat />} />
</Route> </Route>
<Route path="/home" element={<Home />}></Route>
<Route path="/collect" element={<Collect />} /> <Route path="/collect" element={<Collect />} />
<Route path="/tools" element={<Tools />} /> <Route path="/tools" element={<Tools />} />
<Route path="/protocol/:id" element={<Protocol />} /> <Route path="/protocol/:id" element={<Protocol />} />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment