loginPage.tsx 12 KB

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