box.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import { Image } from 'expo-image';
  2. import { useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. FlatList,
  7. ImageBackground,
  8. RefreshControl,
  9. StatusBar,
  10. StyleSheet,
  11. Text,
  12. TextInput,
  13. TouchableOpacity,
  14. View,
  15. } from 'react-native';
  16. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  17. import { Images } from '@/constants/images';
  18. import { getFeedbackList, getPoolList, PoolItem } from '@/services/award';
  19. const typeList = [
  20. { label: '全部', value: '', type: 0, img: Images.box.type1, imgOn: Images.box.type1On },
  21. { label: '高保赏', value: 'UNLIMITED', type: 2, img: Images.box.type3, imgOn: Images.box.type3On },
  22. { label: '高爆赏', value: 'UNLIMITED', type: 1, img: Images.box.type2, imgOn: Images.box.type2On },
  23. { label: '擂台赏', value: 'YFS_PRO', type: 7, img: Images.box.type5, imgOn: Images.box.type5On },
  24. { label: '一番赏', value: 'YFS_PRO', type: 0, img: Images.box.type4, imgOn: Images.box.type4On },
  25. ];
  26. interface BarrageItem {
  27. id: string;
  28. content: string;
  29. nickname?: string;
  30. }
  31. export default function BoxScreen() {
  32. const router = useRouter();
  33. const insets = useSafeAreaInsets();
  34. const [keyword, setKeyword] = useState('');
  35. const [typeIndex, setTypeIndex] = useState(0);
  36. const [priceSort, setPriceSort] = useState(0);
  37. const [list, setList] = useState<PoolItem[]>([]);
  38. const [loading, setLoading] = useState(false);
  39. const [refreshing, setRefreshing] = useState(false);
  40. const [current, setCurrent] = useState(1);
  41. const [total, setTotal] = useState(0);
  42. const [hasMore, setHasMore] = useState(true);
  43. const [barrageList, setBarrageList] = useState<BarrageItem[]>([]);
  44. // 加载弹幕
  45. const loadBarrage = useCallback(async () => {
  46. try {
  47. const res = await getFeedbackList();
  48. if (res.data) {
  49. setBarrageList(res.data);
  50. }
  51. } catch (error) {
  52. console.error('加载弹幕失败:', error);
  53. }
  54. }, []);
  55. const loadData = useCallback(async (isRefresh = false) => {
  56. if (loading) return;
  57. const page = isRefresh ? 1 : current;
  58. if (!isRefresh && !hasMore) return;
  59. setLoading(true);
  60. try {
  61. const selectedType = typeList[typeIndex];
  62. const res = await getPoolList({
  63. current: page,
  64. size: 10,
  65. mode: selectedType.value || undefined,
  66. type: selectedType.type,
  67. keyword: keyword || undefined,
  68. priceSort: priceSort || undefined,
  69. });
  70. if (res.success && res.data) {
  71. const newList = isRefresh ? res.data : [...list, ...res.data];
  72. setList(newList);
  73. setTotal(res.count || 0);
  74. setCurrent(page + 1);
  75. setHasMore(newList.length < (res.count || 0));
  76. }
  77. } catch (error) {
  78. console.error('加载奖池列表失败:', error);
  79. }
  80. setLoading(false);
  81. setRefreshing(false);
  82. }, [current, hasMore, loading, typeIndex, list, keyword, priceSort]);
  83. useEffect(() => {
  84. loadData(true);
  85. loadBarrage();
  86. }, [typeIndex, priceSort]);
  87. const handleRefresh = () => {
  88. setRefreshing(true);
  89. loadData(true);
  90. };
  91. const handleLoadMore = () => {
  92. if (!loading && hasMore) {
  93. loadData(false);
  94. }
  95. };
  96. const handleTypeChange = (index: number) => {
  97. setTypeIndex(index);
  98. setList([]);
  99. setCurrent(1);
  100. setHasMore(true);
  101. };
  102. const handlePriceSort = () => {
  103. setPriceSort((prev) => (prev + 1) % 3);
  104. };
  105. const handleItemPress = (item: PoolItem) => {
  106. // 检查商品状态
  107. if (item.status !== undefined && item.status !== 1) return;
  108. console.log('点击商品:', item.id, 'mode:', item.mode, 'type:', item.type);
  109. // 根据类型跳转到不同页面 - 按照小程序逻辑
  110. if (item.type === 7) {
  111. // 擂台赏跳转到 boxInBox 页面
  112. router.push({ pathname: '/boxInBox', params: { poolId: item.id } } as any);
  113. } else if (item.mode === 'UNLIMITED') {
  114. // 高爆赏/高保赏
  115. router.push({ pathname: '/award-detail', params: { poolId: item.id } } as any);
  116. } else if (item.mode === 'YFS_PRO') {
  117. // 一番赏
  118. router.push({ pathname: '/award-detail-yfs', params: { poolId: item.id } } as any);
  119. } else {
  120. // 其他商品
  121. router.push(`/product/${item.id}` as any);
  122. }
  123. };
  124. const renderItem = ({ item }: { item: PoolItem }) => (
  125. <TouchableOpacity
  126. style={styles.itemContainer}
  127. onPress={() => handleItemPress(item)}
  128. activeOpacity={0.8}
  129. >
  130. <ImageBackground
  131. source={{ uri: Images.box.goodsItemBg }}
  132. style={styles.itemBg}
  133. resizeMode="stretch"
  134. >
  135. <Image
  136. source={{ uri: item.cover }}
  137. style={styles.itemImage}
  138. contentFit="cover"
  139. />
  140. <View style={styles.itemInfo}>
  141. <Text style={styles.itemName} numberOfLines={1}>{item.name}</Text>
  142. <Text style={styles.itemPrice}>
  143. <Text style={styles.priceUnit}>¥</Text>
  144. {item.price}起
  145. </Text>
  146. </View>
  147. </ImageBackground>
  148. </TouchableOpacity>
  149. );
  150. const renderHeader = () => (
  151. <View>
  152. {/* 占位空间 - 给顶部搜索栏留出空间 */}
  153. <View style={{ height: 53 }} />
  154. {/* 顶部主图 - 绝对定位,叠在背景上 */}
  155. <View style={styles.mainImageContainer}>
  156. <Image
  157. source={{ uri: Images.box.awardMainImg }}
  158. style={styles.mainImage}
  159. contentFit="contain"
  160. />
  161. </View>
  162. {/* 占位空间 - 主图高度 */}
  163. <View style={{ height: 300 }} />
  164. {/* 弹幕区域 */}
  165. {barrageList.length > 0 && (
  166. <View style={styles.barrageSection}>
  167. <View style={styles.barrageRow}>
  168. {barrageList.slice(0, 3).map((item, index) => (
  169. <View key={item.id || index} style={styles.barrageItem}>
  170. <Text style={styles.barrageText} numberOfLines={1}>{item.content}</Text>
  171. </View>
  172. ))}
  173. </View>
  174. <View style={[styles.barrageRow, { marginLeft: 25 }]}>
  175. {barrageList.slice(3, 6).map((item, index) => (
  176. <View key={item.id || index} style={styles.barrageItem}>
  177. <Text style={styles.barrageText} numberOfLines={1}>{item.content}</Text>
  178. </View>
  179. ))}
  180. </View>
  181. </View>
  182. )}
  183. {/* 分类筛选 */}
  184. <View style={styles.typeSection}>
  185. <View style={styles.typeList}>
  186. {typeList.map((item, index) => (
  187. <TouchableOpacity
  188. key={index}
  189. style={styles.typeItem}
  190. onPress={() => handleTypeChange(index)}
  191. >
  192. <Image
  193. source={{ uri: typeIndex === index ? item.imgOn : item.img }}
  194. style={styles.typeImage}
  195. contentFit="contain"
  196. />
  197. </TouchableOpacity>
  198. ))}
  199. </View>
  200. <TouchableOpacity style={styles.sortBtn} onPress={handlePriceSort}>
  201. <Image
  202. source={{
  203. uri: priceSort === 0
  204. ? Images.box.sortAmount
  205. : priceSort === 1
  206. ? Images.box.sortAmountOnT
  207. : Images.box.sortAmountOnB,
  208. }}
  209. style={styles.sortIcon}
  210. contentFit="contain"
  211. />
  212. </TouchableOpacity>
  213. </View>
  214. </View>
  215. );
  216. const renderFooter = () => {
  217. if (!loading) return null;
  218. return (
  219. <View style={styles.footer}>
  220. <ActivityIndicator size="small" color="#fff" />
  221. <Text style={styles.footerText}>加载中...</Text>
  222. </View>
  223. );
  224. };
  225. const renderEmpty = () => {
  226. if (loading) return null;
  227. return (
  228. <View style={styles.empty}>
  229. <Text style={styles.emptyText}>暂无数据</Text>
  230. </View>
  231. );
  232. };
  233. return (
  234. <View style={styles.container}>
  235. <StatusBar barStyle="light-content" />
  236. <ImageBackground
  237. source={{ uri: Images.common.indexBg }}
  238. style={styles.background}
  239. resizeMode="cover"
  240. >
  241. {/* 顶部搜索栏 */}
  242. <View style={[styles.header, { paddingTop: insets.top + 10 }]}>
  243. <Image
  244. source={{ uri: Images.home.portrait }}
  245. style={styles.logo}
  246. contentFit="contain"
  247. />
  248. <View style={styles.searchBar}>
  249. <Image
  250. source={{ uri: Images.home.search }}
  251. style={styles.searchIcon}
  252. contentFit="contain"
  253. />
  254. <TextInput
  255. style={styles.searchInput}
  256. value={keyword}
  257. onChangeText={setKeyword}
  258. placeholder="搜索"
  259. placeholderTextColor="rgba(255,255,255,0.5)"
  260. returnKeyType="search"
  261. onSubmitEditing={() => loadData(true)}
  262. />
  263. </View>
  264. </View>
  265. {/* 列表 */}
  266. <FlatList
  267. data={list}
  268. renderItem={renderItem}
  269. keyExtractor={(item) => item.id}
  270. ListHeaderComponent={renderHeader}
  271. ListFooterComponent={renderFooter}
  272. ListEmptyComponent={renderEmpty}
  273. contentContainerStyle={styles.listContent}
  274. showsVerticalScrollIndicator={false}
  275. refreshControl={
  276. <RefreshControl
  277. refreshing={refreshing}
  278. onRefresh={handleRefresh}
  279. tintColor="#fff"
  280. />
  281. }
  282. onEndReached={handleLoadMore}
  283. onEndReachedThreshold={0.3}
  284. />
  285. </ImageBackground>
  286. </View>
  287. );
  288. }
  289. const styles = StyleSheet.create({
  290. container: {
  291. flex: 1,
  292. backgroundColor: '#1a1a2e',
  293. },
  294. background: {
  295. flex: 1,
  296. },
  297. header: {
  298. position: 'relative',
  299. zIndex: 11,
  300. flexDirection: 'row',
  301. alignItems: 'center',
  302. paddingHorizontal: 15,
  303. paddingBottom: 10,
  304. },
  305. logo: {
  306. width: 67,
  307. height: 25,
  308. marginRight: 20,
  309. },
  310. searchBar: {
  311. flex: 1,
  312. flexDirection: 'row',
  313. alignItems: 'center',
  314. backgroundColor: 'rgba(255,255,255,0.38)',
  315. borderRadius: 180,
  316. paddingHorizontal: 15,
  317. height: 28,
  318. },
  319. searchIcon: {
  320. width: 15,
  321. height: 15,
  322. marginRight: 5,
  323. },
  324. searchInput: {
  325. flex: 1,
  326. color: '#fff',
  327. fontSize: 12,
  328. padding: 0,
  329. },
  330. mainImageContainer: {
  331. position: 'absolute',
  332. left: 0,
  333. top: 0,
  334. zIndex: 2,
  335. width: '100%',
  336. height: 395,
  337. },
  338. mainImage: {
  339. width: '100%',
  340. height: '100%',
  341. },
  342. typeSection: {
  343. flexDirection: 'row',
  344. alignItems: 'center',
  345. paddingHorizontal: 10,
  346. paddingVertical: 10,
  347. },
  348. typeList: {
  349. flex: 1,
  350. flexDirection: 'row',
  351. justifyContent: 'space-around',
  352. },
  353. typeItem: {
  354. width: 75,
  355. height: 30,
  356. },
  357. typeImage: {
  358. width: '100%',
  359. height: '100%',
  360. },
  361. sortBtn: {
  362. width: '10%',
  363. alignItems: 'center',
  364. },
  365. sortIcon: {
  366. width: 20,
  367. height: 20,
  368. },
  369. listContent: {
  370. paddingHorizontal: 10,
  371. paddingBottom: 100,
  372. },
  373. itemContainer: {
  374. marginBottom: 8,
  375. },
  376. itemBg: {
  377. width: '100%',
  378. height: 210,
  379. padding: 8,
  380. },
  381. itemImage: {
  382. width: '100%',
  383. height: 142,
  384. borderRadius: 8,
  385. },
  386. itemInfo: {
  387. paddingHorizontal: 15,
  388. paddingTop: 15,
  389. },
  390. itemName: {
  391. color: '#fff',
  392. fontSize: 14,
  393. },
  394. itemPrice: {
  395. color: '#ff0000',
  396. fontSize: 12,
  397. fontWeight: 'bold',
  398. marginTop: 5,
  399. },
  400. priceUnit: {
  401. fontSize: 12,
  402. marginRight: 2,
  403. },
  404. footer: {
  405. flexDirection: 'row',
  406. justifyContent: 'center',
  407. alignItems: 'center',
  408. paddingVertical: 15,
  409. },
  410. footerText: {
  411. color: 'rgba(255,255,255,0.6)',
  412. fontSize: 12,
  413. marginLeft: 8,
  414. },
  415. empty: {
  416. alignItems: 'center',
  417. paddingVertical: 50,
  418. },
  419. emptyText: {
  420. color: 'rgba(255,255,255,0.6)',
  421. fontSize: 14,
  422. },
  423. barrageSection: {
  424. marginVertical: 10,
  425. paddingHorizontal: 10,
  426. },
  427. barrageRow: {
  428. flexDirection: 'row',
  429. marginBottom: 5,
  430. },
  431. barrageItem: {
  432. backgroundColor: 'rgba(0,0,0,0.5)',
  433. borderRadius: 15,
  434. paddingHorizontal: 12,
  435. paddingVertical: 6,
  436. marginRight: 8,
  437. maxWidth: 150,
  438. },
  439. barrageText: {
  440. color: '#fff',
  441. fontSize: 12,
  442. },
  443. });