From d44e9fe9aed4edaf8be4285fac207d290eaf8aa5 Mon Sep 17 00:00:00 2001 From: zimplexing Date: Tue, 15 Jul 2025 21:41:38 +0800 Subject: [PATCH] feat: enhance login status management and improve error logging across services --- app/_layout.tsx | 14 ++++++++---- app/search.tsx | 2 +- backend/src/config/index.ts | 2 +- backend/src/routes/image-proxy.ts | 2 +- components/LoginModal.tsx | 4 ++-- components/VideoCard.tv.tsx | 2 +- services/api.ts | 2 -- services/m3u.ts | 2 +- services/remoteControlService.ts | 10 ++++---- services/storage.ts | 6 ++--- services/tcpHttpServer.ts | 10 ++++---- stores/authStore.ts | 38 ++++++++++++++++--------------- stores/detailStore.ts | 2 +- stores/homeStore.ts | 9 +++++--- stores/playerStore.ts | 4 ++-- stores/remoteControlStore.ts | 2 +- stores/settingsStore.ts | 8 +++---- 17 files changed, 62 insertions(+), 57 deletions(-) diff --git a/app/_layout.tsx b/app/_layout.tsx index 7db48cf..4041c1f 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -19,15 +19,19 @@ export default function RootLayout() { const [loaded, error] = useFonts({ SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"), }); - const { loadSettings, remoteInputEnabled } = useSettingsStore(); + const { loadSettings, remoteInputEnabled, apiBaseUrl } = useSettingsStore(); const { startServer, stopServer } = useRemoteControlStore(); const { checkLoginStatus } = useAuthStore(); useEffect(() => { - loadSettings().then(() => { - checkLoginStatus(); - }); - }, [loadSettings, checkLoginStatus]); + loadSettings(); + }, [loadSettings]); + + useEffect(() => { + if (apiBaseUrl) { + checkLoginStatus(apiBaseUrl); + } + }, [apiBaseUrl, checkLoginStatus]); useEffect(() => { if (loaded || error) { diff --git a/app/search.tsx b/app/search.tsx index f7abf8c..9672b70 100644 --- a/app/search.tsx +++ b/app/search.tsx @@ -59,7 +59,7 @@ export default function SearchScreen() { } } catch (err) { setError("搜索失败,请稍后重试。"); - console.error("Search failed:", err); + console.info("Search failed:", err); } finally { setLoading(false); } diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 8d75ce1..2a713ed 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -54,7 +54,7 @@ let cachedConfig: Config; try { cachedConfig = JSON.parse(fs.readFileSync(configPath, "utf-8")) as Config; } catch (error) { - console.error(`Error reading or parsing config.json at ${configPath}`, error); + console.info(`Error reading or parsing config.json at ${configPath}`, error); // Provide a default fallback config to prevent crashes cachedConfig = { api_site: {}, diff --git a/backend/src/routes/image-proxy.ts b/backend/src/routes/image-proxy.ts index e2ba20f..9c8d4b6 100644 --- a/backend/src/routes/image-proxy.ts +++ b/backend/src/routes/image-proxy.ts @@ -35,7 +35,7 @@ router.get("/", async (req: Request, res: Response) => { res.status(500).send("Image response has no body"); } } catch (error) { - console.error("Image proxy error:", error); + console.info("Image proxy error:", error); res.status(500).send("Error fetching image"); } }); diff --git a/components/LoginModal.tsx b/components/LoginModal.tsx index c7a182d..077f569 100644 --- a/components/LoginModal.tsx +++ b/components/LoginModal.tsx @@ -11,7 +11,7 @@ import { StyledButton } from "./StyledButton"; const LoginModal = () => { const { isLoginModalVisible, hideLoginModal, checkLoginStatus } = useAuthStore(); - const { serverConfig } = useSettingsStore(); + const { serverConfig, apiBaseUrl } = useSettingsStore(); const { refreshPlayRecords } = useHomeStore(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); @@ -28,7 +28,7 @@ const LoginModal = () => { setIsLoading(true); try { await api.login(isLocalStorage ? undefined : username, password); - await checkLoginStatus(); + await checkLoginStatus(apiBaseUrl); await refreshPlayRecords(); Toast.show({ type: "success", text1: "登录成功" }); hideLoginModal(); diff --git a/components/VideoCard.tv.tsx b/components/VideoCard.tv.tsx index 3fca8b5..f50c556 100644 --- a/components/VideoCard.tv.tsx +++ b/components/VideoCard.tv.tsx @@ -112,7 +112,7 @@ export default function VideoCard({ router.replace("/"); } } catch (error) { - console.error("Failed to delete play record:", error); + console.info("Failed to delete play record:", error); Alert.alert("错误", "删除观看记录失败,请重试"); } }, diff --git a/services/api.ts b/services/api.ts index a736f66..d1b90b5 100644 --- a/services/api.ts +++ b/services/api.ts @@ -1,4 +1,3 @@ -import useAuthStore from "@/stores/authStore"; // region: --- Interface Definitions --- export interface DoubanItem { @@ -97,7 +96,6 @@ export class API { const response = await fetch(`${this.baseURL}${url}`, options); if (response.status === 401) { - useAuthStore.getState().showLoginModal(); throw new Error("UNAUTHORIZED"); } diff --git a/services/m3u.ts b/services/m3u.ts index 5c2c090..a2a65c5 100644 --- a/services/m3u.ts +++ b/services/m3u.ts @@ -47,7 +47,7 @@ export const fetchAndParseM3u = async (m3uUrl: string): Promise => { const m3uText = await response.text(); return parseM3U(m3uText); } catch (error) { - console.error("Error fetching or parsing M3U:", error); + console.info("Error fetching or parsing M3U:", error); return []; // Return empty array on error } }; diff --git a/services/remoteControlService.ts b/services/remoteControlService.ts index e1f8ab8..b5f7868 100644 --- a/services/remoteControlService.ts +++ b/services/remoteControlService.ts @@ -25,7 +25,7 @@ const getRemotePageHTML = () => {