diff --git a/app/(guest)/unified-auth-login.tsx b/app/(guest)/unified-auth-login.tsx index b1d7669..abbb3e7 100644 --- a/app/(guest)/unified-auth-login.tsx +++ b/app/(guest)/unified-auth-login.tsx @@ -68,8 +68,8 @@ const UnifiedLoginPage: React.FC = () => { console.log('登录成功:', name, accessToken); - // 跳转到一码通页面 - router.push('/qrcode'); + // 跳转到上一页 + router.back(); } catch (error: any) { console.error(error); Alert.alert('登录失败', error.message); diff --git a/app/(root)/(tabs)/qrcode.tsx b/app/(root)/(tabs)/qrcode.tsx index 7610898..426c8ee 100644 --- a/app/(root)/(tabs)/qrcode.tsx +++ b/app/(root)/(tabs)/qrcode.tsx @@ -4,8 +4,8 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Text } from '@/components/ui/text'; import YMTLogin, { IdentifyRespData, PayCodeRespData } from '@/lib/ymt-login'; import AsyncStorage from '@react-native-async-storage/async-storage'; -import { router } from 'expo-router'; -import { useCallback, useEffect, useMemo, useState } from 'react'; +import { router, useFocusEffect } from 'expo-router'; +import { useCallback, useMemo, useState } from 'react'; import { Alert, View } from 'react-native'; import QRCode from 'react-native-qrcode-svg'; export default function YiMaTongPage() { @@ -33,51 +33,40 @@ export default function YiMaTongPage() { } }, [accessToken, ymtLogin]); - // 初始化时读取本地数据 - useEffect(() => { - // 读取本地数据 - async function getLocalData() { - try { - const storedAccessToken = await AsyncStorage.getItem('accessToken'); - const storedName = await AsyncStorage.getItem('name'); + // 获取焦点时读取本地数据(初始化时,Tab切换时,登录页返回时) + useFocusEffect( + useCallback(() => { + // 读取本地数据 + async function getLocalData() { + try { + const storedAccessToken = await AsyncStorage.getItem('accessToken'); + const storedName = await AsyncStorage.getItem('name'); - setAccessToken(storedAccessToken); - setName(storedName); + setAccessToken(storedAccessToken); + setName(storedName); - console.log('读取本地数据成功:', storedAccessToken); - refresh(); - } catch (error) { - console.error('读取本地数据失败:', error); - await AsyncStorage.removeItem('accessToken'); - } - } - getLocalData(); - // 设置刷新和时间间隔 - const refreshInterval = setInterval(() => { - if (accessToken) { - refresh(); + console.log('读取本地数据成功:', storedAccessToken); + refresh(); + } catch (error) { + console.error('读取本地数据失败:', error); + await AsyncStorage.removeItem('accessToken'); + } } - }, 50000); - const timeInterval = setInterval(() => setCurrentTime(new Date()), 1000); + getLocalData(); + // 设置刷新和时间间隔 + const refreshInterval = setInterval(() => { + if (accessToken) { + refresh(); + } + }, 50000); + const timeInterval = setInterval(() => setCurrentTime(new Date()), 1000); - return () => { - clearInterval(refreshInterval); - clearInterval(timeInterval); - }; - }, [accessToken, refresh]); - - // accessToken 变化时自动刷新 - useEffect(() => { - const checkLoginStatus = async () => { - const token = await AsyncStorage.getItem('accessToken'); - if (!token) { - // 未登录,跳转到登录页面 - router.push('/(guest)/unified-auth-login'); - } - }; - checkLoginStatus(); - refresh(); - }, [refresh, accessToken]); + return () => { + clearInterval(refreshInterval); + clearInterval(timeInterval); + }; + }, [accessToken, refresh]), + ); async function logout() { Alert.alert( @@ -120,7 +109,7 @@ export default function YiMaTongPage() { - @@ -130,38 +119,47 @@ export default function YiMaTongPage() { return ( {/* Tabs 组件 */} - - {/* Tabs 内容区域 */} - - - {PayCodes && renderQRCodeCard('消费码', PayCodes[0].prePayId, '#000000')} - + {accessToken && ( + + {/* Tabs 内容区域 */} + + + {PayCodes && renderQRCodeCard('消费码', PayCodes[0].prePayId, '#000000')} + - - {IdentifyCode && renderQRCodeCard('认证码', IdentifyCode.content, IdentifyCode.color)} - - + + {IdentifyCode && renderQRCodeCard('认证码', IdentifyCode.content, IdentifyCode.color)} + + + + {/* Tabs 切换按钮 */} + + + 消费码 + + + 认证码 + + + + )} - {/* Tabs 切换按钮 */} - - - 消费码 - - - 认证码 - - - + {/* 登录提示 */} + {!accessToken && ( + + 登录统一身份认证平台,享受一码通服务 + + + )} ); }