From c2e52df5608dd91b224b17e35a428fbcc5bdb6f1 Mon Sep 17 00:00:00 2001 From: tx <2622874537@qq.com> Date: Mon, 30 Dec 2024 13:47:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=86=E8=8A=82=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/DeviceControl.tsx | 47 ++++++++-- src/views/Home/NormaIndex.tsx | 125 ++++++++------------------- src/views/ProfileScreen.jsx | 10 +-- src/views/device/DeviceShare.tsx | 6 +- src/views/device/KeyDetail.tsx | 79 +++++++++-------- src/views/device/deviceDetailSet.tsx | 0 src/views/device/deviceList.tsx | 2 +- src/views/device/deviceMap.tsx | 49 ++++------- 8 files changed, 145 insertions(+), 173 deletions(-) create mode 100644 src/views/device/deviceDetailSet.tsx diff --git a/src/components/DeviceControl.tsx b/src/components/DeviceControl.tsx index 4b2b3ce..13b0092 100644 --- a/src/components/DeviceControl.tsx +++ b/src/components/DeviceControl.tsx @@ -58,7 +58,7 @@ const DeviceControl: React.FC = ({ defaultDevice, onDeviceUp try { setIsConnecting(true); showWithAnimation(); - const targetMac = 'FD51BB7A4EE0'; // 暂时使用固定MAC + const targetMac = defaultDevice?.mac// 暂时使用固定MAC console.log(`${isRetry ? '重试' : '开始'}连接设备, 目标MAC:`, targetMac); const initialized = await BluetoothManager.init(); @@ -165,27 +165,27 @@ const DeviceControl: React.FC = ({ defaultDevice, onDeviceUp Alert.alert('提示', '设备数据无效,请确保设备已正确选择'); return; } - + const response = await (status ? apiService.unlocking(defaultDevice.sn) : apiService.lock(defaultDevice.sn)); - + console.log('开关锁API响应:', { 响应数据: response, 操作类型: status ? '开锁' : '关锁', 时间: new Date().toISOString() }); - + if (response.code != 200) { retryAttemptRef.current = false; const connected = await handleBluetoothConnection(false); - + if (connected) { try { await (status ? BluetoothManager.openDevice() : BluetoothManager.closeDevice()); - + if (onDeviceUpdate) { onDeviceUpdate(); // 蓝牙操作成功后调用更新 } @@ -199,7 +199,7 @@ const DeviceControl: React.FC = ({ defaultDevice, onDeviceUp } else { // API 调用成功,直接更新状态 if (onDeviceUpdate) { - onDeviceUpdate(); + onDeviceUpdate(); } } }; @@ -213,6 +213,19 @@ const DeviceControl: React.FC = ({ defaultDevice, onDeviceUp return 'rgba(255, 69, 58, 0.5)'; } }; + const calculateTimeRemaining = (expirationTime: string): string => { + const expiration = new Date(expirationTime).getTime(); + const now = new Date().getTime(); + const diff = expiration - now; + + if (diff <= 0) return '已过期'; + + const days = Math.floor(diff / (1000 * 60 * 60 * 24)); + const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); + + return `${days}天${hours}小时${minutes}分`; + }; useEffect(() => { const removeListener = BluetoothManager.addConnectionStateListener((state) => { @@ -253,6 +266,13 @@ const DeviceControl: React.FC = ({ defaultDevice, onDeviceUp 当前车辆状态: {defaultDevice?.lockStatus == 1 ? '开锁' : '关锁'} + {defaultDevice?.type == 2 && defaultDevice?.expirationTime && ( + + + 剩余有效期:{calculateTimeRemaining(defaultDevice.expirationTime)} + + + )} {showBluetoothStatus && ( { }; const handleDeviceUpdate = async () => { console.log('handleDeviceUpdate'); - + const response = await apiService.getDeviceList(); - - + + if (response?.code == 200 && response.data) { const defaultDev = response.data.find((device: DeviceType) => device.isDefault == 1); if (defaultDev) { setDefaultDevice(defaultDev); // 存储设备 sn 到本地存储 - - + + } } }; @@ -149,11 +149,11 @@ const NormaIndex: React.FC = () => { try { const response = await apiService.getDeviceList(); - console.log(response,'response'); + console.log(response, 'response'); if (response?.code === 200 && response.data) { const defaultDev = response.data.find((device: DeviceType) => device.isDefault == 1); if (defaultDev) { - console.log(defaultDev.sn,'defaultDev.sn'); + console.log(defaultDev.sn, 'defaultDev.sn'); await AsyncStorage.setItem('defaultDeviceSN', defaultDev.sn); // console.log(defaultDev, 'defaultDev'); setDefaultDevice(defaultDev); @@ -223,96 +223,39 @@ const NormaIndex: React.FC = () => { - - {/* - - - {defaultDevice?.remainingPower || 0}% - - - - - - - 当前车辆状态: {defaultDevice?.lockStatus == 1 ? '开锁' : '关锁'} - - - - - - - - - - - - - - - 鸣笛寻车 - - { - // 处理状态改变 - console.log('Lock status changed:', status); - }} - /> - - - 警报已开 - - */} - - - + + + - + - + {defaultDevice?.type == 1 && ( + <> - - - - - - - + + + - - + + + + + + + + + )} diff --git a/src/views/ProfileScreen.jsx b/src/views/ProfileScreen.jsx index cc33ed6..0d81b93 100644 --- a/src/views/ProfileScreen.jsx +++ b/src/views/ProfileScreen.jsx @@ -76,7 +76,7 @@ const ProfileScreen = () => { navigation.navigate('QrBind')} + onPress={() => navigation.navigate('BindIndex')} > { {/* 管理与服务区域 */} 管理与服务 - navigation.navigate('OrderList')} > @@ -97,7 +97,7 @@ const ProfileScreen = () => { style={styles.itemIcon} /> 我的订单 - + */} { )} - {userInfo.userType === '02' && ( + {/* {userInfo.userType === '02' && ( navigation.navigate('MerchantPortal')} @@ -145,7 +145,7 @@ const ProfileScreen = () => { /> 商户端 - )} + )} */} {/* 退出登录按钮 */} diff --git a/src/views/device/DeviceShare.tsx b/src/views/device/DeviceShare.tsx index df424dc..c4a1c1f 100644 --- a/src/views/device/DeviceShare.tsx +++ b/src/views/device/DeviceShare.tsx @@ -19,6 +19,7 @@ const DeviceShare = () => { const getKeyList = async () => { const response = await apiService.getKeyListByOwnerId(); + console.log('response', response); if (response.code == 200) { setKeyList(response.data); } @@ -47,12 +48,13 @@ const DeviceShare = () => { }; const getStatusText = (status: string | number) => { + console.log('status', status); switch (status) { case '0': case 0: return '待领取'; - case '1': - case 1: + case '2': + case 2: return '已领取'; default: return '未知状态'; diff --git a/src/views/device/KeyDetail.tsx b/src/views/device/KeyDetail.tsx index 918a4e3..f247ad4 100644 --- a/src/views/device/KeyDetail.tsx +++ b/src/views/device/KeyDetail.tsx @@ -95,7 +95,7 @@ const KeyDetail = () => { const handleTimeSelect = async (time: string) => { setSelectedTime(time); - + if (!keyItem) return; let days = 0; @@ -105,14 +105,16 @@ const KeyDetail = () => { case '30天': days = 30; break; case '永久': days = 36500; break; } - - const baseDate = new Date(keyItem.createTime); - const newExpirationDate = new Date(baseDate.getTime() + days * 24 * 60 * 60 * 1000); - + + // 使用当前时间作为基准 + const now = new Date(); + const newExpirationDate = new Date(now.getTime() + days * 24 * 60 * 60 * 1000); + + // 设置为当天的最后一秒 newExpirationDate.setHours(23); newExpirationDate.setMinutes(59); newExpirationDate.setSeconds(59); - + const formattedDate = newExpirationDate.getFullYear() + '-' + String(newExpirationDate.getMonth() + 1).padStart(2, '0') + '-' + String(newExpirationDate.getDate()).padStart(2, '0') + ' ' + @@ -132,7 +134,6 @@ const KeyDetail = () => { type: 'success', text1: '更新成功', }); - // 更新成功后重新获取钥匙信息 await fetchKeyInfo(); } else { Toast.show({ @@ -184,6 +185,7 @@ const KeyDetail = () => { navigation.navigate('ShareQrcode', { QrId: keyId }); }; + return ( { style={styles.topNav} /> - - - {keyItem.shareUserName} - {keyItem.sharePhone} - 剩余有效期:{calculateRemainingTime(keyItem.expirationTime)} - setShowTimeModal(true)}> - - 修改钥匙有效期 - - - - - 删除共享人 - - - - - + {keyItem && ( // 添加条件渲染检查 + <> + + + {keyItem.shareUserName} + {keyItem.sharePhone} + 剩余有效期:{calculateRemainingTime(keyItem.expirationTime)} + setShowTimeModal(true)}> + + 修改钥匙有效期 + + + + + 删除共享人 + + + + + + + + )} {/* 删除确认弹窗 */} 车型:{item.model} {item.vehicleNum} - 租赁中 + {item.type == 1 ? '车主' : '临时租赁'} { }; const getCurrentLocation = () => { - // const watchId = Geolocation.watchPosition( - // (position) => { - // const gcjLocation = transformFromWGSToGCJ( - // position.coords.latitude, - // position.coords.longitude - // ); - // setUserLocation(gcjLocation); - // }, - // (error) => { - // console.error('位置监听错误:', error); - // }, - // { - // enableHighAccuracy: true, - // timeout: 5000, - // maximumAge: 1000, - // distanceFilter: 10 - // } - // ); - if (!userLocation) { - console.warn('用户位置未获取'); - return; - } - + // 移除原有的检查,因为 userLocation 可能还未获取到 try { - // 使用 setStatus 方法移动地图 - if (mapRef.current) { + if (mapRef.current && userLocation) { mapRef.current.moveCamera({ target: userLocation, zoom: 15, }, 1000); + } else { + // 如果还没有获取到位置,等待位置信息 + console.log('等待获取用户位置...'); } } catch (error) { console.error('移动地图失败:', error); } }; - useEffect(() => { - getCurrentLocation(); - getDeviceLocation(); + useEffect(() => { + // 先获取位置,再调用 getCurrentLocation const watchId = Geolocation.watchPosition( (position) => { const gcjLocation = transformFromWGSToGCJ( position.coords.latitude, position.coords.longitude ); - console.log(gcjLocation,'gcjLocation'); - setUserLocation(gcjLocation); + + // 位置获取成功后自动移动地图 + if (mapRef.current) { + mapRef.current.moveCamera({ + target: gcjLocation, + zoom: 15, + }, 1000); + } }, (error) => { console.error('位置监听错误:', error); @@ -118,7 +103,9 @@ const DeviceMap = () => { distanceFilter: 10 } ); - + + getDeviceLocation(); + return () => { Geolocation.clearWatch(watchId); };