box.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. import { Barrage } from "@/components/Barrage";
  2. import { CouponModal } from "@/components/CouponModal";
  3. import { Colors } from "@/constants/Colors";
  4. import { Images } from "@/constants/images";
  5. import { CouponItem, getNewbieGiftBagInfo, listValidCoupon } from "@/services/activity";
  6. import { getFeedbackList, getPoolList, PoolItem } from "@/services/award";
  7. import { getToken } from "@/services/http";
  8. import { Image } from "expo-image";
  9. import { useRouter } from "expo-router";
  10. import React, { useCallback, useEffect, useMemo, useState } from "react";
  11. import {
  12. ActivityIndicator,
  13. Dimensions,
  14. FlatList,
  15. RefreshControl,
  16. StatusBar,
  17. StyleSheet,
  18. Text,
  19. TextInput,
  20. TouchableOpacity,
  21. View,
  22. } from "react-native";
  23. import { useSafeAreaInsets } from "react-native-safe-area-context";
  24. const { width: SCREEN_WIDTH } = Dimensions.get("window");
  25. const typeList = [
  26. { label: "全部", value: "", type: 0 },
  27. { label: "高保赏", value: "UNLIMITED", type: 2 },
  28. { label: "高爆赏", value: "UNLIMITED", type: 1 },
  29. { label: "擂台赏", value: "YFS_PRO", type: 7 },
  30. { label: "一番赏", value: "YFS_PRO", type: 0 },
  31. ];
  32. interface BarrageItem {
  33. id: string;
  34. content: string;
  35. nickname?: string;
  36. avatar: string;
  37. poolName?: string;
  38. type?: string;
  39. text?: string;
  40. poolId?: string;
  41. }
  42. // Static Header Component
  43. const StaticHeader = React.memo(
  44. ({ barrageList }: { barrageList: BarrageItem[] }) => (
  45. <View>
  46. <View style={{ height: 60 }} />
  47. {/* Featured Banner Area */}
  48. <View style={styles.bannerContainer}>
  49. {/* Replace image with a styled cyber container for now */}
  50. <View style={styles.cyberBanner}>
  51. <Text style={styles.bannerTitle}>MATRIX</Text>
  52. <Text style={styles.bannerSubtitle}>SUPPLY CRATE</Text>
  53. {/* Visual tech lines */}
  54. <View style={styles.techLine1} />
  55. <View style={styles.techLine2} />
  56. </View>
  57. </View>
  58. {/* Barrage Area */}
  59. {barrageList && barrageList.length > 0 && (
  60. <View style={styles.barrageSection}>
  61. <Barrage
  62. data={barrageList.slice(0, Math.ceil(barrageList.length / 2))}
  63. />
  64. <View style={{ height: 8 }} />
  65. <Barrage
  66. data={barrageList.slice(Math.ceil(barrageList.length / 2))}
  67. speed={35}
  68. />
  69. </View>
  70. )}
  71. </View>
  72. ),
  73. );
  74. // Type Selector Component
  75. const TypeSelector = React.memo(
  76. ({
  77. typeIndex,
  78. priceSort,
  79. onTypeChange,
  80. onSortChange,
  81. }: {
  82. typeIndex: number;
  83. priceSort: number;
  84. onTypeChange: (index: number) => void;
  85. onSortChange: () => void;
  86. }) => (
  87. <View style={styles.typeSection}>
  88. <View style={styles.typeListContainer}>
  89. <FlatList
  90. horizontal
  91. showsHorizontalScrollIndicator={false}
  92. data={typeList}
  93. keyExtractor={(_, index) => index.toString()}
  94. renderItem={({ item, index }) => {
  95. const isActive = typeIndex === index;
  96. return (
  97. <TouchableOpacity
  98. style={[styles.typeItem, isActive && styles.typeItemActive]}
  99. onPress={() => onTypeChange(index)}
  100. activeOpacity={0.7}
  101. >
  102. <Text
  103. style={[styles.typeText, isActive && styles.typeTextActive]}
  104. >
  105. {item.label}
  106. </Text>
  107. </TouchableOpacity>
  108. );
  109. }}
  110. contentContainerStyle={styles.typeListContent}
  111. />
  112. </View>
  113. <TouchableOpacity
  114. style={styles.sortBtn}
  115. onPress={onSortChange}
  116. activeOpacity={0.7}
  117. >
  118. <Text style={styles.sortBtnText}>
  119. 价格 {priceSort === 0 ? "-" : priceSort === 1 ? "↑" : "↓"}
  120. </Text>
  121. </TouchableOpacity>
  122. </View>
  123. ),
  124. );
  125. export default function BoxScreen() {
  126. const router = useRouter();
  127. const insets = useSafeAreaInsets();
  128. const [keyword, setKeyword] = useState("");
  129. const [typeIndex, setTypeIndex] = useState(0);
  130. const [priceSort, setPriceSort] = useState(0);
  131. const [list, setList] = useState<PoolItem[]>([]);
  132. const [loading, setLoading] = useState(false);
  133. const [refreshing, setRefreshing] = useState(false);
  134. const [current, setCurrent] = useState(1);
  135. const [hasMore, setHasMore] = useState(true);
  136. const [barrageList, setBarrageList] = useState<BarrageItem[]>([]);
  137. // 新人优惠券弹窗
  138. const [couponVisible, setCouponVisible] = useState(false);
  139. const [couponList, setCouponList] = useState<CouponItem[]>([]);
  140. const [newPeopleChecked, setNewPeopleChecked] = useState(false);
  141. // 加载弹幕
  142. const loadBarrage = useCallback(async () => {
  143. try {
  144. const res = await getFeedbackList();
  145. if (res.data) {
  146. setBarrageList(res.data);
  147. }
  148. } catch (error) {
  149. console.error("加载弹幕失败:", error);
  150. }
  151. }, []);
  152. // 新人大礼包检测(Vue: initNewPeople)
  153. const initNewPeople = useCallback(async () => {
  154. if (newPeopleChecked) return;
  155. const token = getToken();
  156. if (!token) return; // 未登录不检测
  157. try {
  158. const data = await getNewbieGiftBagInfo();
  159. if (data) {
  160. setNewPeopleChecked(true);
  161. // Vue 原项目只是展示一张海报图片,这里跳过(无具体交互)
  162. }
  163. } catch (e) {
  164. // 静默失败
  165. }
  166. }, [newPeopleChecked]);
  167. // 优惠券检测(Vue: initCoupon)
  168. const initCoupon = useCallback(async () => {
  169. const token = getToken();
  170. if (!token) return;
  171. try {
  172. const coupons = await listValidCoupon("LUCK");
  173. if (coupons && coupons.length > 0) {
  174. setCouponList(coupons);
  175. setCouponVisible(true);
  176. }
  177. } catch (e) {
  178. // 静默失败
  179. }
  180. }, []);
  181. const loadData = useCallback(
  182. async (isRefresh = false) => {
  183. if (loading) return;
  184. const page = isRefresh ? 1 : current;
  185. if (!isRefresh && !hasMore) return;
  186. setLoading(true);
  187. try {
  188. const selectedType = typeList[typeIndex];
  189. const res = await getPoolList({
  190. current: page,
  191. size: 10,
  192. mode: selectedType.value || undefined,
  193. type: selectedType.type,
  194. keyword: keyword || undefined,
  195. priceSort: priceSort || undefined,
  196. });
  197. if (res.success && res.data) {
  198. const newList = isRefresh ? res.data : [...list, ...res.data];
  199. setList(newList);
  200. setCurrent(page + 1);
  201. setHasMore(newList.length < (res.count || 0));
  202. }
  203. } catch (error) {
  204. console.error("加载奖池列表失败:", error);
  205. }
  206. setLoading(false);
  207. setRefreshing(false);
  208. },
  209. [current, hasMore, loading, typeIndex, list, keyword, priceSort],
  210. );
  211. useEffect(() => {
  212. loadData(true);
  213. loadBarrage();
  214. initNewPeople();
  215. initCoupon();
  216. }, [typeIndex, priceSort]);
  217. // 执行搜索
  218. const handleSearch = () => {
  219. setList([]);
  220. setCurrent(1);
  221. setHasMore(true);
  222. setTimeout(() => {
  223. loadData(true);
  224. }, 100);
  225. };
  226. const handleRefresh = () => {
  227. setRefreshing(true);
  228. loadData(true);
  229. };
  230. const handleLoadMore = () => {
  231. if (!loading && hasMore) {
  232. loadData(false);
  233. }
  234. };
  235. const handleTypeChange = useCallback((index: number) => {
  236. setTypeIndex(index);
  237. setList([]);
  238. setCurrent(1);
  239. setHasMore(true);
  240. }, []);
  241. const handlePriceSort = useCallback(() => {
  242. setPriceSort((prev) => (prev + 1) % 3);
  243. }, []);
  244. const handleItemPress = useCallback(
  245. (item: PoolItem) => {
  246. if (item.status !== undefined && item.status !== 1) return;
  247. if (item.type === 7) {
  248. router.push({
  249. pathname: "/boxInBox",
  250. params: { poolId: item.id },
  251. } as any);
  252. } else if (item.mode === "UNLIMITED") {
  253. router.push({
  254. pathname: "/award-detail",
  255. params: { poolId: item.id },
  256. } as any);
  257. } else if (item.mode === "YFS_PRO") {
  258. router.push({
  259. pathname: "/award-detail-yfs",
  260. params: { poolId: item.id },
  261. } as any);
  262. } else {
  263. router.push(`/product/${item.id}` as any);
  264. }
  265. },
  266. [router],
  267. );
  268. const renderItem = useCallback(
  269. ({ item }: { item: PoolItem }) => (
  270. <TouchableOpacity
  271. style={styles.itemContainer}
  272. onPress={() => handleItemPress(item)}
  273. activeOpacity={0.8}
  274. >
  275. <View style={styles.itemCard}>
  276. <View style={styles.itemImageContainer}>
  277. <Image
  278. source={{ uri: item.cover }}
  279. style={styles.itemImage}
  280. contentFit="cover"
  281. />
  282. {/* Cyber Border Overlay */}
  283. <View style={styles.itemCyberOverlay} />
  284. </View>
  285. <View style={styles.itemInfo}>
  286. <Text style={styles.itemName} numberOfLines={1}>
  287. {item.name}
  288. </Text>
  289. <Text style={styles.itemPrice}>
  290. <Text style={styles.priceUnit}>¥</Text>
  291. {item.price} <Text style={styles.priceStart}>起</Text>
  292. </Text>
  293. </View>
  294. </View>
  295. </TouchableOpacity>
  296. ),
  297. [handleItemPress],
  298. );
  299. const ListHeader = useMemo(
  300. () => (
  301. <View>
  302. <StaticHeader barrageList={barrageList} />
  303. <TypeSelector
  304. typeIndex={typeIndex}
  305. priceSort={priceSort}
  306. onTypeChange={handleTypeChange}
  307. onSortChange={handlePriceSort}
  308. />
  309. </View>
  310. ),
  311. [barrageList, typeIndex, priceSort, handleTypeChange, handlePriceSort],
  312. );
  313. const renderFooter = useCallback(() => {
  314. if (!loading) return null;
  315. return (
  316. <View style={styles.footer}>
  317. <ActivityIndicator size="small" color={Colors.neonBlue} />
  318. <Text style={styles.footerText}>数据传输中...</Text>
  319. </View>
  320. );
  321. }, [loading]);
  322. const renderEmpty = useCallback(() => {
  323. if (loading) return null;
  324. return (
  325. <View style={styles.empty}>
  326. <Text style={styles.emptyText}>暂无数据</Text>
  327. </View>
  328. );
  329. }, [loading]);
  330. return (
  331. <View style={styles.container}>
  332. <StatusBar barStyle="light-content" />
  333. <View style={styles.background}>
  334. {/* Header */}
  335. <View style={[styles.header, { paddingTop: insets.top + 10 }]}>
  336. <View style={styles.logoArea}>
  337. <Text style={styles.logoText}>
  338. CYBER<Text style={styles.logoHighlight}>BOX</Text>
  339. </Text>
  340. </View>
  341. <View style={styles.searchBar}>
  342. <Image
  343. source={{ uri: Images.home.search }}
  344. style={styles.searchIcon}
  345. contentFit="contain"
  346. tintColor={Colors.neonBlue}
  347. />
  348. <TextInput
  349. style={styles.searchInput}
  350. value={keyword}
  351. onChangeText={setKeyword}
  352. placeholder="搜索奖池"
  353. placeholderTextColor={Colors.textTertiary}
  354. returnKeyType="search"
  355. onSubmitEditing={handleSearch}
  356. />
  357. </View>
  358. </View>
  359. {/* List */}
  360. <FlatList
  361. data={list}
  362. renderItem={renderItem}
  363. keyExtractor={(item) => item.id}
  364. ListHeaderComponent={ListHeader}
  365. ListFooterComponent={renderFooter}
  366. ListEmptyComponent={renderEmpty}
  367. contentContainerStyle={styles.listContent}
  368. showsVerticalScrollIndicator={false}
  369. refreshControl={
  370. <RefreshControl
  371. refreshing={refreshing}
  372. onRefresh={handleRefresh}
  373. tintColor={Colors.neonBlue}
  374. />
  375. }
  376. onEndReached={handleLoadMore}
  377. onEndReachedThreshold={0.3}
  378. />
  379. {/* 新人优惠券弹窗 */}
  380. <CouponModal
  381. visible={couponVisible}
  382. coupons={couponList}
  383. onClose={() => setCouponVisible(false)}
  384. onSuccess={() => {
  385. setCouponList([]);
  386. loadData(true);
  387. }}
  388. />
  389. </View>
  390. </View>
  391. );
  392. }
  393. const styles = StyleSheet.create({
  394. container: {
  395. flex: 1,
  396. backgroundColor: Colors.darkBg,
  397. },
  398. background: {
  399. flex: 1,
  400. backgroundColor: Colors.darkBg,
  401. },
  402. header: {
  403. position: "relative",
  404. zIndex: 11,
  405. flexDirection: "row",
  406. alignItems: "center",
  407. paddingHorizontal: 15,
  408. paddingBottom: 10,
  409. backgroundColor: Colors.darkBg,
  410. },
  411. logoArea: {
  412. marginRight: 15,
  413. },
  414. logoText: {
  415. color: "#fff",
  416. fontWeight: "bold",
  417. fontSize: 18,
  418. fontStyle: "italic",
  419. },
  420. logoHighlight: {
  421. color: Colors.neonPink,
  422. },
  423. searchBar: {
  424. flex: 1,
  425. flexDirection: "row",
  426. alignItems: "center",
  427. backgroundColor: "rgba(255,255,255,0.05)",
  428. borderRadius: 20,
  429. paddingHorizontal: 15,
  430. height: 32,
  431. borderWidth: 1,
  432. borderColor: "rgba(0, 243, 255, 0.3)",
  433. },
  434. searchIcon: {
  435. width: 14,
  436. height: 14,
  437. marginRight: 8,
  438. },
  439. searchInput: {
  440. flex: 1,
  441. color: "#fff",
  442. fontSize: 12,
  443. padding: 0,
  444. },
  445. // Banner
  446. bannerContainer: {
  447. height: 180,
  448. marginHorizontal: 10,
  449. marginTop: 10,
  450. marginBottom: 20,
  451. borderRadius: 12,
  452. overflow: "hidden",
  453. },
  454. cyberBanner: {
  455. flex: 1,
  456. backgroundColor: Colors.darkCard,
  457. justifyContent: "center",
  458. alignItems: "center",
  459. borderWidth: 1,
  460. borderColor: Colors.neonPink,
  461. position: "relative",
  462. },
  463. bannerTitle: {
  464. fontSize: 32,
  465. fontWeight: "bold",
  466. color: "#fff",
  467. textShadowColor: Colors.neonPink,
  468. textShadowRadius: 10,
  469. fontStyle: "italic",
  470. },
  471. bannerSubtitle: {
  472. fontSize: 14,
  473. color: Colors.neonBlue,
  474. letterSpacing: 4,
  475. marginTop: 5,
  476. },
  477. techLine1: {
  478. position: "absolute",
  479. top: 10,
  480. left: 10,
  481. width: 20,
  482. height: 2,
  483. backgroundColor: Colors.neonBlue,
  484. },
  485. techLine2: {
  486. position: "absolute",
  487. bottom: 10,
  488. right: 10,
  489. width: 20,
  490. height: 2,
  491. backgroundColor: Colors.neonBlue,
  492. },
  493. // Type Selector
  494. typeSection: {
  495. flexDirection: "row",
  496. alignItems: "center",
  497. paddingHorizontal: 10,
  498. paddingVertical: 10,
  499. zIndex: 10,
  500. },
  501. typeListContainer: {
  502. flex: 1,
  503. marginRight: 5,
  504. },
  505. typeListContent: {
  506. paddingRight: 10,
  507. },
  508. typeItem: {
  509. paddingHorizontal: 12,
  510. paddingVertical: 6,
  511. borderRadius: 15,
  512. backgroundColor: "rgba(255,255,255,0.05)",
  513. marginRight: 8,
  514. borderWidth: 1,
  515. borderColor: "transparent",
  516. },
  517. typeItemActive: {
  518. backgroundColor: "rgba(0, 243, 255, 0.15)",
  519. borderColor: Colors.neonBlue,
  520. },
  521. typeText: {
  522. color: Colors.textSecondary,
  523. fontSize: 12,
  524. fontWeight: "600",
  525. },
  526. typeTextActive: {
  527. color: "#fff",
  528. textShadowColor: Colors.neonBlue,
  529. textShadowRadius: 5,
  530. },
  531. sortBtn: {
  532. paddingHorizontal: 8,
  533. paddingVertical: 6,
  534. alignItems: "center",
  535. justifyContent: "center",
  536. backgroundColor: "rgba(255,255,255,0.05)",
  537. borderRadius: 15,
  538. },
  539. sortBtnText: {
  540. color: Colors.textTertiary,
  541. fontSize: 12,
  542. },
  543. // List
  544. listContent: {
  545. paddingHorizontal: 10,
  546. paddingBottom: 100,
  547. },
  548. itemContainer: {
  549. marginBottom: 12,
  550. },
  551. itemCard: {
  552. width: "100%",
  553. backgroundColor: Colors.darkCard,
  554. borderRadius: 8,
  555. overflow: "hidden",
  556. borderWidth: 1,
  557. borderColor: "rgba(0, 243, 255, 0.2)",
  558. },
  559. itemImageContainer: {
  560. height: 160,
  561. width: "100%",
  562. position: "relative",
  563. },
  564. itemImage: {
  565. width: "100%",
  566. height: "100%",
  567. },
  568. itemCyberOverlay: {
  569. ...StyleSheet.absoluteFillObject,
  570. backgroundColor: "rgba(0,0,0,0.1)", // Slight dark overlay
  571. borderBottomWidth: 1,
  572. borderBottomColor: Colors.neonBlue,
  573. },
  574. itemInfo: {
  575. flexDirection: "row",
  576. justifyContent: "space-between",
  577. alignItems: "center",
  578. padding: 12,
  579. },
  580. itemName: {
  581. flex: 1,
  582. color: Colors.textPrimary,
  583. fontSize: 14,
  584. fontWeight: "bold",
  585. },
  586. itemPrice: {
  587. color: Colors.neonBlue,
  588. fontSize: 16,
  589. fontWeight: "bold",
  590. marginLeft: 10,
  591. textShadowColor: Colors.neonBlue,
  592. textShadowRadius: 5,
  593. },
  594. priceUnit: {
  595. fontSize: 12,
  596. marginRight: 2,
  597. },
  598. priceStart: {
  599. fontSize: 10,
  600. color: Colors.textTertiary,
  601. fontWeight: "normal",
  602. },
  603. // Footer
  604. footer: {
  605. paddingVertical: 15,
  606. alignItems: "center",
  607. flexDirection: "row",
  608. justifyContent: "center",
  609. },
  610. footerText: {
  611. color: Colors.textTertiary,
  612. fontSize: 12,
  613. marginLeft: 8,
  614. },
  615. empty: {
  616. alignItems: "center",
  617. paddingVertical: 50,
  618. },
  619. emptyText: {
  620. color: Colors.textTertiary,
  621. fontSize: 14,
  622. },
  623. // Barrage
  624. barrageSection: {
  625. marginVertical: 10,
  626. paddingHorizontal: 5, // Edge to edge
  627. },
  628. });