fix:splash 界面重定向界面问题
This commit is contained in:
@@ -4,6 +4,9 @@ import { useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import { MobileShell } from "@/app/_components/core/mobile-shell";
|
||||
import { useAuthDispatch, useAuthState } from "@/stores/auth/auth-context";
|
||||
import { useChatDispatch } from "@/stores/chat/chat-context";
|
||||
import { useUserDispatch } from "@/stores/user/user-context";
|
||||
import { AuthStorage } from "@/data/storage/auth/auth_storage";
|
||||
import { ROUTES } from "@/router/routes";
|
||||
|
||||
@@ -15,29 +18,24 @@ import styles from "./splash-screen.module.css";
|
||||
|
||||
export function SplashScreen() {
|
||||
const router = useRouter();
|
||||
const state = useAuthState();
|
||||
const authDispatch = useAuthDispatch();
|
||||
const chatDispatch = useChatDispatch();
|
||||
const userDispatch = useUserDispatch();
|
||||
|
||||
// 启动时检查 localStorage 里的 token:
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// useEffect ① 启动时检查 localStorage 里的 loginToken:
|
||||
// - loginToken(OAuth/email sync 写下的)→ 视为"已登录"跳 /chat
|
||||
// - guestToken(用户**显式**走"游客模式"留下的)→ 同样视为"已登录"跳 /chat
|
||||
// - 都没有 → 留在 splash(**不**自动创建游客账号 —— 见 auth-machine.ts 的 checkAuthStatusActor)
|
||||
//
|
||||
// - **不**查 guestToken —— 游客用户**停留 splash**(按你要求)
|
||||
// - 都没有 → 留 splash
|
||||
// proxy 读不到 localStorage(Edge runtime),所以这条 fallback 必须在 Client 侧。
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
const storage = AuthStorage.getInstance();
|
||||
// 用 getXxxToken() 拿 string | null(不是 hasXxxToken() 拿 boolean)
|
||||
const [loginR, guestR] = await Promise.all([
|
||||
storage.getLoginToken(),
|
||||
storage.getGuestToken(),
|
||||
]);
|
||||
const loginTok = loginR.success ? loginR.data : null;
|
||||
const guestTok = guestR.success ? guestR.data : null;
|
||||
const hasAny =
|
||||
(loginTok != null && loginTok.length > 0) ||
|
||||
(guestTok != null && guestTok.length > 0);
|
||||
|
||||
if (!cancelled && hasAny) {
|
||||
// 用 getLoginToken() 拿 string | null(不是 hasLoginToken() 拿 boolean)
|
||||
const r = await AuthStorage.getInstance().getLoginToken();
|
||||
if (!cancelled && r.success && r.data) {
|
||||
router.replace(ROUTES.chat);
|
||||
}
|
||||
})();
|
||||
@@ -46,6 +44,41 @@ export function SplashScreen() {
|
||||
};
|
||||
}, [router]);
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// useEffect ② 按钮**刚**派了事件(pendingRedirect=true) +
|
||||
// loginStatus 是非 notLoggedIn → 跳 /chat
|
||||
//
|
||||
// **关键**:`pendingRedirect` 是 auth context 里的**持久**状态(不是 ref)——
|
||||
// re-mount 时**不**重置,区分"刚按了"和"历史登录"准确无误。
|
||||
// 跳完调 `AuthClearPendingRedirect` 置 false(一次性信号)。
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
useEffect(() => {
|
||||
// 调试日志(保留,不删 —— 用于排查"re-mount 误跳 /chat"问题)
|
||||
console.log("[splash] useEffect ② (loginStatus + pendingRedirect)", {
|
||||
pending: state.pendingRedirect,
|
||||
loginStatus: state.loginStatus,
|
||||
willRedirect:
|
||||
state.pendingRedirect && state.loginStatus !== "notLoggedIn",
|
||||
});
|
||||
|
||||
if (state.pendingRedirect && state.loginStatus !== "notLoggedIn") {
|
||||
console.log(
|
||||
"[splash] useEffect ② → router.replace(/chat) [via pendingRedirect flag]",
|
||||
);
|
||||
authDispatch({ type: "AuthClearPendingRedirect" });
|
||||
chatDispatch({ type: "ChatAuthStatusChanged" });
|
||||
userDispatch({ type: "UserInit" });
|
||||
router.replace(ROUTES.chat);
|
||||
}
|
||||
}, [
|
||||
state.loginStatus,
|
||||
state.pendingRedirect,
|
||||
authDispatch,
|
||||
chatDispatch,
|
||||
userDispatch,
|
||||
router,
|
||||
]);
|
||||
|
||||
return (
|
||||
<MobileShell background="var(--color-sidebar-background)">
|
||||
<div className={styles.wrapper}>
|
||||
|
||||
Reference in New Issue
Block a user