feat: 登录态跳转到ask页面
This commit is contained in:
parent
b0fde981e6
commit
c022e7f92f
@ -1,96 +1,108 @@
|
|||||||
import IP from '@/assets/icons/svg/ip.svg';
|
import IP from '@/assets/icons/svg/ip.svg';
|
||||||
import { registerBackgroundUploadTask, triggerManualUpload } from '@/components/file-upload/backgroundUploader';
|
import { registerBackgroundUploadTask, triggerManualUpload } from '@/components/file-upload/backgroundUploader';
|
||||||
import Lottie from '@/components/lottie/lottie';
|
|
||||||
import MessagePush from '@/components/message-push';
|
|
||||||
import * as MediaLibrary from 'expo-media-library';
|
import * as MediaLibrary from 'expo-media-library';
|
||||||
import { useRouter } from 'expo-router';
|
import { useRouter } from 'expo-router';
|
||||||
import * as SecureStore from 'expo-secure-store';
|
import * as SecureStore from 'expo-secure-store';
|
||||||
import { useEffect } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { Platform, Text, TouchableOpacity, View } from 'react-native';
|
import { Platform, Text, TouchableOpacity, View } from 'react-native';
|
||||||
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||||
|
import MemoList from './memo-list';
|
||||||
|
|
||||||
export default function HomeScreen() {
|
export default function HomeScreen() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const insets = useSafeAreaInsets();
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||||
|
|
||||||
let token;
|
|
||||||
// 在组件中使用
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const setupBackgroundTask = async () => {
|
const checkAuthStatus = async () => {
|
||||||
try {
|
try {
|
||||||
// 请求必要的权限
|
let token;
|
||||||
const { status } = await MediaLibrary.requestPermissionsAsync();
|
if (Platform.OS === 'web') {
|
||||||
if (status !== 'granted') {
|
token = localStorage.getItem('token') || '';
|
||||||
console.warn('Media library permission not granted');
|
} else {
|
||||||
return;
|
token = await SecureStore.getItemAsync('token') || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注册后台任务
|
const loggedIn = !!token;
|
||||||
await registerBackgroundUploadTask();
|
setIsLoggedIn(loggedIn);
|
||||||
|
|
||||||
// 立即执行一次上传
|
if (loggedIn) {
|
||||||
const now = new Date();
|
// 已登录,请求必要的权限
|
||||||
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
const { status } = await MediaLibrary.requestPermissionsAsync();
|
||||||
triggerManualUpload(oneDayAgo, now);
|
if (status === 'granted') {
|
||||||
|
await registerBackgroundUploadTask();
|
||||||
|
const now = new Date();
|
||||||
|
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
||||||
|
await triggerManualUpload(oneDayAgo, now);
|
||||||
|
}
|
||||||
|
router.replace('/ask');
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error setting up background task:', error);
|
console.error('检查登录状态出错:', error);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
setupBackgroundTask();
|
checkAuthStatus();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
if (isLoading) {
|
||||||
<View className="flex-1 bg-bgPrimary px-[1rem] h-screen overflow-auto py-[2rem] pt-[10rem]">
|
return (
|
||||||
{/* 标题区域 */}
|
<View className="flex-1 bg-bgPrimary justify-center items-center">
|
||||||
<View className="items-start mb-10 w-full px-5">
|
<Text className="text-white">加载中...</Text>
|
||||||
<Text className="text-white text-3xl font-bold mb-3 text-left">
|
|
||||||
{t('auth.welcomeAwaken.awaken', { ns: 'login' })}
|
|
||||||
{"\n"}
|
|
||||||
{t('auth.welcomeAwaken.your', { ns: 'login' })}
|
|
||||||
{"\n"}
|
|
||||||
{t('auth.welcomeAwaken.pm', { ns: 'login' })}
|
|
||||||
</Text>
|
|
||||||
<Text className="text-white/85 text-base text-left">
|
|
||||||
{t('auth.welcomeAwaken.slogan', { ns: 'login' })}
|
|
||||||
</Text>
|
|
||||||
</View>
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
{/* Memo 形象区域 */}
|
return (
|
||||||
{/* 如果是web端,使用静态ip形象,否则使用lottie */}
|
<View className="flex-1">
|
||||||
{Platform.OS === 'web' ? <IP /> : <Lottie source={'welcome'} style={{ width: 200, height: 200 }} />}
|
{
|
||||||
|
isLoggedIn ? <MemoList /> :
|
||||||
|
<View className="flex-1 bg-bgPrimary px-[1rem] h-screen overflow-auto py-[2rem] " style={{ paddingTop: insets.top + 48 }}>
|
||||||
|
{/* 标题区域 */}
|
||||||
|
<View className="items-start mb-10 w-full px-5">
|
||||||
|
<Text className="text-white text-3xl font-bold mb-3 text-left">
|
||||||
|
{t('auth.welcomeAwaken.awaken', { ns: 'login' })}
|
||||||
|
{"\n"}
|
||||||
|
{t('auth.welcomeAwaken.your', { ns: 'login' })}
|
||||||
|
{"\n"}
|
||||||
|
{t('auth.welcomeAwaken.pm', { ns: 'login' })}
|
||||||
|
</Text>
|
||||||
|
<Text className="text-white/85 text-base text-left">
|
||||||
|
{t('auth.welcomeAwaken.slogan', { ns: 'login' })}
|
||||||
|
</Text>
|
||||||
|
</View>
|
||||||
|
|
||||||
{/* 介绍文本 */}
|
{/* Memo 形象区域 */}
|
||||||
<Text className="text-white text-base text-center mb-[1rem] leading-6 opacity-90 px-10 -mt-[4rem]">
|
<View className="items-center">
|
||||||
{t('auth.welcomeAwaken.gallery', { ns: 'login' })}
|
<IP />
|
||||||
{"\n"}
|
</View>
|
||||||
{t('auth.welcomeAwaken.back', { ns: 'login' })}
|
|
||||||
</Text>
|
|
||||||
<MessagePush />
|
|
||||||
{/* 唤醒按钮 */}
|
|
||||||
<TouchableOpacity
|
|
||||||
className="bg-white rounded-full px-10 py-4 shadow-[0_2px_4px_rgba(0,0,0,0.1)] w-full items-center"
|
|
||||||
onPress={async () => {
|
|
||||||
// 判断是否有用户信息,有的话直接到usermessage页面 没有到登录页
|
|
||||||
if (Platform.OS === 'web') {
|
|
||||||
token = localStorage.getItem('token') || "";
|
|
||||||
} else {
|
|
||||||
token = await SecureStore.getItemAsync('token') || "";
|
|
||||||
}
|
|
||||||
console.log("token111111111", token);
|
|
||||||
if (token) {
|
|
||||||
router.push('/user-message')
|
|
||||||
} else {
|
|
||||||
router.push('/login')
|
|
||||||
}
|
|
||||||
|
|
||||||
}}
|
{/* 介绍文本 */}
|
||||||
activeOpacity={0.8}
|
<Text className="text-white text-base text-center mb-[1rem] leading-6 opacity-90 px-10 -mt-[4rem]">
|
||||||
>
|
{t('auth.welcomeAwaken.gallery', { ns: 'login' })}
|
||||||
<Text className="text-[#4C320C] font-bold text-lg">
|
{"\n"}
|
||||||
{t('auth.welcomeAwaken.awake', { ns: 'login' })}
|
{t('auth.welcomeAwaken.back', { ns: 'login' })}
|
||||||
</Text>
|
</Text>
|
||||||
</TouchableOpacity>
|
{/* <MessagePush /> */}
|
||||||
|
{/* 唤醒按钮 */}
|
||||||
|
<TouchableOpacity
|
||||||
|
className="bg-white rounded-full px-10 py-4 shadow-[0_2px_4px_rgba(0,0,0,0.1)] w-full items-center"
|
||||||
|
onPress={async () => {
|
||||||
|
router.push('/login')
|
||||||
|
}}
|
||||||
|
activeOpacity={0.8}
|
||||||
|
>
|
||||||
|
<Text className="text-[#4C320C] font-bold text-lg">
|
||||||
|
{t('auth.welcomeAwaken.awake', { ns: 'login' })}
|
||||||
|
</Text>
|
||||||
|
</TouchableOpacity>
|
||||||
|
</View>
|
||||||
|
}
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user