BikeApp_demo/src/views/device/DeviceShare.tsx

247 lines
7.7 KiB
TypeScript
Raw Normal View History

2024-12-26 14:12:13 +08:00
import React, { useEffect, useState, useCallback } from 'react';
2024-12-26 08:38:13 +08:00
import { View, Text, Image, TouchableOpacity, StyleSheet } from 'react-native';
2024-12-26 16:17:19 +08:00
import { useNavigation, useFocusEffect } from '@react-navigation/native';
2024-12-26 08:38:13 +08:00
import { apiService } from '../../utils/api';
2024-11-14 13:56:11 +08:00
import { rpx } from '../../utils/rpx';
2024-12-26 16:17:19 +08:00
2024-12-26 08:38:13 +08:00
interface KeyItem {
id: string;
avatarUrl: string;
shareUserName: string;
sharePhone: string;
status: string;
expirationTime: string;
}
2024-11-14 13:56:11 +08:00
const DeviceShare = () => {
const navigation = useNavigation();
2024-12-26 08:38:13 +08:00
const [keyList, setKeyList] = useState<KeyItem[]>([]);
const getKeyList = async () => {
const response = await apiService.getKeyListByOwnerId();
if (response.code == 200) {
setKeyList(response.data);
}
};
2024-12-26 16:17:19 +08:00
2024-12-26 08:38:13 +08:00
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}分钟`;
}
};
2024-12-26 16:17:19 +08:00
2024-12-26 08:38:13 +08:00
const getStatusText = (status: string | number) => {
switch (status) {
case '0':
case 0:
return '待领取';
case '1':
case 1:
return '已领取';
default:
return '未知状态';
}
};
2024-12-26 16:17:19 +08:00
2024-12-26 14:12:13 +08:00
useFocusEffect(
useCallback(() => {
getKeyList();
}, [])
);
2024-12-26 08:38:13 +08:00
2024-11-14 13:56:11 +08:00
return (
<View style={styles.container}>
<View style={styles.shareBox}>
2024-12-26 08:38:13 +08:00
<Text style={styles.shareTitle}>{keyList.length}/3</Text>
2024-11-14 13:56:11 +08:00
<Text style={styles.shareTxt}>使</Text>
2024-12-26 08:38:13 +08:00
2024-12-27 09:32:51 +08:00
{keyList.length > 0 && keyList.map((key) => (
2024-12-26 16:17:19 +08:00
<TouchableOpacity
2024-12-27 09:32:51 +08:00
key={key.id || Math.random().toString()} // 确保 key 是唯一的
2024-12-26 16:17:19 +08:00
onPress={() => {
navigation.navigate('ShareDetailScreen', { keyItem: key });
}}
>
<View style={styles.card}>
2024-12-26 08:38:13 +08:00
<View style={styles.cardTop}>
<Image
source={{ uri: key.avatarUrl || 'https://lxnapi.ccttiot.com/bike/img/static/uVnIDwcwQP7oo12PeYVJ' }}
style={styles.cardTopImg}
/>
<View style={styles.cardTopTxt}>
<Text style={styles.cardTopName}>{key.shareUserName}</Text>
<Text style={styles.cardTopPhone}>{key.sharePhone}</Text>
</View>
<Text style={[
styles.cardType,
{ color: key.status == '0' ? '#FF9900' : '#4297F3' }
]}>
{getStatusText(key.status)}
</Text>
</View>
<View style={styles.cardBottom}>
<Text style={styles.lasttime}>
{calculateRemainingTime(key.expirationTime)}
</Text>
<Image
source={{ uri: 'https://lxnapi.ccttiot.com/bike/img/static/uGq4yJlU1ZZRkwiJ8Y74' }}
style={styles.lasttimeImg}
/>
</View>
</View>
</TouchableOpacity>
))}
2024-12-26 16:17:19 +08:00
2024-12-26 08:38:13 +08:00
{keyList.length < 3 && (
<TouchableOpacity onPress={() => {
navigation.navigate('AddShare' as never);
}}>
<View style={styles.addBtn}>
<Image
source={{ uri: 'https://lxnapi.ccttiot.com/bike/img/static/uI4CdJFzS1GkY1AzfFyG' }}
style={styles.addBtnImg}
/>
<Text style={styles.addBtnTxt}></Text>
2024-11-14 13:56:11 +08:00
</View>
2024-12-26 08:38:13 +08:00
</TouchableOpacity>
)}
2024-11-14 13:56:11 +08:00
</View>
2024-12-26 14:12:13 +08:00
<TouchableOpacity onPress={() => {
2024-12-26 16:17:19 +08:00
navigation.navigate('ExpiredKeysScreen' as never);
2024-12-26 14:12:13 +08:00
}}>
<View style={styles.shareTip}>
<Text style={styles.shareBtnTxt}></Text>
<Image
2024-12-26 16:17:19 +08:00
source={{ uri: 'https://lxnapi.ccttiot.com/bike/img/static/uGq4yJlU1ZZRkwiJ8Y74' }}
style={styles.lasttimeImg}
/>
2024-12-26 14:12:13 +08:00
</View>
</TouchableOpacity>
2024-11-14 13:56:11 +08:00
</View>
);
};
2024-12-26 08:38:13 +08:00
2024-11-14 13:56:11 +08:00
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),
2024-12-26 16:17:19 +08:00
borderColor: '#808080',
2024-11-14 13:56:11 +08:00
},
});
2024-12-26 16:17:19 +08:00
export default DeviceShare;