loginPage.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. Alert.alert('登入失敗', '請檢查您的電話號碼和密碼');
  110. }
  111. }
  112. setIsLoading(false);
  113. };
  114. const insets = useSafeAreaInsets();
  115. return (
  116. <View className={`flex-1 justify-center ${screenHeight < 750 ? 'h-screen' : 'h-[80vh] space-y-8'}`}>
  117. <View className="flex-3 items-center justify-end" style={{}}>
  118. <Image
  119. source={require('../../../../assets/ccLogo.png')}
  120. resizeMode="contain"
  121. style={{
  122. width: screenHeight > 750 ? 250 : 200,
  123. height: screenHeight > 750 ? 250 : 200
  124. }}
  125. />
  126. </View>
  127. <View
  128. style={{
  129. gap: 10
  130. // paddingBottom: Math.max(insets.bottom, 20)
  131. // marginTop: screenHeight > 750 ? 40 : 0
  132. }}
  133. className="mx-[5%] flex-2 "
  134. >
  135. <Text className="text-lg text-center text-[#888888] font-[600]">
  136. 提示: 舊用戶<Text className="font-bold">必須</Text>綁定手機號碼才能登入
  137. </Text>
  138. <Pressable onPress={goToBindingPhoneNumberPage}>
  139. <Text className="text-lg text-center text-[#888888] font-[600] underline">
  140. 立即按我前往綁定頁面
  141. </Text>
  142. </Pressable>
  143. {/* <NormalInput
  144. placeholder="輸入電話號碼"
  145. value={loginPhone}
  146. onChangeText={(phone) => setLoginPhone(phone)}
  147. extendedStyle={{ borderRadius: 12, padding: 20 }}
  148. textContentType="username"
  149. autoComplete="username"
  150. keyboardType="email-address"
  151. autoCapitalize="none"
  152. /> */}
  153. <PhoneInput
  154. value={loginPhone}
  155. onChangeText={(phone) => setLoginPhone(phone)}
  156. placeholder="輸入電話號碼"
  157. extendedStyle={{ borderRadius: 12, padding: 20 }}
  158. textContentType="telephoneNumber"
  159. autoComplete="tel"
  160. keyboardType="phone-pad"
  161. autoCapitalize="none"
  162. />
  163. <View className="relative">
  164. <NormalInput
  165. value={loginPassword}
  166. placeholder="密碼"
  167. onChangeText={(password) => setLoginPassword(password)}
  168. secureTextEntry={!showPassword}
  169. extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }}
  170. textContentType="password"
  171. autoComplete="password"
  172. />
  173. <Pressable
  174. className="absolute right-4 top-0 bottom-0 justify-center"
  175. onPress={() => setShowPassword(!showPassword)}
  176. >
  177. <Ionicons name={showPassword ? 'eye-outline' : 'eye-off-outline'} size={24} color="#02677D" />
  178. </Pressable>
  179. </View>
  180. <View className="flex flex-row items-center ">
  181. <Checkbox
  182. style={styles.checkbox}
  183. value={saveAccount}
  184. color={saveAccount ? '#02677D' : '#02677D'}
  185. onValueChange={(newValue) => {
  186. setSaveAccount(newValue);
  187. console.log(newValue);
  188. }}
  189. />
  190. <Text style={styles.text}>記住我的電話號碼</Text>
  191. </View>
  192. <NormalButton
  193. extendedStyle={{ padding: 20 }}
  194. onPress={() => _login(loginPhone, loginPassword)}
  195. title={
  196. isLoading ? (
  197. <Text
  198. style={{
  199. fontWeight: '700',
  200. fontSize: 20,
  201. color: '#fff'
  202. }}
  203. >
  204. <ActivityIndicator />
  205. </Text>
  206. ) : (
  207. <Text
  208. style={{
  209. fontWeight: '700',
  210. fontSize: 20,
  211. color: '#fff'
  212. }}
  213. >
  214. 登入
  215. </Text>
  216. )
  217. }
  218. />
  219. <View className="flex flex-row justify-between relative">
  220. <Pressable className="self-start" onPress={goToNextPage}>
  221. <Text style={styles.text}>註冊會員</Text>
  222. </Pressable>
  223. <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
  224. <Text style={styles.text}>忘記密碼</Text>
  225. </Pressable>
  226. {/* <View className="flex flex-row justify-center absolute top-16 left-0 right-0 items-center">
  227. <View className="flex-1 h-[1px] bg-[#888888] mx-2" />
  228. <Pressable onPress={goToBindingPhoneNumberPage}>
  229. <Text className="text-[#888888] text-lg">舊用戶/已有用戶/手機號碼綁定</Text>
  230. </Pressable>
  231. <View className="flex-1 h-[1px] bg-[#888888] mx-2" />
  232. </View> */}
  233. </View>
  234. </View>
  235. </View>
  236. );
  237. };
  238. const styles = StyleSheet.create({
  239. container: {
  240. flex: 1,
  241. height: Dimensions.get('window').height,
  242. justifyContent: 'center',
  243. marginHorizontal: 20
  244. },
  245. topContainerForLogo: {},
  246. checkbox: {
  247. margin: 8
  248. },
  249. text: {
  250. color: '#02677D',
  251. fontSize: 16,
  252. paddingVertical: 5
  253. }
  254. });
  255. export default LoginPage;