From 44ff7ce36d016c38e6918b916cc642f98f2b85cb Mon Sep 17 00:00:00 2001 From: jinyaqiu Date: Thu, 17 Jul 2025 20:16:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A0=B7=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/(tabs)/index.tsx | 157 ++++++++++------------------------ app/(tabs)/owner.tsx | 7 +- components/owner/resource.tsx | 8 +- 3 files changed, 54 insertions(+), 118 deletions(-) diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index dccdf2a..5fd34c8 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -1,88 +1,26 @@ import IP from '@/assets/icons/svg/ip.svg'; -import * as MediaLibrary from 'expo-media-library'; +import { checkAuthStatus } from '@/lib/auth'; import { useRouter } from 'expo-router'; -import * as SecureStore from 'expo-secure-store'; -import React, { useEffect, useRef, useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { Platform, Text, TouchableOpacity, View } from 'react-native'; +import { Text, TouchableOpacity, View } from 'react-native'; import { useSafeAreaInsets } from "react-native-safe-area-context"; -import MemoList from './memo-list'; export default function HomeScreen() { const router = useRouter(); const { t } = useTranslation(); const insets = useSafeAreaInsets(); const [isLoading, setIsLoading] = useState(true); - const [isLoggedIn, setIsLoggedIn] = useState(false); - const [token, setToken] = useState(''); - const tokenInterval = useRef(null); - const isMounted = useRef(true); - - const getAuthToken = async (): Promise => { - let tokenValue = ''; - if (Platform.OS === 'web') { - tokenValue = localStorage.getItem('token') || ''; - } else { - tokenValue = (await SecureStore.getItemAsync('token')) || ''; - } - setToken(tokenValue); // 只在获取到新token时更新状态 - return tokenValue; - }; useEffect(() => { - const checkAuthStatus = async () => { - try { - const loggedIn = !!token; - setIsLoggedIn(loggedIn); - - if (loggedIn) { - // 已登录,请求必要的权限 - const { status } = await MediaLibrary.requestPermissionsAsync(); - if (status === 'granted') { - console.log('Media library permission granted in HomeScreen.'); - } - router.replace('/ask'); - } - setIsLoading(false); - } catch (error) { - console.error('检查登录状态出错:', error); - setIsLoading(false); - } finally { - setIsLoading(false); - } - }; - checkAuthStatus(); + setIsLoading(true); + checkAuthStatus(router, () => { + router.replace('/ask') + }).then(() => { + setIsLoading(false); + }); }, []); - // 轮询获取token - useEffect(() => { - // 如果已经有token,直接返回 - if (token) { - if (tokenInterval.current) { - clearInterval(tokenInterval.current); - } - return; - } - if (!tokenInterval.current) return; - // 设置轮询 - tokenInterval.current = setInterval(async () => { - if (isMounted.current) { - const currentToken = await getAuthToken(); - // 如果获取到token,清除定时器 - if (currentToken && tokenInterval.current) { - clearInterval(tokenInterval.current); - } - } - }, 5000); - - // 返回清理函数 - return () => { - if (tokenInterval.current) { - clearInterval(tokenInterval.current); - } - }; - }, [token]); // 添加token作为依赖 - if (isLoading) { return ( @@ -93,49 +31,46 @@ export default function HomeScreen() { return ( - { - isLoggedIn ? : - - {/* 标题区域 */} - - - {t('auth.welcomeAwaken.awaken', { ns: 'login' })} - {"\n"} - {t('auth.welcomeAwaken.your', { ns: 'login' })} - {"\n"} - {t('auth.welcomeAwaken.pm', { ns: 'login' })} - - - {t('auth.welcomeAwaken.slogan', { ns: 'login' })} - - + + {/* 标题区域 */} + + + {t('auth.welcomeAwaken.awaken', { ns: 'login' })} + {"\n"} + {t('auth.welcomeAwaken.your', { ns: 'login' })} + {"\n"} + {t('auth.welcomeAwaken.pm', { ns: 'login' })} + + + {t('auth.welcomeAwaken.slogan', { ns: 'login' })} + + - {/* Memo 形象区域 */} - - - + {/* Memo 形象区域 */} + + + - {/* 介绍文本 */} - - {t('auth.welcomeAwaken.gallery', { ns: 'login' })} - {"\n"} - {t('auth.welcomeAwaken.back', { ns: 'login' })} - + {/* 介绍文本 */} + + {t('auth.welcomeAwaken.gallery', { ns: 'login' })} + {"\n"} + {t('auth.welcomeAwaken.back', { ns: 'login' })} + - {/* 唤醒按钮 */} - { - router.push('/login') - }} - activeOpacity={0.8} - > - - {t('auth.welcomeAwaken.awake', { ns: 'login' })} - - - - } + {/* 唤醒按钮 */} + { + router.push('/login') + }} + activeOpacity={0.8} + > + + {t('auth.welcomeAwaken.awake', { ns: 'login' })} + + + ); } \ No newline at end of file diff --git a/app/(tabs)/owner.tsx b/app/(tabs)/owner.tsx index fd3812e..19dcac5 100644 --- a/app/(tabs)/owner.tsx +++ b/app/(tabs)/owner.tsx @@ -89,11 +89,13 @@ export default function OwnerPage() { {/* 资源数据 */} - + } isFormatBytes={true} /> } /> - + + + {/* 数据统计 */}