loginPage.tsx 11 KB

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