diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index 5fd34c8..42103ec 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -16,7 +16,7 @@ export default function HomeScreen() { setIsLoading(true); checkAuthStatus(router, () => { router.replace('/ask') - }).then(() => { + }, false).then(() => { setIsLoading(false); }); }, []); diff --git a/app/(tabs)/top.tsx b/app/(tabs)/top.tsx index e6d8015..e1bf4a0 100644 --- a/app/(tabs)/top.tsx +++ b/app/(tabs)/top.tsx @@ -109,8 +109,6 @@ export default function OwnerPage() { // 当用户选择发生变化时,重新获取排名 useEffect(() => { - console.log('selectedLocation', selectedLocation); - console.log('selectedClassify', selectedClassify); if (selectedLocation?.length > 0 && selectedClassify?.length > 0) { getRanking(); } @@ -151,8 +149,6 @@ export default function OwnerPage() { return item.name === JSON.parse(location || "").city }) || [], JSON.parse(location || "").district); if (location) { - console.log("xuhuiElement", xuhuiElement); - setSelectedLocation([xuhuiElement]); } } diff --git a/components/owner/ranking.tsx b/components/owner/ranking.tsx index ea797ee..00ac830 100644 --- a/components/owner/ranking.tsx +++ b/components/owner/ranking.tsx @@ -23,7 +23,7 @@ const Ranking = ({ data }: { data: TitleRankings[] }) => { data={data} showsVerticalScrollIndicator={false} contentContainerStyle={{ width: "100%" }} - keyExtractor={(item) => item.display_name} + keyExtractor={(item, index) => `${item.display_name}-${index}`} renderItem={({ item }) => ( No.{item.ranking} diff --git a/lib/auth.ts b/lib/auth.ts index e4d4913..56ede38 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -1,7 +1,7 @@ import { useRouter } from 'expo-router'; import * as SecureStore from 'expo-secure-store'; import { Platform } from 'react-native'; -import { API_ENDPOINT } from './server-api-util'; +import { API_ENDPOINT, refreshAuthToken } from './server-api-util'; export async function identityCheck(token: string) { @@ -12,14 +12,18 @@ export async function identityCheck(token: string) { }, }); const data = await res.json(); + if (data.code != 0) { + await refreshAuthToken("Token expired"); + } return data.code == 0; } /** * 检查登录态,未登录自动跳转到 /login,已登录可执行回调。 * @param onAuthed 已登录时的回调(可选) + * @param login 是否跳转登录页(可选) */ -export async function checkAuthStatus(router: ReturnType, onAuthed?: () => Promise | void) { +export async function checkAuthStatus(router: ReturnType, onAuthed?: () => Promise | void, login: boolean = true) { let token: string | null = ''; if (Platform.OS === 'web') { token = localStorage.getItem('token') || ''; @@ -28,11 +32,11 @@ export async function checkAuthStatus(router: ReturnType, onAu } const loggedIn = !!token && await identityCheck(token); - if (!loggedIn) { + if (!loggedIn && login) { router.replace('/login'); return false; } - if (onAuthed) { + if (onAuthed && loggedIn) { await onAuthed(); } return true; diff --git a/lib/server-api-util.ts b/lib/server-api-util.ts index b122f7f..8331343 100644 --- a/lib/server-api-util.ts +++ b/lib/server-api-util.ts @@ -64,15 +64,14 @@ export const refreshAuthToken = async(message: string | null): Promise cookie = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') || "")?.refresh_token || "" : ""; userId = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') || "")?.user_id || "" : ""; } else { - await SecureStore.getItemAsync('user').then((user: User) => { - cookie = user?.refresh_token || ""; - userId = user?.user_id || ""; + await SecureStore.getItemAsync('user').then((user: string | null) => { + cookie = JSON.parse(user || "")?.refresh_token || ""; + userId = JSON.parse(user || "")?.user_id || ""; }) } // 退出刷新会重新填充数据 let response; - response = await fetch(`${API_ENDPOINT}/v1/iam/access-token-refresh`, { method: "POST", body: JSON.stringify({ @@ -97,6 +96,13 @@ export const refreshAuthToken = async(message: string | null): Promise user: userData, token: userData.access_token })); + if (Platform.OS === 'web') { + localStorage.setItem('user', JSON.stringify(userData)); + localStorage.setItem('token', userData.access_token); + } else { + SecureStore.setItemAsync('user', JSON.stringify(userData)); + SecureStore.setItemAsync('token', userData.access_token); + } } return userData;