From 98a57965922ec3456f9c508d347891050f1e208b Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Sun, 16 Jul 2023 13:20:25 +0800 Subject: [PATCH] ssr init --- client/src/components/Layout/index.tsx | 5 +++-- client/src/pages/_app.tsx | 18 +++++++++++++++++- client/src/pages/_error.tsx | 9 +++++++++ client/src/pages/chat/share.tsx | 11 ++++++----- client/src/pages/kb/detail/components/Info.tsx | 6 ++++++ client/src/store/global.ts | 12 ++++++++++-- 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/client/src/components/Layout/index.tsx b/client/src/components/Layout/index.tsx index 8a64c8147..c6103cdb9 100644 --- a/client/src/components/Layout/index.tsx +++ b/client/src/components/Layout/index.tsx @@ -63,7 +63,7 @@ const Layout = ({ children }: { children: JSX.Element }) => { return ( <> - {isPc ? ( + {isPc === true && ( <> {pcUnShowLayoutRoute[router.pathname] ? ( {children} @@ -78,7 +78,8 @@ const Layout = ({ children }: { children: JSX.Element }) => { )} - ) : ( + )} + {isPc === false && ( <> {phoneUnShowLayoutRoute[router.pathname] || isChatPage ? ( diff --git a/client/src/pages/_app.tsx b/client/src/pages/_app.tsx index 993c6421d..fb2b96940 100644 --- a/client/src/pages/_app.tsx +++ b/client/src/pages/_app.tsx @@ -11,6 +11,9 @@ import Router from 'next/router'; import 'nprogress/nprogress.css'; import '@/styles/reset.scss'; import { clientInitData } from '@/store/static'; +import { NextPageContext } from 'next'; +import { useGlobalStore } from '@/store/global'; +import { GET } from '@/service/api/axios'; //Binding events. Router.events.on('routeChangeStart', () => NProgress.start()); @@ -28,9 +31,14 @@ const queryClient = new QueryClient({ } }); -function App({ Component, pageProps }: AppProps) { +function App({ Component, pageProps, isPc }: AppProps & { isPc?: boolean; response: any }) { const [googleVerKey, setGoogleVerKey] = useState(); const [baiduTongji, setBaiduTongji] = useState(); + const { initIsPc } = useGlobalStore(); + + if (isPc !== undefined) { + initIsPc(isPc); + } useEffect(() => { (async () => { @@ -76,4 +84,12 @@ function App({ Component, pageProps }: AppProps) { ); } +App.getInitialProps = async ({ ctx }: { ctx: NextPageContext }) => { + const reg = /mobile/gi; + + const isPc = !reg.test(ctx.req?.headers?.['user-agent'] || ''); + + return { isPc }; +}; + export default App; diff --git a/client/src/pages/_error.tsx b/client/src/pages/_error.tsx index 09b006b6e..de75ac7cc 100644 --- a/client/src/pages/_error.tsx +++ b/client/src/pages/_error.tsx @@ -1,4 +1,13 @@ +import { useEffect } from 'react'; +import { useRouter } from 'next/router'; function Error() { + const router = useRouter(); + useEffect(() => { + setTimeout(() => { + router.replace('/app/list'); + }, 2000); + }, []); + return (

部分系统不兼容,导致页面崩溃。如果可以,请联系作者,反馈下具体操作和页面。大部分是 苹果 的 diff --git a/client/src/pages/chat/share.tsx b/client/src/pages/chat/share.tsx index 2912d44dc..de4cc21e0 100644 --- a/client/src/pages/chat/share.tsx +++ b/client/src/pages/chat/share.tsx @@ -90,7 +90,9 @@ const ShareChat = ({ shareId, historyId }: { shareId: string; historyId: string const loadAppInfo = useCallback( async (shareId: string, historyId: string) => { - if (!shareId || !historyId) return null; + console.log(shareId, historyId); + + if (!shareId) return null; const history = shareChatHistory.find((item) => item._id === historyId) || defaultHistory; ChatBoxRef.current?.resetHistory(history.chats); @@ -129,9 +131,9 @@ const ShareChat = ({ shareId, historyId }: { shareId: string; historyId: string [delManyShareChatHistoryByShareId, setShareChatData, shareChatData, shareChatHistory, toast] ); - useEffect(() => { - loadAppInfo(shareId, historyId); - }, [shareId, historyId]); + useQuery(['init', shareId, historyId], () => { + return loadAppInfo(shareId, historyId); + }); return ( @@ -168,7 +170,6 @@ const ShareChat = ({ shareId, historyId }: { shareId: string; historyId: string } }} onDelHistory={delOneShareHistoryByHistoryId} - onCloseSlider={onCloseSlider} /> )} diff --git a/client/src/pages/kb/detail/components/Info.tsx b/client/src/pages/kb/detail/components/Info.tsx index f2b879d73..69c5ec40e 100644 --- a/client/src/pages/kb/detail/components/Info.tsx +++ b/client/src/pages/kb/detail/components/Info.tsx @@ -145,6 +145,12 @@ const Info = ( return ( + + + 知识库 ID + + {kbDetail._id} + 知识库头像 diff --git a/client/src/store/global.ts b/client/src/store/global.ts index 916908455..425c4d982 100644 --- a/client/src/store/global.ts +++ b/client/src/store/global.ts @@ -7,7 +7,8 @@ type State = { setLoading: (val: boolean) => null; screenWidth: number; setScreenWidth: (val: number) => void; - isPc: boolean; + isPc?: boolean; + initIsPc(val: boolean): void; }; export const useGlobalStore = create()( @@ -27,7 +28,14 @@ export const useGlobalStore = create()( state.isPc = val < 900 ? false : true; }); }, - isPc: false + isPc: undefined, + initIsPc(val: boolean) { + if (get().isPc !== undefined) return; + + set((state) => { + state.isPc = val; + }); + } })) ) );