loginPage.tsx 17 KB

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