| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 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<LoginPageProps> = ({ goToNextPage, goToForgetPassWordPage, goToBindingPhoneNumberPage }) => {
- // const [loginEmail, setLoginEmail] = useState('');
- const [loginPhone, setLoginPhone] = useState('');
- const [loginPassword, setLoginPassword] = useState('');
- const [saveAccount, setSaveAccount] = useState(false);
- const [userTerms, setUserTerms] = 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);
- setUserTerms(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 {
- if (userTerms) {
- const isBinding = false;
- const response = await login(username, password, isBinding);
- if (response) {
- 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}`);
- }
- } else {
- Alert.alert('請先同意用戶條款');
- }
-
- }
- setIsLoading(false);
- };
- const goToUserTermsPage = () => {
- router.push('(public)/userTermsPage');
- };
- const insets = useSafeAreaInsets();
- return (
- <ScrollView
- contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }}
- keyboardShouldPersistTaps="handled"
- className={`flex-1 ${screenHeight < 750 ? '' : 'space-y-8'}`}
- >
- {/* // <View className={`flex-1 justify-center ${screenHeight < 750 ? 'h-screen' : 'h-[80vh] space-y-8'}`}> */}
- <View className="flex-1 py-4">
- <View className="items-center">
- {/* <View className="flex-3 items-center justify-end" style={{}}> */}
- <Image
- source={require('../../../../assets/ccLogo.png')}
- resizeMode="contain"
- style={{
- width: screenHeight > 750 ? 250 : 180,
- height: screenHeight > 750 ? 250 : 180
- }}
- />
- </View>
- <View
- style={{
- gap: 10
- // paddingBottom: Math.max(insets.bottom, 20)
- // marginTop: screenHeight > 750 ? 40 : 0
- }}
- className="mx-[5%] mt-4"
- >
- <Text className="text-lg text-center text-[#888888] font-[500]">
- 提示: 舊用戶<Text className="font-bold">必須</Text>綁定手機號碼才能登入
- </Text>
- <Pressable onPress={goToBindingPhoneNumberPage}>
- <Text className="text-xl text-center text-[#02677D] font-[800] underline">
- 立即按我前往綁定頁面
- </Text>
- </Pressable>
- <PhoneInput
- value={loginPhone}
- onChangeText={(phone) => setLoginPhone(phone)}
- placeholder="輸入電話號碼"
- extendedStyle={{ borderRadius: 12, padding: 20 }}
- textContentType="telephoneNumber"
- autoComplete="tel"
- keyboardType="phone-pad"
- autoCapitalize="none"
- />
- <View className="relative">
- <NormalInput
- value={loginPassword}
- placeholder="密碼"
- onChangeText={(password) => setLoginPassword(password)}
- secureTextEntry={!showPassword}
- extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }}
- textContentType="password"
- autoComplete="password"
- />
- <Pressable
- className="absolute right-4 top-0 bottom-0 flex justify-center"
- onPress={() => setShowPassword(!showPassword)}
- >
- <Ionicons
- name={showPassword ? 'eye-outline' : 'eye-off-outline'}
- size={24}
- color="#02677D"
- />
- </Pressable>
- </View>
- <View className="flex flex-row items-center ">
- <Checkbox
- style={styles.checkbox}
- value={saveAccount}
- color={saveAccount ? '#02677D' : '#02677D'}
- onValueChange={(newValue) => {
- setSaveAccount(newValue);
- }}
- />
- <Text style={styles.text}>記住我的電話號碼</Text>
-
- </View>
- <View className="flex flex-row items-center">
- <Checkbox
- style={styles.checkbox}
- value={userTerms}
- color={userTerms ? '#02677D' : '#02677D'}
- onValueChange={(newValue) => {
- setUserTerms(newValue);
- }}
- />
- <Text style={styles.text}>登入即同意
- <Text style={styles.userTerms} onPress={goToUserTermsPage}>用戶條款</Text>
- </Text>
- </View>
- <NormalButton
- extendedStyle={{ padding: 20 }}
- onPress={() => _login(loginPhone, loginPassword)}
- title={
- isLoading ? (
- <Text
- style={{
- fontWeight: '700',
- fontSize: 20,
- color: '#fff'
- }}
- >
- <ActivityIndicator />
- </Text>
- ) : (
- <Text
- style={{
- fontWeight: '700',
- fontSize: 20,
- color: '#fff'
- }}
- >
- 登入
- </Text>
- )
- }
- />
- <View className="flex flex-row justify-between relative">
- <Pressable className="self-start" onPress={goToNextPage}>
- <Text style={styles.text}>註冊會員</Text>
- </Pressable>
- <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
- <Text style={styles.text}>忘記密碼</Text>
- </Pressable>
- </View>
- </View>
- <View className="pb-6" />
- </View>
- </ScrollView>
- );
- };
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- height: Dimensions.get('window').height,
- justifyContent: 'center',
- marginHorizontal: 20
- },
- topContainerForLogo: {},
- checkbox: {
- marginLeft: 8,
- marginRight: 8
- },
- text: {
- color: '#02677D',
- fontSize: 16,
- paddingVertical: 5
- },
- userTerms: {
- color: '#02677D',
- fontSize: 16,
- paddingVertical: 5,
- textDecorationLine: 'underline'
- }
- });
- export default LoginPage;
|