loginPage.tsx 11 KB

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