2026-06-16 19:03:26 +08:00
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
import { ActivityIndicator, View } from 'react-native';
|
2026-03-24 14:21:31 +08:00
|
|
|
|
import { Redirect } from 'expo-router';
|
|
|
|
|
|
|
|
|
|
|
|
import { AppRouteStack } from '../../src/app-navigation/AppRouteStack';
|
|
|
|
|
|
import { messageManager, useAuthStore } from '../../src/stores';
|
2026-06-12 23:42:09 +08:00
|
|
|
|
import { useRegisterPushDevice } from '../../src/hooks';
|
|
|
|
|
|
import { hrefAuthLogin } from '../../src/navigation/hrefs';
|
2026-06-16 19:03:26 +08:00
|
|
|
|
import { useAppColors } from '../../src/theme';
|
2026-03-24 14:21:31 +08:00
|
|
|
|
|
|
|
|
|
|
export default function AppLayout() {
|
|
|
|
|
|
const isAuthenticated = useAuthStore((s) => s.isAuthenticated);
|
2026-06-16 19:03:26 +08:00
|
|
|
|
const fetchCurrentUser = useAuthStore((s) => s.fetchCurrentUser);
|
2026-04-27 23:21:08 +08:00
|
|
|
|
const userID = useAuthStore((s) => s.currentUser?.id);
|
2026-06-16 19:03:26 +08:00
|
|
|
|
const colors = useAppColors();
|
|
|
|
|
|
const [verified, setVerified] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
// 冷启动 token 校验:仅鉴权分组 (app) 需要。
|
|
|
|
|
|
// 公开页(/privacy、/terms)和 (auth) 分组在根布局直接渲染,不受此校验影响,
|
|
|
|
|
|
// 因此冷启动 401 不会把公开页顶到 /login。
|
|
|
|
|
|
fetchCurrentUser().finally(() => setVerified(true));
|
|
|
|
|
|
}, [fetchCurrentUser]);
|
|
|
|
|
|
|
|
|
|
|
|
useRegisterPushDevice(isAuthenticated, userID);
|
2026-03-24 14:21:31 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
messageManager.initialize();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-06-16 19:03:26 +08:00
|
|
|
|
// 持久化状态显示已登录则立即渲染(后台静默校验)
|
|
|
|
|
|
if (isAuthenticated) {
|
|
|
|
|
|
return <AppRouteStack />;
|
|
|
|
|
|
}
|
2026-04-27 23:21:08 +08:00
|
|
|
|
|
2026-06-16 19:03:26 +08:00
|
|
|
|
// 未登录且校验未完成,显示 loading
|
|
|
|
|
|
if (!verified) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<View
|
|
|
|
|
|
style={{
|
|
|
|
|
|
flex: 1,
|
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
backgroundColor: colors.background.default,
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<ActivityIndicator size="large" color={colors.primary.main} />
|
|
|
|
|
|
</View>
|
|
|
|
|
|
);
|
2026-03-24 14:21:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-16 19:03:26 +08:00
|
|
|
|
// 校验完成仍未登录,跳转登录页
|
|
|
|
|
|
return <Redirect href={hrefAuthLogin()} />;
|
2026-06-12 23:42:09 +08:00
|
|
|
|
}
|