import { View, Text, Image, StyleSheet, Pressable, Alert, Dimensions, ActivityIndicator, ScrollView } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import Logo from '../../../global/logo'; import NormalButton from '../../../global/normal_button'; import NormalInput from '../../../global/normal_input'; import { useEffect, useState } from 'react'; import { useAuth } from '../../../../context/AuthProvider'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import AsyncStorage from '@react-native-async-storage/async-storage'; import Checkbox from 'expo-checkbox'; import { router } from 'expo-router'; import PhoneInput from '../../../global/phone_input'; type LoginPageProps = { goToNextPage: () => void; goToForgetPassWordPage: () => void; goToBindingPhoneNumberPage: () => void; }; const screenHeight = Dimensions.get('window').height; const LoginPage: React.FC = ({ goToNextPage, goToForgetPassWordPage, goToBindingPhoneNumberPage }) => { // const [loginEmail, setLoginEmail] = useState(''); const [loginPhone, setLoginPhone] = useState(''); const [loginPassword, setLoginPassword] = useState(''); const [saveAccount, setSaveAccount] = useState(false); const [isLoading, setIsLoading] = useState(false); const { login } = useAuth(); const [isChecked, setChecked] = useState(false); const [showPassword, setShowPassword] = useState(false); useEffect(() => { loadSavedCredentials(); }, []); const loadSavedCredentials = async () => { try { const savedPhone = await AsyncStorage.getItem('savedPhone'); // const savedEmail = await AsyncStorage.getItem('savedEmail'); const savedPassword = await AsyncStorage.getItem('savedPassword'); if (savedPhone && savedPassword) { // setLoginEmail(savedEmail); setLoginPhone(savedPhone); setLoginPassword(savedPassword); setSaveAccount(true); setChecked(true); } } catch (error) { console.error('Error loading saved credentials:', error); } }; const _login = async (username: string, password: string) => { setIsLoading(true); if (username === '' || password === '') { Alert.alert('請輸入資料', '請輸入電話號碼和密碼'); } else if (username.includes('@')) { Alert.alert('請綁定您的手機號碼', '客戶現在起只能使用已經綁定的手機號碼進行登入', [ { text: '我要進行綁定', onPress: () => goToBindingPhoneNumberPage() }, { text: '我已綁定,帶我回登入頁面', onPress: () => router.replace('/login') } ]); } else { // const lowerCaseUsername = username.toLowerCase(); const isBinding = false; const response = await login(username, password, isBinding); if (response === 'login successful') { if (saveAccount) { await AsyncStorage.setItem('savedPhone', username); await AsyncStorage.setItem('savedPassword', password); } else { await AsyncStorage.removeItem('savedPhone'); await AsyncStorage.removeItem('savedPassword'); } } else { Alert.alert('登入失敗', `原因: ${response}`); } } setIsLoading(false); }; const insets = useSafeAreaInsets(); console.log(screenHeight); return ( {/* // */} {/* */} 750 ? 250 : 180, height: screenHeight > 750 ? 250 : 180 }} /> 750 ? 40 : 0 }} className="mx-[5%] mt-4" > 提示: 舊用戶必須綁定手機號碼才能登入 立即按我前往綁定頁面 setLoginPhone(phone)} placeholder="輸入電話號碼" extendedStyle={{ borderRadius: 12, padding: 20 }} textContentType="telephoneNumber" autoComplete="tel" keyboardType="phone-pad" autoCapitalize="none" /> setLoginPassword(password)} secureTextEntry={!showPassword} extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }} textContentType="password" autoComplete="password" /> setShowPassword(!showPassword)} > { setSaveAccount(newValue); console.log(newValue); }} /> 記住我的電話號碼 _login(loginPhone, loginPassword)} title={ isLoading ? ( ) : ( 登入 ) } /> 註冊會員 goToForgetPassWordPage()}> 忘記密碼 ); }; const styles = StyleSheet.create({ container: { flex: 1, height: Dimensions.get('window').height, justifyContent: 'center', marginHorizontal: 20 }, topContainerForLogo: {}, checkbox: { margin: 8 }, text: { color: '#02677D', fontSize: 16, paddingVertical: 5 } }); export default LoginPage;