You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4 KiB

import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Alert } from 'react-native';
import { useTranslation } from 'react-i18next';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useAuth } from '../contexts/AuthContext';
import { useNavigation } from '@react-navigation/native';
import type { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { RootStackParamList } from '../types/navigation';
2 months ago
import { userApi } from '../services/api/userApi';
export const ProfileScreen = () => {
const { t } = useTranslation();
const { logout } = useAuth();
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
const handleClearLanguage = async () => {
try {
await AsyncStorage.removeItem('selectedCountry');
// 设置清除标记
await AsyncStorage.setItem('languageCleared', 'true');
// 使用 replace 而不是 reset,这样会替换当前路由而不是重置整个导航栈
navigation.replace('CountrySelect');
} catch (error) {
Alert.alert(t('error'), t('clearLanguageError'));
}
};
const handleClearLogin = async () => {
Alert.alert(
t('clearLoginTitle'),
t('clearLoginMessage'),
[
{
text: t('cancel'),
style: 'cancel',
},
{
text: t('confirm'),
onPress: async () => {
try {
await logout();
Alert.alert(t('success'), t('loginCleared'));
} catch (error) {
Alert.alert(t('error'), t('clearLoginError'));
}
},
},
]
);
2 months ago
};
const handleLogin = async () => {
const params = {
grant_type:'password',
username:'test',
password:'string',
client_id:'2',
client_secret:'',
scope:'',
}
const res = await userApi.login(params);
const token = res.token_type + ' ' + res.access_token;
await AsyncStorage.setItem('token', token);
navigation.navigate('Home');
}
return (
<View style={styles.container}>
<Text style={styles.title}>{t('profile')}</Text>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={[styles.button, styles.clearButton]}
onPress={handleClearLanguage}
>
<Text style={styles.buttonText}>{t('clearLanguage')}</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.button, styles.clearButton]}
onPress={handleClearLogin}
>
<Text style={styles.buttonText}>{t('clearLogin')}</Text>
</TouchableOpacity>
2 months ago
<TouchableOpacity
style={[styles.button, styles.clearButton]}
onPress={handleLogin}
>
<Text style={styles.buttonText}></Text>
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#fff',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
},
buttonContainer: {
gap: 12,
},
button: {
backgroundColor: '#0066FF',
padding: 15,
borderRadius: 8,
alignItems: 'center',
},
clearButton: {
backgroundColor: '#FF3B30',
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});