detail.tsx 20 KB

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