uberUploadPageComponent.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { View, Text, Pressable, Image, ScrollView, Alert } from 'react-native';
  2. import { SafeAreaView } from 'react-native-safe-area-context';
  3. import { router } from 'expo-router';
  4. import { PreviousPageBlackSvg } from '../global/SVG';
  5. import NormalButton from '../global/normal_button';
  6. import { useEffect, useState } from 'react';
  7. import * as ImagePicker from 'expo-image-picker';
  8. import * as SecureStore from 'expo-secure-store';
  9. import * as FileSystem from 'expo-file-system';
  10. import { authenticationService } from '../../service/authService';
  11. const UberUploadPageComponent = () => {
  12. const [image, setImage] = useState('');
  13. const MAX_FILE_SIZE = 3 * 1024 * 1024;
  14. const pickImage = async () => {
  15. let result = await ImagePicker.launchImageLibraryAsync({
  16. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  17. allowsEditing: true,
  18. aspect: [4, 3],
  19. quality: 1
  20. });
  21. if (!result.canceled) {
  22. const fileUri = result.assets[0].uri;
  23. const fileExtension = fileUri.split('.').pop().toLowerCase();
  24. if (!['jpg', 'jpeg', 'png'].includes(fileExtension)) {
  25. Alert.alert('錯誤', '請選擇 JPG, JPEG 或 PNG 格式的圖片');
  26. return;
  27. }
  28. // const fileInfo = await FileSystem.getInfoAsync(fileUri);
  29. // if (fileInfo.size > MAX_FILE_SIZE) {
  30. // Alert.alert('錯誤', '檔案大小不得超過3MB');
  31. // return;
  32. // }
  33. setImage(fileUri);
  34. }
  35. };
  36. const uploadImage = async () => {
  37. if (!image) {
  38. Alert.alert('錯誤', '請先選擇一張圖片');
  39. return;
  40. }
  41. try {
  42. const fileInfo = await FileSystem.getInfoAsync(image);
  43. if (!fileInfo.exists) {
  44. Alert.alert('錯誤', '無法讀取所選圖片');
  45. return;
  46. }
  47. const userInfo = await authenticationService.getUserInfo();
  48. if (!userInfo || !userInfo.data || !userInfo.data.id) {
  49. Alert.alert('錯誤', '無法取得使用者資訊');
  50. return;
  51. }
  52. const userId = userInfo?.data.id;
  53. const timestamp = new Date().getTime();
  54. const fileExtension = image.split('.').pop()?.toLowerCase() || 'jpg';
  55. const customFileName = `uber_${userId}_${timestamp}.${fileExtension}`;
  56. const result = await authenticationService.uploadUberImage(image, customFileName);
  57. if (result) {
  58. Alert.alert('成功', '證明上傳成功');
  59. router.push('uberUploadCompletePage');
  60. } else {
  61. Alert.alert('錯誤', result.message || '上傳失敗,請稍後再試');
  62. }
  63. } catch (error) {
  64. console.error('Error uploading image:', error);
  65. console.error('Status:', error.response?.status);
  66. console.error('Headers:', error.response?.headers);
  67. Alert.alert('錯誤', '上傳時發生錯誤,請稍後再試');
  68. throw error;
  69. }
  70. };
  71. return (
  72. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  73. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  74. <View style={{ marginTop: 25 }}>
  75. <Pressable
  76. onPress={() => {
  77. if (router.canGoBack()) {
  78. router.back();
  79. } else {
  80. router.replace('/accountMainPage');
  81. }
  82. }}
  83. >
  84. <PreviousPageBlackSvg />
  85. </Pressable>
  86. <Text style={{ fontSize: 45, marginVertical: 25 }}>上傳證明</Text>
  87. <View className="space-y-2">
  88. <Text style={{ fontSize: 22 }}>請上傳Uber司機的證明圖片</Text>
  89. <Text style={{ fontSize: 18 }}>• 司機名稱</Text>
  90. <Text style={{ fontSize: 18 }}>• 車牌</Text>
  91. <Text style={{ fontSize: 18 }}>• 車輛品牌及型號</Text>
  92. </View>
  93. <View className="mt-5 ">
  94. <Text style={{ fontSize: 22, marginBottom: 9 }}>上傳範例</Text>
  95. <View className="flex-row w-full justify-between">
  96. <Image
  97. source={require('../../assets/uberUploadSample1.png')}
  98. resizeMode="contain"
  99. className="flex-1 h-auto"
  100. style={{ aspectRatio: 1 }}
  101. />
  102. <Image
  103. source={require('../../assets/uberUploadSample2.png')}
  104. resizeMode="contain"
  105. className="flex-1 h-auto"
  106. style={{ aspectRatio: 1 }}
  107. />
  108. </View>
  109. {image ? (
  110. <Pressable onPress={pickImage} disabled={true}>
  111. <View
  112. style={{ borderWidth: 1 }}
  113. className="border-[#EEEEEE] rounded-2xl items-center justify-center p-4 "
  114. >
  115. <Text className="text-xl text-[#02677d] mb-6 text-center ">上傳成功</Text>
  116. </View>
  117. </Pressable>
  118. ) : (
  119. <Pressable onPress={pickImage}>
  120. <View
  121. style={{ borderWidth: 1 }}
  122. className="border-[#EEEEEE] rounded-2xl items-center justify-center p-4 "
  123. >
  124. <Text style={{ fontSize: 48 }} className="text-[#02677d] text-center">
  125. +
  126. </Text>
  127. <Text className="text-xl text-[#02677d] mb-6 text-center ">上傳圖片</Text>
  128. </View>
  129. </Pressable>
  130. )}
  131. <View className="mt-4">
  132. <NormalButton
  133. title={<Text className="text-xl text-white">提交</Text>}
  134. onPress={
  135. uploadImage
  136. // router.push('uberUploadCompletePage')
  137. }
  138. />
  139. </View>
  140. </View>
  141. </View>
  142. </ScrollView>
  143. </SafeAreaView>
  144. );
  145. };
  146. export default UberUploadPageComponent;