| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- // import { CameraView, useCameraPermissions } from 'expo-camera';
- // import { useEffect, useRef, useState } from 'react';
- // import { ActivityIndicator, Dimensions, Pressable, ScrollView, StyleSheet, Text, Vibration, View } from 'react-native';
- // import ChooseCarForChargingRow from '../../../../component/global/chooseCarForChargingRow';
- // import { CrossLogoWhiteSvg, QuestionSvg } from '../../../../component/global/SVG';
- // import { router } from 'expo-router';
- // import { chargeStationService } from '../../../../service/chargeStationService';
- // import { authenticationService } from '../../../../service/authService';
- // import { walletService } from '../../../../service/walletService';
- // const ScanQrPage = () => {
- // const [permission, requestPermission] = useCameraPermissions();
- // const [scanned, setScanned] = useState(false);
- // const viewRef = useRef(null);
- // const [scannedResult, setScannedResult] = useState('');
- // const [selectedCar, setSelectedCar] = useState('');
- // const [userID, setUserID] = useState<string>('');
- // const now = new Date();
- // useEffect(() => {
- // (async () => {
- // const { status } = await requestPermission();
- // if (status !== 'granted') {
- // alert('需要相機權限以掃描QR碼');
- // }
- // })();
- // }, []);
- // useEffect(() => {
- // const fetchID = async () => {
- // try {
- // const response = await authenticationService.getUserInfo();
- // if (response) {
- // setUserID(response.data.id);
- // } else {
- // console.log('fail to set user ID');
- // }
- // } catch (error) {
- // console.log(error);
- // }
- // };
- // fetchID();
- // }, []);
- // // useEffect(() => {
- // // console.log(selectedCar);
- // // }, [selectedCar]);
- // if (!permission) {
- // return <View />;
- // }
- // if (!permission.granted) {
- // return (
- // <View className="flex-1 justify-center items-center">
- // <Text style={{ textAlign: 'center' }}>需要相機權限以掃描QR碼,請在設定中開啟相機權限</Text>
- // </View>
- // );
- // }
- // const { width: screenWidth, height: screenHeight } = Dimensions.get('window');
- // const handleBarCodeScanned = ({ bounds, data, type }: { bounds: any; data: any; type: any }) => {
- // const { origin, size } = bounds;
- // // Calculate the size of the square transparent area
- // const transparentAreaSize = Math.min(screenWidth * 0.6, screenHeight * 0.3);
- // const transparentAreaX = (screenWidth - transparentAreaSize) / 2;
- // const transparentAreaY = (screenHeight - transparentAreaSize) / 2;
- // // Check if the barcode is within the transparent area
- // if (
- // origin.x >= transparentAreaX &&
- // origin.y >= transparentAreaY &&
- // origin.x + size.width <= transparentAreaX + transparentAreaSize &&
- // origin.y + size.height <= transparentAreaY + transparentAreaSize
- // ) {
- // setScanned(true);
- // setScannedResult(data);
- // Vibration.vibrate(100);
- // console.log(` type: ${type} data: ${data} typeofData ${typeof data}`);
- // startCharging(data);
- // setTimeout(() => {
- // setScanned(false);
- // }, 5000);
- // }
- // };
- // const ChooseCar = () => {
- // const [loading, setLoading] = useState(false);
- // const isLargeScreen = screenHeight >= 800;
- // const [carData, setCarData] = useState([]);
- // const defaultImageUrl = require('../../../../assets/car1.png');
- // useEffect(() => {
- // const fetchAllCars = async () => {
- // setLoading(true);
- // try {
- // const response = await chargeStationService.getUserCars();
- // if (response) {
- // // console.log(response.data);
- // const carTypes = response.data.map((item: any) => ({
- // id: item.id,
- // name: item.car_type.name,
- // image: item.car_type.type_image_url
- // }));
- // // console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', carTypes);
- // let updatedCarTypes = [...carTypes];
- // for (let i = 0; i < carTypes.length; i++) {
- // const car = updatedCarTypes[i];
- // const imageUrl = await chargeStationService.getProcessedImageUrl(car.image);
- // updatedCarTypes[i] = {
- // ...car,
- // image: imageUrl
- // };
- // }
- // setCarData(updatedCarTypes);
- // return true;
- // }
- // } catch (error) {
- // console.log(error);
- // } finally {
- // setLoading(false);
- // }
- // };
- // fetchAllCars();
- // }, []);
- // return (
- // <View
- // style={{
- // ...(isLargeScreen
- // ? {
- // marginTop: '10%',
- // marginBottom: '12%',
- // paddingBottom: 12
- // }
- // : {
- // flex: 1,
- // alignItems: 'center',
- // justifyContent: 'center'
- // })
- // }}
- // >
- // <View className="justify-center items-center flex-1 ">
- // <View
- // style={{
- // ...(isLargeScreen
- // ? {}
- // : {
- // backgroundColor: 'rgba(0,0,0,0.7)'
- // })
- // }}
- // >
- // {loading ? (
- // <View className="w-full">
- // <ActivityIndicator color="#34657b" />
- // </View>
- // ) : (
- // <View className="w-full bg-[#000000B3]">
- // <View className="flex-row items-center justify-between mx-[5%] ">
- // <Pressable
- // className="pt-4 "
- // onPress={() => {
- // if (router.canGoBack()) {
- // router.back();
- // } else {
- // router.replace('mainPage');
- // }
- // }}
- // >
- // <CrossLogoWhiteSvg />
- // </Pressable>
- // <Text className="text-base text-white pt-2">選擇充電車輛</Text>
- // <Text className="text-xl text-white pt-2"></Text>
- // </View>
- // <ScrollView
- // horizontal={true}
- // showsHorizontalScrollIndicator={false}
- // contentContainerStyle={{
- // alignItems: 'center',
- // flexDirection: 'row',
- // marginVertical: 12
- // }}
- // className="space-x-2 mx-[5%]"
- // >
- // {carData.map((car, index) => (
- // <ChooseCarForChargingRow
- // key={`${car.name}+${index}`}
- // image={car.image}
- // onPress={() => {
- // setSelectedCar(car.id);
- // console.log(car.id);
- // }}
- // isSelected={selectedCar === car.id}
- // // imageUrl={image}
- // VehicleName={car.name}
- // isDefault={car.isDefault}
- // />
- // ))}
- // </ScrollView>
- // </View>
- // )}
- // </View>
- // </View>
- // </View>
- // );
- // };
- // const dataForSubmission = {
- // stationID: '2405311022116801000',
- // connector: scannedResult,
- // user: userID,
- // book_time: now,
- // end_time: now,
- // total_power: 0,
- // total_fee: 0,
- // promotion_code: '',
- // car: selectedCar,
- // type: 'walking'
- // };
- // const startCharging = async () => {
- // try {
- // const response = await walletService.submitPayment(
- // dataForSubmission.stationID,
- // dataForSubmission.connector,
- // dataForSubmission.user,
- // dataForSubmission.book_time,
- // dataForSubmission.end_time,
- // dataForSubmission.total_power,
- // dataForSubmission.total_fee,
- // dataForSubmission.promotion_code,
- // dataForSubmission.car,
- // dataForSubmission.type
- // );
- // if (response) {
- // console.log('Charging started', response);
- // router.push('(auth)/(tabs)/(charging)/chargingPage');
- // } else {
- // console.log('Failed to start charging:', response);
- // }
- // } catch (error) {
- // console.log('Failed to start charging:', error);
- // }
- // };
- // return (
- // <View style={styles.container} ref={viewRef}>
- // <CameraView
- // style={styles.camera}
- // facing="back"
- // barcodeScannerSettings={{
- // barcodeTypes: ['qr']
- // }}
- // onBarcodeScanned={scanned ? undefined : handleBarCodeScanned}
- // responsiveOrientationWhenOrientationLocked={true}
- // >
- // <View style={styles.overlay}>
- // <View style={styles.topOverlay}>
- // <ChooseCar />
- // </View>
- // <View style={styles.centerRow}>
- // <View style={styles.leftOverlay}></View>
- // <View style={styles.transparentArea}></View>
- // <View style={styles.rightOverlay} />
- // </View>
- // <View className="items-center justify-between" style={styles.bottomOverlay}>
- // <View>
- // <Text className="text-white text-lg font-bold mt-2 ">請掃瞄充電座上的二維碼</Text>
- // </View>
- // <View className="flex-row space-x-2 items-center ">
- // <QuestionSvg />
- // <Pressable onPress={() => router.push('assistancePage')}>
- // <Text className="text-white text-base">需要協助?</Text>
- // </Pressable>
- // </View>
- // <View />
- // </View>
- // </View>
- // </CameraView>
- // </View>
- // );
- // };
- // const styles = StyleSheet.create({
- // container: {
- // flex: 1
- // },
- // camera: {
- // flex: 1
- // },
- // overlay: {
- // flex: 1
- // },
- // topOverlay: {
- // flex: 35,
- // alignItems: 'center',
- // backgroundColor: 'rgba(0,0,0,0.5)'
- // },
- // centerRow: {
- // flex: 30,
- // flexDirection: 'row'
- // },
- // leftOverlay: {
- // flex: 20,
- // backgroundColor: 'rgba(0,0,0,0.5)'
- // },
- // transparentArea: {
- // flex: 60,
- // aspectRatio: 1,
- // position: 'relative'
- // },
- // rightOverlay: {
- // flex: 20,
- // backgroundColor: 'rgba(0,0,0,0.5)'
- // },
- // bottomOverlay: {
- // flex: 35,
- // backgroundColor: 'rgba(0,0,0,0.5)'
- // }
- // });
- // export default ScanQrPage;
- import { CameraView, useCameraPermissions } from 'expo-camera';
- import { useEffect, useRef, useState } from 'react';
- import {
- ActivityIndicator,
- Alert,
- Dimensions,
- Pressable,
- ScrollView,
- StyleSheet,
- Text,
- Vibration,
- View
- } from 'react-native';
- import ChooseCarForChargingRow from '../../../../component/global/chooseCarForChargingRow';
- import { CrossLogoWhiteSvg, QuestionSvg } from '../../../../component/global/SVG';
- import { router } from 'expo-router';
- import { chargeStationService } from '../../../../service/chargeStationService';
- import { authenticationService } from '../../../../service/authService';
- import { walletService } from '../../../../service/walletService';
- const ScanQrPage = () => {
- const [permission, requestPermission] = useCameraPermissions();
- const [scanned, setScanned] = useState(false);
- const viewRef = useRef(null);
- const [scannedResult, setScannedResult] = useState('');
- const [selectedCar, setSelectedCar] = useState('');
- const [userID, setUserID] = useState<string>('');
- const now = new Date();
- const [loading, setLoading] = useState(false);
- const [carData, setCarData] = useState([]);
-
- useEffect(() => {
- (async () => {
- const { status } = await requestPermission();
- if (status !== 'granted') {
- alert('需要相機權限以掃描QR碼');
- }
- })();
- }, []);
- useEffect(() => {
- const fetchAllCars = async () => {
- setLoading(true);
- try {
- const response = await chargeStationService.getUserCars();
- if (response) {
- // console.log(response.data);
- const carTypes = response.data.map((item: any) => ({
- id: item.id,
- name: item.car_type.name,
- image: item.car_type.type_image_url
- }));
- // console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', carTypes);
- let updatedCarTypes = [...carTypes];
- for (let i = 0; i < carTypes.length; i++) {
- const car = updatedCarTypes[i];
- const imageUrl = await chargeStationService.getProcessedImageUrl(car.image);
- updatedCarTypes[i] = {
- ...car,
- image: imageUrl
- };
- }
- setCarData(updatedCarTypes);
- return true;
- }
- } catch (error) {
- console.log(error);
- } finally {
- setLoading(false);
- }
- };
- fetchAllCars();
- }, []);
- useEffect(() => {
- const fetchID = async () => {
- try {
- const response = await authenticationService.getUserInfo();
- if (response) {
- setUserID(response.data.id);
- } else {
- console.log('fail to set user ID');
- }
- } catch (error) {
- console.log(error);
- }
- };
- fetchID();
- }, []);
- useEffect(() => {
- console.log(selectedCar);
- }, [selectedCar]);
- if (!permission) {
- return <View />;
- }
- if (!permission.granted) {
- return (
- <View className="flex-1 justify-center items-center">
- <Text style={{ textAlign: 'center' }}>需要相機權限以掃描QR碼,請在設定中開啟相機權限</Text>
- </View>
- );
- }
- const { width: screenWidth, height: screenHeight } = Dimensions.get('window');
- const handleBarCodeScanned = ({ bounds, data, type }: { bounds: any; data: any; type: any }) => {
- const { origin, size } = bounds;
- // Calculate the size of the square transparent area
- const transparentAreaSize = Math.min(screenWidth * 0.6, screenHeight * 0.3);
- const transparentAreaX = (screenWidth - transparentAreaSize) / 2;
- const transparentAreaY = (screenHeight - transparentAreaSize) / 2;
- // Check if the barcode is within the transparent area
- if (
- origin.x >= transparentAreaX &&
- origin.y >= transparentAreaY &&
- origin.x + size.width <= transparentAreaX + transparentAreaSize &&
- origin.y + size.height <= transparentAreaY + transparentAreaSize
- ) {
- setScanned(true);
- setScannedResult(data);
- Vibration.vibrate(100);
- console.log(` type: ${type} data: ${data} typeofData ${typeof data}`);
- startCharging(data);
- setTimeout(() => {
- setScanned(false);
- }, 2000);
- }
- };
- const ChooseCar = ({ carData, loading, selectedCar, setSelectedCar }) => {
- const isLargeScreen = screenHeight >= 800;
- const defaultImageUrl = require('../../../../assets/car1.png');
- return (
- <View
- style={{
- ...(isLargeScreen
- ? {
- marginTop: '10%',
- marginBottom: '12%',
- paddingBottom: 12
- }
- : {
- flex: 1,
- alignItems: 'center',
- justifyContent: 'center'
- })
- }}
- >
- <View className="justify-center items-center flex-1 ">
- <View
- style={{
- ...(isLargeScreen
- ? {}
- : {
- backgroundColor: 'rgba(0,0,0,0.7)'
- })
- }}
- >
- {loading ? (
- <View className="w-full">
- <ActivityIndicator color="#34657b" />
- </View>
- ) : (
- <View className="w-full bg-[#000000B3]">
- <View className="flex-row items-center justify-between mx-[5%] ">
- <Pressable
- className="pt-4 "
- onPress={() => {
- if (router.canGoBack()) {
- router.back();
- } else {
- router.replace('mainPage');
- }
- }}
- >
- <CrossLogoWhiteSvg />
- </Pressable>
- <Text className="text-base text-white pt-2">選擇充電車輛</Text>
- <Text className="text-xl text-white pt-2"></Text>
- </View>
- <ScrollView
- horizontal={true}
- showsHorizontalScrollIndicator={false}
- contentContainerStyle={{
- alignItems: 'center',
- flexDirection: 'row',
- marginVertical: 12
- }}
- className="space-x-2 mx-[5%]"
- >
- {carData.map((car, index) => (
- <ChooseCarForChargingRow
- key={`${car.name}+${index}`}
- image={car.image}
- onPress={() => {
- setSelectedCar(car.id);
- console.log(car.id);
- }}
- isSelected={selectedCar === car.id}
- // imageUrl={image}
- VehicleName={car.name}
- isDefault={car.isDefault}
- />
- ))}
- </ScrollView>
- </View>
- )}
- </View>
- </View>
- </View>
- );
- };
- const dataForSubmission = {
- stationID: '2405311022116801000',
- connector: scannedResult,
- user: userID,
- book_time: now,
- end_time: now,
- total_power: 0,
- total_fee: 0,
- promotion_code: '',
- car: selectedCar,
- type: 'walking'
- };
- const startCharging = async (scanResult) => {
- try {
- if (selectedCar === '') {
- Alert.alert('請選擇車輛');
- return;
- }
- const response = await walletService.submitPayment(
- dataForSubmission.stationID,
- scanResult,
- dataForSubmission.user,
- dataForSubmission.book_time,
- dataForSubmission.end_time,
- dataForSubmission.total_power,
- dataForSubmission.total_fee,
- dataForSubmission.promotion_code,
- dataForSubmission.car,
- dataForSubmission.type
- );
- if (response) {
- console.log('Charging stasdasdasdarted', response);
- router.push('(auth)/(tabs)/(charging)/chargingPage');
- } else {
- console.log('Failed to start chargi12312312ng:', response);
- Alert.alert('掃描失敗 請稍後再試。', response);
- }
- } catch (error) {
- console.log('Failed to start charging:', error);
- }
- };
- return (
- <View style={styles.container} ref={viewRef}>
- <CameraView
- style={styles.camera}
- facing="back"
- barcodeScannerSettings={{
- barcodeTypes: ['qr']
- }}
- onBarcodeScanned={scanned ? undefined : handleBarCodeScanned}
- responsiveOrientationWhenOrientationLocked={true}
- >
- <View style={styles.overlay}>
- <View style={styles.topOverlay}>
- <ChooseCar
- carData={carData}
- loading={loading}
- selectedCar={selectedCar}
- setSelectedCar={setSelectedCar}
- />
- </View>
- <View style={styles.centerRow}>
- <View style={styles.leftOverlay}></View>
- <View style={styles.transparentArea}></View>
- <View style={styles.rightOverlay} />
- </View>
- <View className="items-center justify-between" style={styles.bottomOverlay}>
- <View>
- <Text className="text-white text-lg font-bold mt-2 ">請掃瞄充電座上的二維碼</Text>
- </View>
- <View className="flex-row space-x-2 items-center ">
- <QuestionSvg />
- <Pressable onPress={() => router.push('assistancePage')}>
- <Text className="text-white text-base">需要協助?</Text>
- </Pressable>
- </View>
- <View />
- </View>
- </View>
- </CameraView>
- </View>
- );
- };
- const styles = StyleSheet.create({
- container: {
- flex: 1
- },
- camera: {
- flex: 1
- },
- overlay: {
- flex: 1
- },
- topOverlay: {
- flex: 35,
- alignItems: 'center',
- backgroundColor: 'rgba(0,0,0,0.5)'
- },
- centerRow: {
- flex: 30,
- flexDirection: 'row'
- },
- leftOverlay: {
- flex: 20,
- backgroundColor: 'rgba(0,0,0,0.5)'
- },
- transparentArea: {
- flex: 60,
- aspectRatio: 1,
- position: 'relative'
- },
- rightOverlay: {
- flex: 20,
- backgroundColor: 'rgba(0,0,0,0.5)'
- },
- bottomOverlay: {
- flex: 35,
- backgroundColor: 'rgba(0,0,0,0.5)'
- }
- });
- export default ScanQrPage;
|