box.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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.type === 7) {
  108. // 擂台赏跳转到 boxInBox 页面
  109. router.push(`/boxInBox?poolId=${item.id}` as any);
  110. } else if (item.mode === 'UNLIMITED') {
  111. router.push(`/award-detail?poolId=${item.id}` as any);
  112. } else if (item.mode === 'YFS_PRO') {
  113. router.push(`/award-detail-yfs?poolId=${item.id}` as any);
  114. } else {
  115. router.push(`/product/${item.id}` as any);
  116. }
  117. };
  118. const renderItem = ({ item }: { item: PoolItem }) => (
  119. <TouchableOpacity
  120. style={styles.itemContainer}
  121. onPress={() => handleItemPress(item)}
  122. activeOpacity={0.8}
  123. >
  124. <ImageBackground
  125. source={{ uri: Images.box.goodsItemBg }}
  126. style={styles.itemBg}
  127. resizeMode="stretch"
  128. >
  129. <Image
  130. source={{ uri: item.cover }}
  131. style={styles.itemImage}
  132. contentFit="cover"
  133. />
  134. <View style={styles.itemInfo}>
  135. <Text style={styles.itemName} numberOfLines={1}>{item.name}</Text>
  136. <Text style={styles.itemPrice}>
  137. <Text style={styles.priceUnit}>¥</Text>
  138. {item.price}起
  139. </Text>
  140. </View>
  141. </ImageBackground>
  142. </TouchableOpacity>
  143. );
  144. const renderHeader = () => (
  145. <View>
  146. {/* 顶部主图 */}
  147. <Image
  148. source={{ uri: Images.box.awardMainImg }}
  149. style={styles.mainImage}
  150. contentFit="contain"
  151. />
  152. {/* 弹幕区域 */}
  153. {barrageList.length > 0 && (
  154. <View style={styles.barrageSection}>
  155. <View style={styles.barrageRow}>
  156. {barrageList.slice(0, 3).map((item, index) => (
  157. <View key={item.id || index} style={styles.barrageItem}>
  158. <Text style={styles.barrageText} numberOfLines={1}>{item.content}</Text>
  159. </View>
  160. ))}
  161. </View>
  162. <View style={[styles.barrageRow, { marginLeft: 25 }]}>
  163. {barrageList.slice(3, 6).map((item, index) => (
  164. <View key={item.id || index} style={styles.barrageItem}>
  165. <Text style={styles.barrageText} numberOfLines={1}>{item.content}</Text>
  166. </View>
  167. ))}
  168. </View>
  169. </View>
  170. )}
  171. {/* 分类筛选 */}
  172. <View style={styles.typeSection}>
  173. <View style={styles.typeList}>
  174. {typeList.map((item, index) => (
  175. <TouchableOpacity
  176. key={index}
  177. style={styles.typeItem}
  178. onPress={() => handleTypeChange(index)}
  179. >
  180. <Image
  181. source={{ uri: typeIndex === index ? item.imgOn : item.img }}
  182. style={styles.typeImage}
  183. contentFit="contain"
  184. />
  185. </TouchableOpacity>
  186. ))}
  187. </View>
  188. <TouchableOpacity style={styles.sortBtn} onPress={handlePriceSort}>
  189. <Image
  190. source={{
  191. uri: priceSort === 0
  192. ? Images.box.sortAmount
  193. : priceSort === 1
  194. ? Images.box.sortAmountOnT
  195. : Images.box.sortAmountOnB,
  196. }}
  197. style={styles.sortIcon}
  198. contentFit="contain"
  199. />
  200. </TouchableOpacity>
  201. </View>
  202. </View>
  203. );
  204. const renderFooter = () => {
  205. if (!loading) return null;
  206. return (
  207. <View style={styles.footer}>
  208. <ActivityIndicator size="small" color="#fff" />
  209. <Text style={styles.footerText}>加载中...</Text>
  210. </View>
  211. );
  212. };
  213. const renderEmpty = () => {
  214. if (loading) return null;
  215. return (
  216. <View style={styles.empty}>
  217. <Text style={styles.emptyText}>暂无数据</Text>
  218. </View>
  219. );
  220. };
  221. return (
  222. <View style={styles.container}>
  223. <StatusBar barStyle="light-content" />
  224. <ImageBackground
  225. source={{ uri: Images.common.awardBg }}
  226. style={styles.background}
  227. resizeMode="cover"
  228. >
  229. {/* 顶部搜索栏 */}
  230. <View style={[styles.header, { paddingTop: insets.top + 10 }]}>
  231. <Image
  232. source={{ uri: Images.home.portrait }}
  233. style={styles.logo}
  234. contentFit="contain"
  235. />
  236. <View style={styles.searchBar}>
  237. <Image
  238. source={{ uri: Images.home.search }}
  239. style={styles.searchIcon}
  240. contentFit="contain"
  241. />
  242. <TextInput
  243. style={styles.searchInput}
  244. value={keyword}
  245. onChangeText={setKeyword}
  246. placeholder="搜索"
  247. placeholderTextColor="rgba(255,255,255,0.5)"
  248. returnKeyType="search"
  249. onSubmitEditing={() => loadData(true)}
  250. />
  251. </View>
  252. </View>
  253. {/* 列表 */}
  254. <FlatList
  255. data={list}
  256. renderItem={renderItem}
  257. keyExtractor={(item) => item.id}
  258. ListHeaderComponent={renderHeader}
  259. ListFooterComponent={renderFooter}
  260. ListEmptyComponent={renderEmpty}
  261. contentContainerStyle={styles.listContent}
  262. showsVerticalScrollIndicator={false}
  263. refreshControl={
  264. <RefreshControl
  265. refreshing={refreshing}
  266. onRefresh={handleRefresh}
  267. tintColor="#fff"
  268. />
  269. }
  270. onEndReached={handleLoadMore}
  271. onEndReachedThreshold={0.3}
  272. />
  273. </ImageBackground>
  274. </View>
  275. );
  276. }
  277. const styles = StyleSheet.create({
  278. container: {
  279. flex: 1,
  280. backgroundColor: '#1a1a2e',
  281. },
  282. background: {
  283. flex: 1,
  284. },
  285. header: {
  286. flexDirection: 'row',
  287. alignItems: 'center',
  288. paddingHorizontal: 15,
  289. paddingBottom: 10,
  290. },
  291. logo: {
  292. width: 67,
  293. height: 25,
  294. marginRight: 20,
  295. },
  296. searchBar: {
  297. flex: 1,
  298. flexDirection: 'row',
  299. alignItems: 'center',
  300. backgroundColor: 'rgba(255,255,255,0.38)',
  301. borderRadius: 180,
  302. paddingHorizontal: 15,
  303. height: 28,
  304. },
  305. searchIcon: {
  306. width: 15,
  307. height: 15,
  308. marginRight: 5,
  309. },
  310. searchInput: {
  311. flex: 1,
  312. color: '#fff',
  313. fontSize: 12,
  314. padding: 0,
  315. },
  316. mainImage: {
  317. width: '100%',
  318. height: 395,
  319. },
  320. typeSection: {
  321. flexDirection: 'row',
  322. alignItems: 'center',
  323. paddingHorizontal: 10,
  324. paddingVertical: 10,
  325. },
  326. typeList: {
  327. flex: 1,
  328. flexDirection: 'row',
  329. justifyContent: 'space-around',
  330. },
  331. typeItem: {
  332. width: 75,
  333. height: 30,
  334. },
  335. typeImage: {
  336. width: '100%',
  337. height: '100%',
  338. },
  339. sortBtn: {
  340. width: '10%',
  341. alignItems: 'center',
  342. },
  343. sortIcon: {
  344. width: 20,
  345. height: 20,
  346. },
  347. listContent: {
  348. paddingHorizontal: 10,
  349. paddingBottom: 100,
  350. },
  351. itemContainer: {
  352. marginBottom: 8,
  353. },
  354. itemBg: {
  355. width: '100%',
  356. height: 210,
  357. padding: 8,
  358. },
  359. itemImage: {
  360. width: '100%',
  361. height: 142,
  362. borderRadius: 8,
  363. },
  364. itemInfo: {
  365. paddingHorizontal: 15,
  366. paddingTop: 15,
  367. },
  368. itemName: {
  369. color: '#fff',
  370. fontSize: 14,
  371. },
  372. itemPrice: {
  373. color: '#ff0000',
  374. fontSize: 12,
  375. fontWeight: 'bold',
  376. marginTop: 5,
  377. },
  378. priceUnit: {
  379. fontSize: 12,
  380. marginRight: 2,
  381. },
  382. footer: {
  383. flexDirection: 'row',
  384. justifyContent: 'center',
  385. alignItems: 'center',
  386. paddingVertical: 15,
  387. },
  388. footerText: {
  389. color: 'rgba(255,255,255,0.6)',
  390. fontSize: 12,
  391. marginLeft: 8,
  392. },
  393. empty: {
  394. alignItems: 'center',
  395. paddingVertical: 50,
  396. },
  397. emptyText: {
  398. color: 'rgba(255,255,255,0.6)',
  399. fontSize: 14,
  400. },
  401. barrageSection: {
  402. marginVertical: 10,
  403. paddingHorizontal: 10,
  404. },
  405. barrageRow: {
  406. flexDirection: 'row',
  407. marginBottom: 5,
  408. },
  409. barrageItem: {
  410. backgroundColor: 'rgba(0,0,0,0.5)',
  411. borderRadius: 15,
  412. paddingHorizontal: 12,
  413. paddingVertical: 6,
  414. marginRight: 8,
  415. maxWidth: 150,
  416. },
  417. barrageText: {
  418. color: '#fff',
  419. fontSize: 12,
  420. },
  421. });