loginPage.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {
  2. View,
  3. Text,
  4. Image,
  5. StyleSheet,
  6. Pressable,
  7. Alert,
  8. Dimensions,
  9. ActivityIndicator,
  10. ScrollView
  11. } from 'react-native';
  12. import { Ionicons } from '@expo/vector-icons';
  13. import Logo from '../../../global/logo';
  14. import NormalButton from '../../../global/normal_button';
  15. import NormalInput from '../../../global/normal_input';
  16. import { useEffect, useState } from 'react';
  17. import { useAuth } from '../../../../context/AuthProvider';
  18. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  19. import AsyncStorage from '@react-native-async-storage/async-storage';
  20. import Checkbox from 'expo-checkbox';
  21. import { router } from 'expo-router';
  22. import PhoneInput from '../../../global/phone_input';
  23. type LoginPageProps = {
  24. goToNextPage: () => void;
  25. goToForgetPassWordPage: () => void;
  26. goToBindingPhoneNumberPage: () => void;
  27. };
  28. const screenHeight = Dimensions.get('window').height;
  29. const LoginPage: React.FC<LoginPageProps> = ({ goToNextPage, goToForgetPassWordPage, goToBindingPhoneNumberPage }) => {
  30. // const [loginEmail, setLoginEmail] = useState('');
  31. const [loginPhone, setLoginPhone] = useState('');
  32. const [loginPassword, setLoginPassword] = useState('');
  33. const [saveAccount, setSaveAccount] = useState(false);
  34. const [isLoading, setIsLoading] = useState(false);
  35. const { login } = useAuth();
  36. const [isChecked, setChecked] = useState(false);
  37. const [showPassword, setShowPassword] = useState(false);
  38. useEffect(() => {
  39. loadSavedCredentials();
  40. }, []);
  41. const loadSavedCredentials = async () => {
  42. try {
  43. const savedPhone = await AsyncStorage.getItem('savedPhone');
  44. // const savedEmail = await AsyncStorage.getItem('savedEmail');
  45. const savedPassword = await AsyncStorage.getItem('savedPassword');
  46. if (savedPhone && savedPassword) {
  47. // setLoginEmail(savedEmail);
  48. setLoginPhone(savedPhone);
  49. setLoginPassword(savedPassword);
  50. setSaveAccount(true);
  51. setChecked(true);
  52. }
  53. } catch (error) {
  54. console.error('Error loading saved credentials:', error);
  55. }
  56. };
  57. const _login = async (username: string, password: string) => {
  58. setIsLoading(true);
  59. if (username === '' || password === '') {
  60. Alert.alert('請輸入資料', '請輸入電話號碼和密碼');
  61. } else if (username.includes('@')) {
  62. Alert.alert('請綁定您的手機號碼', '客戶現在起只能使用已經綁定的手機號碼進行登入', [
  63. { text: '我要進行綁定', onPress: () => goToBindingPhoneNumberPage() },
  64. { text: '我已綁定,帶我回登入頁面', onPress: () => router.replace('/login') }
  65. ]);
  66. } else {
  67. // const lowerCaseUsername = username.toLowerCase();
  68. const isBinding = false;
  69. const response = await login(username, password, isBinding);
  70. if (response === 'login successful') {
  71. if (saveAccount) {
  72. await AsyncStorage.setItem('savedPhone', username);
  73. await AsyncStorage.setItem('savedPassword', password);
  74. } else {
  75. await AsyncStorage.removeItem('savedPhone');
  76. await AsyncStorage.removeItem('savedPassword');
  77. }
  78. } else {
  79. Alert.alert('登入失敗', `原因: ${response}`);
  80. }
  81. }
  82. setIsLoading(false);
  83. };
  84. const insets = useSafeAreaInsets();
  85. console.log(screenHeight);
  86. return (
  87. <ScrollView
  88. contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }}
  89. keyboardShouldPersistTaps="handled"
  90. className={`flex-1 ${screenHeight < 750 ? '' : 'space-y-8'}`}
  91. >
  92. {/* // <View className={`flex-1 justify-center ${screenHeight < 750 ? 'h-screen' : 'h-[80vh] space-y-8'}`}> */}
  93. <View className="flex-1 py-4">
  94. <View className="items-center">
  95. {/* <View className="flex-3 items-center justify-end" style={{}}> */}
  96. <Image
  97. source={require('../../../../assets/ccLogo.png')}
  98. resizeMode="contain"
  99. style={{
  100. width: screenHeight > 750 ? 250 : 180,
  101. height: screenHeight > 750 ? 250 : 180
  102. }}
  103. />
  104. </View>
  105. <View
  106. style={{
  107. gap: 10
  108. // paddingBottom: Math.max(insets.bottom, 20)
  109. // marginTop: screenHeight > 750 ? 40 : 0
  110. }}
  111. className="mx-[5%] mt-4"
  112. >
  113. <Text className="text-lg text-center text-[#888888] font-[500]">
  114. 提示: 舊用戶<Text className="font-bold">必須</Text>綁定手機號碼才能登入
  115. </Text>
  116. <Pressable onPress={goToBindingPhoneNumberPage}>
  117. <Text className="text-xl text-center text-[#02677D] font-[800] underline">
  118. 立即按我前往綁定頁面
  119. </Text>
  120. </Pressable>
  121. <PhoneInput
  122. value={loginPhone}
  123. onChangeText={(phone) => setLoginPhone(phone)}
  124. placeholder="輸入電話號碼"
  125. extendedStyle={{ borderRadius: 12, padding: 20 }}
  126. textContentType="telephoneNumber"
  127. autoComplete="tel"
  128. keyboardType="phone-pad"
  129. autoCapitalize="none"
  130. />
  131. <View className="relative">
  132. <NormalInput
  133. value={loginPassword}
  134. placeholder="密碼"
  135. onChangeText={(password) => setLoginPassword(password)}
  136. secureTextEntry={!showPassword}
  137. extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }}
  138. textContentType="password"
  139. autoComplete="password"
  140. />
  141. <Pressable
  142. className="absolute right-4 top-0 bottom-0 flex justify-center"
  143. onPress={() => setShowPassword(!showPassword)}
  144. >
  145. <Ionicons
  146. name={showPassword ? 'eye-outline' : 'eye-off-outline'}
  147. size={24}
  148. color="#02677D"
  149. />
  150. </Pressable>
  151. </View>
  152. <View className="flex flex-row items-center ">
  153. <Checkbox
  154. style={styles.checkbox}
  155. value={saveAccount}
  156. color={saveAccount ? '#02677D' : '#02677D'}
  157. onValueChange={(newValue) => {
  158. setSaveAccount(newValue);
  159. console.log(newValue);
  160. }}
  161. />
  162. <Text style={styles.text}>記住我的電話號碼</Text>
  163. </View>
  164. <NormalButton
  165. extendedStyle={{ padding: 20 }}
  166. onPress={() => _login(loginPhone, loginPassword)}
  167. title={
  168. isLoading ? (
  169. <Text
  170. style={{
  171. fontWeight: '700',
  172. fontSize: 20,
  173. color: '#fff'
  174. }}
  175. >
  176. <ActivityIndicator />
  177. </Text>
  178. ) : (
  179. <Text
  180. style={{
  181. fontWeight: '700',
  182. fontSize: 20,
  183. color: '#fff'
  184. }}
  185. >
  186. 登入
  187. </Text>
  188. )
  189. }
  190. />
  191. <View className="flex flex-row justify-between relative">
  192. <Pressable className="self-start" onPress={goToNextPage}>
  193. <Text style={styles.text}>註冊會員</Text>
  194. </Pressable>
  195. <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
  196. <Text style={styles.text}>忘記密碼</Text>
  197. </Pressable>
  198. </View>
  199. </View>
  200. <View className="pb-6" />
  201. </View>
  202. </ScrollView>
  203. );
  204. };
  205. const styles = StyleSheet.create({
  206. container: {
  207. flex: 1,
  208. height: Dimensions.get('window').height,
  209. justifyContent: 'center',
  210. marginHorizontal: 20
  211. },
  212. topContainerForLogo: {},
  213. checkbox: {
  214. margin: 8
  215. },
  216. text: {
  217. color: '#02677D',
  218. fontSize: 16,
  219. paddingVertical: 5
  220. }
  221. });
  222. export default LoginPage;