import React, { useEffect, useState, useCallback } from 'react'; import { View, Text, Image, TouchableOpacity, StyleSheet } from 'react-native'; import { useNavigation, useFocusEffect } from '@react-navigation/native'; import { apiService } from '../../utils/api'; import { rpx } from '../../utils/rpx'; interface KeyItem { id: string; avatarUrl: string; shareUserName: string; sharePhone: string; status: string; expirationTime: string; } const DeviceShare = () => { const navigation = useNavigation(); const [keyList, setKeyList] = useState([]); const getKeyList = async () => { const response = await apiService.getKeyListByOwnerId(); if (response.code == 200) { setKeyList(response.data); } }; const calculateRemainingTime = (expirationTime: string): string => { const now = new Date(); const expiration = new Date(expirationTime); const diffTime = expiration.getTime() - now.getTime(); if (diffTime <= 0) { return '已过期'; } const days = Math.floor(diffTime / (1000 * 60 * 60 * 24)); const hours = Math.floor((diffTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((diffTime % (1000 * 60 * 60)) / (1000 * 60)); if (days > 0) { return `${days}天${hours}小时`; } else if (hours > 0) { return `${hours}小时${minutes}分钟`; } else { return `${minutes}分钟`; } }; const getStatusText = (status: string | number) => { switch (status) { case '0': case 0: return '待领取'; case '1': case 1: return '已领取'; default: return '未知状态'; } }; useFocusEffect( useCallback(() => { getKeyList(); }, []) ); return ( 共享钥匙({keyList.length}/3) 临时借用车辆,可以使用基础控车功能 {keyList.length > 0 && keyList.map((key) => ( { navigation.navigate('ShareDetailScreen', { keyItem: key }); }} > {key.shareUserName} {key.sharePhone} {getStatusText(key.status)} 剩余有效期:{calculateRemainingTime(key.expirationTime)} ))} {keyList.length < 3 && ( { navigation.navigate('AddShare' as never); }}> 添加成员 )} { navigation.navigate('ExpiredKeysScreen' as never); }}> 查看已失效共享 ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F3FCFF', alignItems: 'center', }, addBtnImg: { width: rpx(32), height: rpx(32), }, shareTip: { flexDirection: 'row', alignItems: 'center', marginTop: rpx(44), }, shareBtnTxt: { fontSize: rpx(32), color: '#808080', }, addBtn: { width: rpx(592), height: rpx(108), backgroundColor: '#ffffff', borderRadius: rpx(16), marginTop: rpx(30), borderWidth: rpx(2), borderColor: '#808080', alignItems: 'center', justifyContent: 'center', flexDirection: 'row', }, addBtnTxt: { marginLeft: rpx(12), fontSize: rpx(36), color: '#808080', }, cardBottom: { flexDirection: 'row', alignItems: 'center', display: 'flex', flexWrap: 'nowrap', }, lasttime: { marginLeft: rpx(124), fontSize: rpx(24), color: '#4297F3', }, lasttimeImg: { marginLeft: 'auto', width: rpx(24), height: rpx(24), }, cardTop: { flexDirection: 'row', alignItems: 'center', display: 'flex', flexWrap: 'nowrap', }, cardType: { marginLeft: 'auto', alignSelf: 'flex-start', fontSize: rpx(32), color: '#4297F3', }, cardTopTxt: { marginLeft: rpx(28), }, cardTopImg: { width: rpx(96), height: rpx(96), borderRadius: rpx(48), }, cardTopName: { fontSize: rpx(44), color: '#3D3D3D', }, cardTopPhone: { fontSize: rpx(36), color: '#808080', }, shareBox: { width: rpx(688), borderRadius: rpx(30), backgroundColor: '#fff', padding: rpx(32), }, shareTitle: { fontWeight: 'bold', fontSize: rpx(44), color: '#3D3D3D', }, shareTxt: { marginTop: rpx(14), fontWeight: '500', fontSize: rpx(28), color: '#999', }, card: { width: rpx(592), padding: rpx(32), marginTop: rpx(32), borderRadius: rpx(16), borderWidth: rpx(2), borderColor: '#808080', }, }); export default DeviceShare;