loginPage.tsx 11 KB

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