loginPage.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 [isLoading, setIsLoading] = useState(false);
  35. const { login } = useAuth();
  36. const [isChecked, setChecked] = useState(false);
  37. const [showPassword, setShowPassword] = useState(false);
  38. useEffect(() => {
  39. loadSavedCredentials();
  40. }, []);
  41. const loadSavedCredentials = async () => {
  42. try {
  43. const savedPhone = await AsyncStorage.getItem('savedPhone');
  44. // const savedEmail = await AsyncStorage.getItem('savedEmail');
  45. const savedPassword = await AsyncStorage.getItem('savedPassword');
  46. if (savedPhone && savedPassword) {
  47. // setLoginEmail(savedEmail);
  48. setLoginPhone(savedPhone);
  49. setLoginPassword(savedPassword);
  50. setSaveAccount(true);
  51. setChecked(true);
  52. }
  53. } catch (error) {
  54. console.error('Error loading saved credentials:', error);
  55. }
  56. };
  57. // const _login = async (username: string, password: string) => {
  58. // setIsLoading(true);
  59. // if (username === '' || password === '') {
  60. // Alert.alert('請輸入資料', '請輸入電子郵件和密碼');
  61. // } else {
  62. // const lowerCaseUsername = username.toLowerCase();
  63. // const success = await login(lowerCaseUsername, password);
  64. // if (!success) {
  65. // Alert.alert('登入失敗', '請檢查您的電子郵件和密碼');
  66. // }
  67. // }
  68. // setIsLoading(false);
  69. // };
  70. // const _login = async (username: string, password: string) => {
  71. // setIsLoading(true);
  72. // if (username === '' || password === '') {
  73. // Alert.alert('請輸入資料', '請輸入電話號碼和密碼');
  74. // } else if (username.includes('@')) {
  75. // Alert.alert('請綁定您的手機號碼', '客戶只能使用已經綁定的手機號碼進行登入', [
  76. // { text: '我要進行綁定', onPress: () => goToBindingPhoneNumberPage() },
  77. // { text: '我已綁定,帶我回登入頁面', onPress: () => router.replace('/login') }
  78. // ]);
  79. // } else {
  80. // // const lowerCaseUsername = username.toLowerCase();
  81. // const isBinding = false;
  82. // const success = await login(username, password, isBinding);
  83. // if (success) {
  84. // if (saveAccount) {
  85. // await AsyncStorage.setItem('savedEmail', username);
  86. // await AsyncStorage.setItem('savedPassword', password);
  87. // } else {
  88. // await AsyncStorage.removeItem('savedEmail');
  89. // await AsyncStorage.removeItem('savedPassword');
  90. // }
  91. // } else {
  92. // Alert.alert('登入失敗', '請檢查您的電子郵件和密碼');
  93. // }
  94. // }
  95. // setIsLoading(false);
  96. // };
  97. const _login = async (username: string, password: string) => {
  98. setIsLoading(true);
  99. if (username === '' || password === '') {
  100. Alert.alert('請輸入資料', '請輸入電話號碼和密碼');
  101. } else if (username.includes('@')) {
  102. Alert.alert('請綁定您的手機號碼', '客戶現在起只能使用已經綁定的手機號碼進行登入', [
  103. { text: '我要進行綁定', onPress: () => goToBindingPhoneNumberPage() },
  104. { text: '我已綁定,帶我回登入頁面', onPress: () => router.replace('/login') }
  105. ]);
  106. } else {
  107. // const lowerCaseUsername = username.toLowerCase();
  108. const isBinding = false;
  109. const response = await login(username, password, isBinding);
  110. if (response === 'login successful') {
  111. if (saveAccount) {
  112. await AsyncStorage.setItem('savedPhone', username);
  113. await AsyncStorage.setItem('savedPassword', password);
  114. } else {
  115. await AsyncStorage.removeItem('savedPhone');
  116. await AsyncStorage.removeItem('savedPassword');
  117. }
  118. } else {
  119. Alert.alert('登入失敗', `原因: ${response}`);
  120. }
  121. }
  122. setIsLoading(false);
  123. };
  124. const insets = useSafeAreaInsets();
  125. console.log(screenHeight);
  126. return (
  127. // <View className={`flex-1 justify-center ${screenHeight < 750 ? 'h-screen' : 'h-screen space-y-8'}`}>
  128. // {/* // <View className={`flex-1 justify-center ${screenHeight < 750 ? 'h-screen' : 'h-[80vh] space-y-8'}`}> */}
  129. // <View className="flex-3 items-center justify-end" style={{}}>
  130. // <Image
  131. // source={require('../../../../assets/ccLogo.png')}
  132. // resizeMode="contain"
  133. // style={{
  134. // width: screenHeight > 750 ? 250 : 200,
  135. // height: screenHeight > 750 ? 250 : 200
  136. // }}
  137. // />
  138. // </View>
  139. // <View
  140. // style={{
  141. // gap: 10
  142. // // paddingBottom: Math.max(insets.bottom, 20)
  143. // // marginTop: screenHeight > 750 ? 40 : 0
  144. // }}
  145. // className="mx-[5%] flex-2 "
  146. // >
  147. // <Text className="text-lg text-center text-[#888888] font-[600]">
  148. // 提示: 舊用戶<Text className="font-bold">必須</Text>綁定手機號碼才能登入
  149. // </Text>
  150. // <Pressable onPress={goToBindingPhoneNumberPage}>
  151. // <Text className="text-lg text-center text-[#888888] font-[600] underline">
  152. // 立即按我前往綁定頁面
  153. // </Text>
  154. // </Pressable>
  155. // <PhoneInput
  156. // value={loginPhone}
  157. // onChangeText={(phone) => setLoginPhone(phone)}
  158. // placeholder="輸入電話號碼"
  159. // extendedStyle={{ borderRadius: 12, padding: 20 }}
  160. // textContentType="telephoneNumber"
  161. // autoComplete="tel"
  162. // keyboardType="phone-pad"
  163. // autoCapitalize="none"
  164. // />
  165. // <View className="relative">
  166. // <NormalInput
  167. // value={loginPassword}
  168. // placeholder="密碼"
  169. // onChangeText={(password) => setLoginPassword(password)}
  170. // secureTextEntry={!showPassword}
  171. // extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }}
  172. // textContentType="password"
  173. // autoComplete="password"
  174. // />
  175. // <Pressable
  176. // className="absolute right-4 top-0 bottom-0 justify-center"
  177. // onPress={() => setShowPassword(!showPassword)}
  178. // >
  179. // <Ionicons name={showPassword ? 'eye-outline' : 'eye-off-outline'} size={24} color="#02677D" />
  180. // </Pressable>
  181. // </View>
  182. // <View className="flex flex-row items-center ">
  183. // <Checkbox
  184. // style={styles.checkbox}
  185. // value={saveAccount}
  186. // color={saveAccount ? '#02677D' : '#02677D'}
  187. // onValueChange={(newValue) => {
  188. // setSaveAccount(newValue);
  189. // console.log(newValue);
  190. // }}
  191. // />
  192. // <Text style={styles.text}>記住我的電話號碼</Text>
  193. // </View>
  194. // <NormalButton
  195. // extendedStyle={{ padding: 20 }}
  196. // onPress={() => _login(loginPhone, loginPassword)}
  197. // title={
  198. // isLoading ? (
  199. // <Text
  200. // style={{
  201. // fontWeight: '700',
  202. // fontSize: 20,
  203. // color: '#fff'
  204. // }}
  205. // >
  206. // <ActivityIndicator />
  207. // </Text>
  208. // ) : (
  209. // <Text
  210. // style={{
  211. // fontWeight: '700',
  212. // fontSize: 20,
  213. // color: '#fff'
  214. // }}
  215. // >
  216. // 登入
  217. // </Text>
  218. // )
  219. // }
  220. // />
  221. // <View className="flex flex-row justify-between relative">
  222. // <Pressable className="self-start" onPress={goToNextPage}>
  223. // <Text style={styles.text}>註冊會員</Text>
  224. // </Pressable>
  225. // <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
  226. // <Text style={styles.text}>忘記密碼</Text>
  227. // </Pressable>
  228. // </View>
  229. // </View>
  230. // </View>
  231. <ScrollView
  232. contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }}
  233. keyboardShouldPersistTaps="handled"
  234. className={`flex-1 ${screenHeight < 750 ? '' : 'space-y-8'}`}
  235. >
  236. {/* // <View className={`flex-1 justify-center ${screenHeight < 750 ? 'h-screen' : 'h-[80vh] space-y-8'}`}> */}
  237. <View className="flex-1 py-4">
  238. <View className="items-center">
  239. {/* <View className="flex-3 items-center justify-end" style={{}}> */}
  240. <Image
  241. source={require('../../../../assets/ccLogo.png')}
  242. resizeMode="contain"
  243. style={{
  244. width: screenHeight > 750 ? 250 : 180,
  245. height: screenHeight > 750 ? 250 : 180
  246. }}
  247. />
  248. </View>
  249. <View
  250. style={{
  251. gap: 10
  252. // paddingBottom: Math.max(insets.bottom, 20)
  253. // marginTop: screenHeight > 750 ? 40 : 0
  254. }}
  255. className="mx-[5%] mt-4 "
  256. >
  257. <Text className="text-lg text-center text-[#888888] font-[500]">
  258. 提示: 舊用戶<Text className="font-bold">必須</Text>綁定手機號碼才能登入
  259. </Text>
  260. <Pressable onPress={goToBindingPhoneNumberPage}>
  261. <Text className="text-xl text-center text-[#02677D] font-[800] underline">
  262. 立即按我前往綁定頁面
  263. </Text>
  264. </Pressable>
  265. <PhoneInput
  266. value={loginPhone}
  267. onChangeText={(phone) => setLoginPhone(phone)}
  268. placeholder="輸入電話號碼"
  269. extendedStyle={{ borderRadius: 12, padding: 20 }}
  270. textContentType="telephoneNumber"
  271. autoComplete="tel"
  272. keyboardType="phone-pad"
  273. autoCapitalize="none"
  274. />
  275. <View className="relative">
  276. <NormalInput
  277. value={loginPassword}
  278. placeholder="密碼"
  279. onChangeText={(password) => setLoginPassword(password)}
  280. secureTextEntry={!showPassword}
  281. extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }}
  282. textContentType="password"
  283. autoComplete="password"
  284. />
  285. <Pressable
  286. className="absolute right-4 top-0 bottom-0 justify-center"
  287. onPress={() => setShowPassword(!showPassword)}
  288. >
  289. <Ionicons
  290. name={showPassword ? 'eye-outline' : 'eye-off-outline'}
  291. size={24}
  292. color="#02677D"
  293. />
  294. </Pressable>
  295. </View>
  296. <View className="flex flex-row items-center ">
  297. <Checkbox
  298. style={styles.checkbox}
  299. value={saveAccount}
  300. color={saveAccount ? '#02677D' : '#02677D'}
  301. onValueChange={(newValue) => {
  302. setSaveAccount(newValue);
  303. console.log(newValue);
  304. }}
  305. />
  306. <Text style={styles.text}>記住我的電話號碼</Text>
  307. </View>
  308. <NormalButton
  309. extendedStyle={{ padding: 20 }}
  310. onPress={() => _login(loginPhone, loginPassword)}
  311. title={
  312. isLoading ? (
  313. <Text
  314. style={{
  315. fontWeight: '700',
  316. fontSize: 20,
  317. color: '#fff'
  318. }}
  319. >
  320. <ActivityIndicator />
  321. </Text>
  322. ) : (
  323. <Text
  324. style={{
  325. fontWeight: '700',
  326. fontSize: 20,
  327. color: '#fff'
  328. }}
  329. >
  330. 登入
  331. </Text>
  332. )
  333. }
  334. />
  335. <View className="flex flex-row justify-between relative">
  336. <Pressable className="self-start" onPress={goToNextPage}>
  337. <Text style={styles.text}>註冊會員</Text>
  338. </Pressable>
  339. <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
  340. <Text style={styles.text}>忘記密碼</Text>
  341. </Pressable>
  342. </View>
  343. </View>
  344. <View className="pb-6" />
  345. </View>
  346. </ScrollView>
  347. );
  348. };
  349. const styles = StyleSheet.create({
  350. container: {
  351. flex: 1,
  352. height: Dimensions.get('window').height,
  353. justifyContent: 'center',
  354. marginHorizontal: 20
  355. },
  356. topContainerForLogo: {},
  357. checkbox: {
  358. margin: 8
  359. },
  360. text: {
  361. color: '#02677D',
  362. fontSize: 16,
  363. paddingVertical: 5
  364. }
  365. });
  366. export default LoginPage;