detail.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import { Image } from 'expo-image';
  2. import { useLocalSearchParams, useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useRef, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. Alert,
  7. ImageBackground,
  8. Modal,
  9. ScrollView,
  10. StatusBar,
  11. StyleSheet,
  12. Text,
  13. TextInput,
  14. TouchableOpacity,
  15. View,
  16. } from 'react-native';
  17. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  18. import { Images } from '@/constants/images';
  19. import { getWealDetail, getWinningRecord, joinWealRoom } from '@/services/weal';
  20. const TYPE_MAP: any = {
  21. COMMON: { title: '福利房' },
  22. PASSWORD: { title: '口令房' },
  23. ACHIEVEMENT: { title: '成就房' },
  24. EUROPEAN_GAS: { title: '欧气房' },
  25. HONOR_ROLL: { title: '荣耀榜' },
  26. };
  27. export default function WealDetailScreen() {
  28. const { id } = useLocalSearchParams<{ id: string }>();
  29. const router = useRouter();
  30. const insets = useSafeAreaInsets();
  31. const [loading, setLoading] = useState(true);
  32. const [data, setData] = useState<any>(null);
  33. const [leftTime, setLeftTime] = useState(0);
  34. const [scrollTop, setScrollTop] = useState(0);
  35. const [winVisible, setWinVisible] = useState(false);
  36. const [winRecords, setWinRecords] = useState([]);
  37. const [joinVisible, setJoinVisible] = useState(false);
  38. const [password, setPassword] = useState('');
  39. const timerRef = useRef<any>(null);
  40. const loadData = useCallback(async (showLoading = false) => {
  41. if (showLoading) setLoading(true);
  42. try {
  43. const res = await getWealDetail(id as string);
  44. if (res) {
  45. setData(res);
  46. setLeftTime(res.leftTime);
  47. }
  48. } catch (error) {
  49. console.error('加载详情失败:', error);
  50. }
  51. setLoading(false);
  52. }, [id]);
  53. useEffect(() => {
  54. loadData(true);
  55. return () => stopTimer();
  56. }, [loadData]);
  57. useEffect(() => {
  58. if (data?.status === 1 && leftTime > 0) {
  59. startTimer();
  60. } else {
  61. stopTimer();
  62. }
  63. }, [data, leftTime]);
  64. const startTimer = () => {
  65. stopTimer();
  66. timerRef.current = setInterval(() => {
  67. setLeftTime(prev => {
  68. if (prev <= 0) {
  69. stopTimer();
  70. loadData();
  71. return 0;
  72. }
  73. return prev - 1000;
  74. });
  75. }, 1000);
  76. };
  77. const stopTimer = () => {
  78. if (timerRef.current) {
  79. clearInterval(timerRef.current);
  80. timerRef.current = null;
  81. }
  82. };
  83. const formatLeftTime = () => {
  84. if (leftTime <= 0) return '00:00:00';
  85. let second = Math.floor(leftTime / 1000);
  86. const d = Math.floor(second / (24 * 3600));
  87. second %= 24 * 3600;
  88. const h = Math.floor(second / 3600);
  89. second %= 3600;
  90. const m = Math.floor(second / 60);
  91. const s = second % 60;
  92. let res = '';
  93. if (d > 0) res += `${d}天`;
  94. res += `${h.toString().padStart(2, '0')}时${m.toString().padStart(2, '0')}分${s.toString().padStart(2, '0')}秒`;
  95. return res;
  96. };
  97. const handleJoin = async () => {
  98. if (data.status !== 1 || data.myParticipatedFlag === 1) return;
  99. if (data.type === 'PASSWORD') {
  100. setJoinVisible(true);
  101. } else {
  102. try {
  103. const res = await joinWealRoom(id as string, '');
  104. if (res.success) {
  105. Alert.alert('提示', '加入成功');
  106. loadData();
  107. } else {
  108. Alert.alert('错误', res.msg || '加入失败');
  109. }
  110. } catch (error) {
  111. Alert.alert('错误', '请求异常');
  112. }
  113. }
  114. };
  115. const handleJoinWithPassword = async () => {
  116. if (!password) return;
  117. try {
  118. const res = await joinWealRoom(id as string, password);
  119. if (res.success) {
  120. Alert.alert('提示', '加入成功');
  121. setJoinVisible(false);
  122. setPassword('');
  123. loadData();
  124. } else {
  125. Alert.alert('错误', res.msg || '口令错误');
  126. }
  127. } catch (error) {
  128. Alert.alert('错误', '请求异常');
  129. }
  130. };
  131. const showWinRecords = async () => {
  132. try {
  133. const res = await getWinningRecord(id as string);
  134. setWinRecords(res || []);
  135. setWinVisible(true);
  136. } catch (error) {
  137. console.error('获取中奖记录失败:', error);
  138. }
  139. };
  140. if (loading) {
  141. return <View style={styles.loading}><ActivityIndicator color="#fff" /></View>;
  142. }
  143. const headerBg = scrollTop > 50 ? '#333' : 'transparent';
  144. return (
  145. <View style={styles.container}>
  146. <StatusBar barStyle="light-content" />
  147. <ImageBackground source={{ uri: Images.mine.kaixinMineBg }} style={styles.background} resizeMode="cover">
  148. {/* 导航 */}
  149. <View style={[styles.nav, { paddingTop: insets.top, backgroundColor: headerBg }]}>
  150. <TouchableOpacity onPress={() => router.back()} style={styles.backBtn}>
  151. <Text style={styles.backText}>←</Text>
  152. </TouchableOpacity>
  153. <Text style={styles.navTitle}>{TYPE_MAP[data.type]?.title || '详情'}</Text>
  154. <View style={styles.placeholder} />
  155. </View>
  156. <ScrollView
  157. style={styles.scrollView}
  158. onScroll={e => setScrollTop(e.nativeEvent.contentOffset.y)}
  159. scrollEventThrottle={16}
  160. showsVerticalScrollIndicator={false}
  161. >
  162. <ImageBackground source={{ uri: Images.mine.kaixinMineHeadBg }} style={styles.headerBg} resizeMode="cover" />
  163. <View style={styles.content}>
  164. <View style={styles.roomInfo}>
  165. <Text style={styles.roomName}>{data.name}</Text>
  166. <View style={styles.roomType}>
  167. <Text style={styles.roomTypeText}>{TYPE_MAP[data.type]?.title}</Text>
  168. </View>
  169. <Text style={styles.roomDesc} numberOfLines={1}>{data.description}</Text>
  170. </View>
  171. {/* 中奖记录按钮 */}
  172. {data.prizeMode !== 1 && data.type !== 'EUROPEAN_GAS' && (
  173. <TouchableOpacity style={styles.recordBtn} onPress={showWinRecords}>
  174. <ImageBackground source={{ uri: Images.welfare.detail.record }} style={styles.recordBtnBg} resizeMode="contain">
  175. <Image source={{ uri: Images.welfare.detail.recordIcon }} style={styles.recordIcon} contentFit="contain" />
  176. <Text style={styles.recordBtnText}>中奖记录</Text>
  177. </ImageBackground>
  178. </TouchableOpacity>
  179. )}
  180. {/* 赠品池 */}
  181. <View style={styles.goodsCard}>
  182. <View style={styles.cardHeader}>
  183. <Text style={styles.cardTitle}>赠品池</Text>
  184. <Text style={styles.cardNum}>{data.luckRoomGoodsList?.length}件赠品</Text>
  185. </View>
  186. <View style={styles.goodsList}>
  187. {data.luckRoomGoodsList?.map((item: any, index: number) => (
  188. <View key={index} style={styles.goodsItem}>
  189. <Image source={{ uri: item.spu.cover }} style={styles.goodsImg} contentFit="contain" />
  190. <Text style={styles.goodsName} numberOfLines={1}>{item.spu.name}</Text>
  191. <View style={styles.goodsCountTag}>
  192. <Text style={styles.goodsCountText}>数量:{item.quantity}</Text>
  193. </View>
  194. </View>
  195. ))}
  196. </View>
  197. </View>
  198. {/* 参与度 */}
  199. <View style={styles.participantSection}>
  200. <View style={styles.cardHeader}>
  201. <Text style={styles.cardTitle}>参与度</Text>
  202. <Text style={styles.cardNum}>{data.participatingList?.length}个玩家</Text>
  203. </View>
  204. <ScrollView horizontal showsHorizontalScrollIndicator={false} style={styles.userList}>
  205. {data.participatingList?.map((user: any, index: number) => (
  206. <View key={index} style={styles.userItem}>
  207. <Image source={{ uri: user.avatar }} style={styles.userAvatar} />
  208. <Text style={styles.userName} numberOfLines={1}>{user.nickname}</Text>
  209. </View>
  210. ))}
  211. </ScrollView>
  212. </View>
  213. </View>
  214. <View style={{ height: 120 }} />
  215. </ScrollView>
  216. {/* 底部按钮栏 */}
  217. <View style={[styles.bottomBar, { paddingBottom: insets.bottom + 10 }]}>
  218. {data.status === 1 && (
  219. <View style={styles.timerRow}>
  220. <Text style={styles.timerLabel}>倒计时:</Text>
  221. <Text style={styles.timerValue}>{formatLeftTime()}</Text>
  222. </View>
  223. )}
  224. <TouchableOpacity
  225. style={[styles.joinBtn, (data.myParticipatedFlag === 1 || data.status !== 1) && styles.joinBtnDisabled]}
  226. onPress={handleJoin}
  227. disabled={data.myParticipatedFlag === 1 || data.status !== 1}
  228. >
  229. <ImageBackground
  230. source={{ uri: data.status === 1 ? Images.common.loginBtn : undefined }}
  231. style={styles.joinBtnBg}
  232. resizeMode="stretch"
  233. >
  234. <Text style={styles.joinBtnText}>
  235. {data.status !== 1 ? '已开奖' : data.myParticipatedFlag === 1 ? (
  236. data.participateMode === 1 && data.myAuditStatus === 0 ? '待审核' :
  237. data.participateMode === 1 && data.myAuditStatus === 1 ? '审核不通过' :
  238. '等待开赏'
  239. ) : '加入房间,即可参与'}
  240. </Text>
  241. </ImageBackground>
  242. </TouchableOpacity>
  243. </View>
  244. {/* 口令弹窗 */}
  245. <Modal visible={joinVisible} transparent animationType="fade">
  246. <View style={styles.modalOverlay}>
  247. <View style={styles.modalContent}>
  248. <Text style={styles.modalTitle}>请输入房间口令</Text>
  249. <TextInput
  250. style={styles.modalInput}
  251. value={password}
  252. onChangeText={setPassword}
  253. placeholder="请输入口令"
  254. />
  255. <View style={styles.modalBtns}>
  256. <TouchableOpacity style={styles.modalBtn} onPress={() => setJoinVisible(false)}>
  257. <Text style={styles.modalBtnText}>取消</Text>
  258. </TouchableOpacity>
  259. <TouchableOpacity style={[styles.modalBtn, styles.modalBtnConfirm]} onPress={handleJoinWithPassword}>
  260. <Text style={styles.modalBtnText}>确认加入</Text>
  261. </TouchableOpacity>
  262. </View>
  263. </View>
  264. </View>
  265. </Modal>
  266. {/* 中奖记录弹窗 */}
  267. <Modal visible={winVisible} transparent animationType="slide">
  268. <View style={styles.winOverlay}>
  269. <View style={styles.winContent}>
  270. <View style={styles.winHeader}>
  271. <Text style={styles.winTitle}>中奖记录</Text>
  272. <TouchableOpacity onPress={() => setWinVisible(false)} style={styles.winClose}>
  273. <Text style={styles.winCloseText}>×</Text>
  274. </TouchableOpacity>
  275. </View>
  276. <ScrollView style={styles.winList}>
  277. {winRecords.length === 0 ? (
  278. <View style={styles.empty}><Text style={{ color: '#999' }}>暂无记录</Text></View>
  279. ) : winRecords.map((item: any, index: number) => (
  280. <View key={index} style={styles.winItem}>
  281. <Image source={{ uri: item.avatar }} style={styles.winAvatar} />
  282. <Text style={styles.winUser}>{item.nickname}</Text>
  283. <Text style={styles.winGot}>获得了</Text>
  284. <Text style={styles.winGoods} numberOfLines={1}>{item.spu.name}</Text>
  285. <Image source={{ uri: item.spu.cover }} style={styles.winGoodsImg} contentFit="contain" />
  286. </View>
  287. ))}
  288. </ScrollView>
  289. </View>
  290. </View>
  291. </Modal>
  292. </ImageBackground>
  293. </View>
  294. );
  295. }
  296. const styles = StyleSheet.create({
  297. container: { flex: 1 },
  298. background: { flex: 1 },
  299. loading: { flex: 1, backgroundColor: '#1a1a2e', justifyContent: 'center', alignItems: 'center' },
  300. nav: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 15, height: 90, zIndex: 100 },
  301. backBtn: { width: 40 },
  302. backText: { color: '#fff', fontSize: 24 },
  303. navTitle: { color: '#fff', fontSize: 16, fontWeight: 'bold' },
  304. placeholder: { width: 40 },
  305. scrollView: { flex: 1 },
  306. headerBg: { width: '100%', height: 180, position: 'absolute', top: 0 },
  307. content: { paddingHorizontal: 15, marginTop: 90 },
  308. roomInfo: { alignItems: 'center', marginBottom: 20 },
  309. roomName: { color: '#fff', fontSize: 22, fontWeight: 'bold', textShadowColor: '#000', textShadowOffset: { width: 1, height: 1 }, textShadowRadius: 2 },
  310. roomType: { marginTop: 8, paddingVertical: 4, paddingHorizontal: 12, backgroundColor: 'rgba(255,255,255,0.2)', borderRadius: 10 },
  311. roomTypeText: { color: '#fff', fontSize: 12 },
  312. roomDesc: { marginTop: 10, color: '#BEBBB3', fontSize: 12 },
  313. recordBtn: { position: 'absolute', right: 0, top: 0, zIndex: 10 },
  314. recordBtnBg: { width: 78, height: 26, justifyContent: 'center', alignItems: 'center', flexDirection: 'row' },
  315. recordIcon: { width: 16, height: 16, marginRight: 2 },
  316. recordBtnText: { color: '#fff', fontSize: 12, fontWeight: 'bold', textShadowColor: '#000', textShadowOffset: { width: 1, height: 1 }, textShadowRadius: 1 },
  317. goodsCard: { marginTop: 25, backgroundColor: 'rgba(255,255,255,0.1)', borderRadius: 15, padding: 15 },
  318. cardHeader: { flexDirection: 'row', alignItems: 'center', marginBottom: 15 },
  319. cardTitle: { color: '#fff', fontSize: 18, fontWeight: 'bold' },
  320. cardNum: { color: '#eee', fontSize: 12, marginLeft: 10 },
  321. goodsList: { flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between' },
  322. goodsItem: { width: '31%', aspectRatio: 0.8, backgroundColor: 'rgba(0,0,0,0.3)', borderRadius: 10, padding: 8, marginBottom: 10, alignItems: 'center' },
  323. goodsImg: { width: '80%', height: '60%' },
  324. goodsName: { color: '#fff', fontSize: 10, marginTop: 5 },
  325. goodsCountTag: { position: 'absolute', left: 0, bottom: 25, backgroundColor: '#FFDD00', paddingHorizontal: 5, borderTopRightRadius: 5, borderBottomRightRadius: 5 },
  326. goodsCountText: { color: '#000', fontSize: 8, fontWeight: 'bold' },
  327. participantSection: { marginTop: 20, backgroundColor: 'rgba(255,255,255,0.1)', borderRadius: 15, padding: 15 },
  328. userList: { flexDirection: 'row' },
  329. userItem: { alignItems: 'center', marginRight: 15, width: 50 },
  330. userAvatar: { width: 40, height: 40, borderRadius: 20, borderWidth: 1, borderColor: '#fff' },
  331. userName: { color: '#fff', fontSize: 8, marginTop: 5, width: '100%', textAlign: 'center' },
  332. bottomBar: { position: 'absolute', left: 0, right: 0, bottom: 0, backgroundColor: 'rgba(0,0,0,0.8)', paddingHorizontal: 15, paddingTop: 10 },
  333. timerRow: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', marginBottom: 10 },
  334. timerLabel: { color: '#fff', fontSize: 12 },
  335. timerValue: { color: '#fdf685', fontSize: 12, fontWeight: 'bold' },
  336. joinBtn: { height: 45, borderRadius: 22.5, width: '100%', overflow: 'hidden' },
  337. joinBtnDisabled: { backgroundColor: '#666' },
  338. joinBtnBg: { width: '100%', height: '100%', justifyContent: 'center', alignItems: 'center' },
  339. joinBtnText: { color: '#fff', fontSize: 14, fontWeight: 'bold' },
  340. modalOverlay: { flex: 1, backgroundColor: 'rgba(0,0,0,0.6)', justifyContent: 'center', alignItems: 'center' },
  341. modalContent: { width: '80%', backgroundColor: '#fff', borderRadius: 15, padding: 20, alignItems: 'center' },
  342. modalTitle: { fontSize: 18, fontWeight: 'bold', marginBottom: 20 },
  343. modalInput: { width: '100%', height: 45, backgroundColor: '#f5f5f5', borderRadius: 8, paddingHorizontal: 15, marginBottom: 20 },
  344. modalBtns: { flexDirection: 'row', justifyContent: 'space-between', width: '100%' },
  345. modalBtn: { flex: 0.45, height: 40, justifyContent: 'center', alignItems: 'center', borderRadius: 20, backgroundColor: '#eee' },
  346. modalBtnConfirm: { backgroundColor: '#e79018' },
  347. modalBtnText: { fontWeight: 'bold' },
  348. winOverlay: { flex: 1, backgroundColor: 'rgba(0,0,0,0.5)', justifyContent: 'flex-end' },
  349. winContent: { backgroundColor: '#fff', height: '60%', borderTopLeftRadius: 20, borderTopRightRadius: 20, padding: 20 },
  350. winHeader: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 20 },
  351. winTitle: { fontSize: 18, fontWeight: 'bold' },
  352. winClose: { width: 30, height: 30, backgroundColor: '#eee', borderRadius: 15, justifyContent: 'center', alignItems: 'center' },
  353. winCloseText: { fontSize: 20, color: '#999' },
  354. winList: { flex: 1 },
  355. winItem: { flexDirection: 'row', alignItems: 'center', paddingVertical: 10, borderBottomWidth: 1, borderBottomColor: '#f5f5f5' },
  356. winAvatar: { width: 30, height: 30, borderRadius: 15 },
  357. winUser: { marginLeft: 10, fontSize: 12, width: 60 },
  358. winGot: { fontSize: 12, color: '#999', marginHorizontal: 5 },
  359. winGoods: { flex: 1, fontSize: 12 },
  360. winGoodsImg: { width: 30, height: 30, marginLeft: 10 },
  361. empty: { alignItems: 'center', padding: 30 },
  362. });