From 60430f821612a27b6dd382204ed8c2b6092d765e Mon Sep 17 00:00:00 2001 From: "jif2.zhang" Date: Tue, 15 Sep 2026 09:32:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20APP=20=E4=B8=8D=E5=86=8D=E6=8E=A5?= =?UTF-8?q?=E5=85=A5=E9=BB=98=E8=AE=A4=E8=B7=AF=E5=BE=84=20/,=20=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=BF=9B=E7=99=BB=E5=BD=95=E6=88=96=E5=86=85=E9=83=A8?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 RootGate: Tauri 环境下访问 / 时, 未登录重定向 /login, 已登录重定向 /home; web 浏览器仍走 Landing 作为网络入口 - 同步读取 localStorage 已保存会话, 避免已登录用户闪一下登录页 - ota.ts 导出 inTauri 检测, App.tsx 复用 --- src/App.tsx | 27 +++++++++++++++++++++++++-- src/api/ota.ts | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 9438c42..4a47f2f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import React, { useEffect } from "react"; import { BrowserRouter, Routes, Route, Navigate, useNavigate } from "react-router-dom"; import { useAppStore } from "./stores/app"; -import { initOta } from "./api/ota"; +import { initOta, inTauri } from "./api/ota"; import Login from "./pages/Login"; import Landing from "./pages/Landing"; import Home from "./pages/Home"; @@ -19,6 +19,29 @@ function ProtectedRoute({ children }: { children: React.ReactNode }) { return <>{children}; } +// 同步读取已保存的会话 (避免首帧闪烁: RouterGate 的 effect 是异步的) +function getStoredUser(): unknown { + try { + const saved = localStorage.getItem("wardrobe_user"); + if (!saved) return null; + const p = JSON.parse(saved); + return p?.user ?? null; + } catch { + return null; + } +} + +// 默认路径 /: web 是网络入口(落地页); APP (Tauri) 不进 Landing, +// 未登录直接去 /login, 已登录直接进内部页面 /home +function RootGate() { + const user = useAppStore((s) => s.user); + if (inTauri) { + const effective = user ?? getStoredUser(); + return ; + } + return ; +} + function RouterGate() { const user = useAppStore((s) => s.user); const navigate = useNavigate(); @@ -41,7 +64,7 @@ function RouterGate() { return ( - } /> + } /> } /> }> } /> diff --git a/src/api/ota.ts b/src/api/ota.ts index 1525084..08e5149 100644 --- a/src/api/ota.ts +++ b/src/api/ota.ts @@ -8,7 +8,7 @@ import { APP_VERSION, BUILD_ID } from "../version"; // 判断是否在 Tauri WebView 内 (有 __TAURI_INTERNALS__ 即 Tauri 环境) -const inTauri = typeof window !== "undefined" && "__TAURI_INTERNALS__" in window; +export const inTauri = typeof window !== "undefined" && "__TAURI_INTERNALS__" in window; // 拼接绝对 URL: web 模式同源 (OTA 服务端同时托管 dist), Tauri 不走这里 function resolveUrl(p: string) {