diff --git a/src/App.svelte b/src/App.svelte index 1463ce5..ea93f13 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -21,6 +21,7 @@ // 状态管理 let isAuthenticated = $state(false); + let isInitializing = $state(true); // 新增:初始化状态 let username = $state(''); let password = $state(''); let error = $state(''); @@ -45,6 +46,42 @@ { id: 'other', label: '其他' } ]; + // 初始化函数:检查认证状态 + async function initializeAuth() { + const auth = localStorage.getItem('auth'); + if (auth) { + try { + // 使用 page 接口验证登录状态 + const response = await request.get>('/media/page', { + params: { + type: 'game', + currentPage: 1, + pageSize: 10 + } + }); + + if (response.data.code === 0) { + isAuthenticated = true; + error = ''; + // 设置初始数据 + mediaList = response.data.data.list; + totalItems = response.data.data.total; + } else { + // 如果认证失败,清除存储的认证信息 + localStorage.removeItem('auth'); + } + } catch (e: any) { + // 如果请求失败,清除存储的认证信息 + localStorage.removeItem('auth'); + error = e.message || 'Connection error'; + } + } + isInitializing = false; // 初始化完成 + } + + // 页面加载时初始化认证状态 + initializeAuth(); + // 获取媒体列表 async function fetchMediaList() { loading = true; @@ -138,7 +175,14 @@ My Score -{#if !isAuthenticated} +{#if isInitializing} +
+
+
+

加载中...

+
+
+{:else if !isAuthenticated}