feat(auth): 添加401未授权统一处理机制

- 在API服务层实现401状态码的统一拦截和处理
- 添加防抖机制,避免短时间内重复处理401错误
- 支持应用层注册自定义的未授权处理器
- 在应用启动时注册401处理器,自动清除登录状态并跳转到登录页
- 同时处理普通请求和流式请求的401响应
This commit is contained in:
richarjiang
2025-11-18 15:59:47 +08:00
parent 21e57634e0
commit 9d424c7bd2
2 changed files with 120 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
import '@/i18n';
import { DefaultTheme, ThemeProvider } from '@react-navigation/native';
import { useFonts } from 'expo-font';
import { Stack } from 'expo-router';
import { Stack, useRouter } from 'expo-router';
import { StatusBar } from 'expo-status-bar';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import 'react-native-reanimated';
@@ -19,7 +19,7 @@ import { WaterRecordSource } from '@/services/waterRecords';
import { workoutMonitorService } from '@/services/workoutMonitor';
import { store } from '@/store';
import { hydrateActiveSchedule, selectActiveFastingSchedule } from '@/store/fastingSlice';
import { fetchMyProfile, setPrivacyAgreed } from '@/store/userSlice';
import { fetchMyProfile, logout, setPrivacyAgreed } from '@/store/userSlice';
import { createWaterRecordAction } from '@/store/waterSlice';
import { loadActiveFastingSchedule } from '@/utils/fasting';
import { initializeHealthPermissions } from '@/utils/health';
@@ -31,7 +31,7 @@ import { DialogProvider } from '@/components/ui/DialogProvider';
import { MembershipModalProvider } from '@/contexts/MembershipModalContext';
import { ToastProvider } from '@/contexts/ToastContext';
import { useAuthGuard } from '@/hooks/useAuthGuard';
import { STORAGE_KEYS } from '@/services/api';
import { STORAGE_KEYS, setUnauthorizedHandler } from '@/services/api';
import { BackgroundTaskManager } from '@/services/backgroundTaskManagerV2';
import { fetchChallenges } from '@/store/challengesSlice';
import AsyncStorage from '@/utils/kvStore';
@@ -52,6 +52,7 @@ if (__DEV__) {
function Bootstrapper({ children }: { children: React.ReactNode }) {
const dispatch = useAppDispatch();
const router = useRouter();
const { profile, onboardingCompleted } = useAppSelector((state) => state.user);
const activeFastingSchedule = useAppSelector(selectActiveFastingSchedule);
const [showPrivacyModal, setShowPrivacyModal] = React.useState(false);
@@ -62,6 +63,28 @@ function Bootstrapper({ children }: { children: React.ReactNode }) {
// 初始化快捷动作处理
useQuickActions();
// 注册401未授权处理器应用启动时执行一次
React.useEffect(() => {
const handle401 = async () => {
try {
logger.info('[401处理] 开始处理登录过期');
// 清除Redux状态
await dispatch(logout());
// 跳转到登录页
router.push('/auth/login');
logger.info('[401处理] 登录过期处理完成');
} catch (error) {
logger.error('[401处理] 处理失败:', error);
}
};
setUnauthorizedHandler(handle401);
logger.info('[401处理器] 已注册到API服务');
}, [dispatch, router]);
React.useEffect(() => {
if (fastingHydrationRequestedRef.current) return;
if (activeFastingSchedule) {