Commit 5725d3c7 by weiw

fix: 接入sso登录

parent 9965a18a
......@@ -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
*/
......
......@@ -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') {
......
import type React from 'react'
import { useCallback, useEffect, useState } from 'react'
import { useCallback, useEffect, useRef, useState } 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 { fetchLoginByUid } from '@/api/common'
import { fetchLoginByToken } 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}>
......
......@@ -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 />} />
......
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