room.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import { Image } from 'expo-image';
  2. import { useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useRef, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. Dimensions,
  7. ImageBackground,
  8. RefreshControl,
  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 { getRoomTypePermission, getWealList } from '@/services/weal';
  20. import { RuleModal, RuleModalRef } from './components/RuleModal';
  21. interface RoomItem {
  22. id: string;
  23. name: string;
  24. type: string;
  25. goodsQuantity: number;
  26. officialFlag: number;
  27. user: { avatar: string; username: string };
  28. luckRoomGoodsList: { spu: { cover: string } }[];
  29. participatingList: any[];
  30. mode?: string;
  31. }
  32. export default function RoomScreen() {
  33. const router = useRouter();
  34. const insets = useSafeAreaInsets();
  35. const [loading, setLoading] = useState(true);
  36. const [refreshing, setRefreshing] = useState(false);
  37. const [list, setList] = useState<RoomItem[]>([]);
  38. const [searchVal, setSearchVal] = useState('');
  39. const [type, setType] = useState('');
  40. const [typeIndex, setTypeIndex] = useState(0);
  41. const [scrollTop, setScrollTop] = useState(0);
  42. const [roomTab, setRoomTab] = useState([
  43. { name: '全部', value: '' }
  44. ]);
  45. const ruleRef = useRef<RuleModalRef>(null);
  46. const loadPermission = async () => {
  47. try {
  48. const permission = await getRoomTypePermission();
  49. let tabs = [{ name: '全部', value: '' }];
  50. if (permission && permission.roomConfig !== 0) {
  51. tabs.push({ name: '福利房', value: 'COMMON' });
  52. tabs.push({ name: '口令房', value: 'PASSWORD' });
  53. }
  54. if (permission && permission.roomGAS !== 0) {
  55. tabs.push({ name: '欧气房', value: 'EUROPEAN_GAS' });
  56. }
  57. if (permission && permission.roomCjf !== 0) {
  58. tabs.push({ name: '成就房', value: 'ACHIEVEMENT' });
  59. }
  60. setRoomTab(tabs);
  61. } catch (error) {
  62. console.error('获取房间类型权限失败:', error);
  63. }
  64. };
  65. const loadData = useCallback(async (isRefresh = false) => {
  66. if (isRefresh) setRefreshing(true);
  67. else setLoading(true);
  68. try {
  69. const data = await getWealList(1, 20, searchVal, type);
  70. setList(data || []);
  71. } catch (error) {
  72. console.error('加载房间列表失败:', error);
  73. }
  74. setLoading(false);
  75. setRefreshing(false);
  76. }, [type, searchVal]);
  77. useEffect(() => {
  78. loadPermission();
  79. }, []);
  80. useEffect(() => {
  81. loadData();
  82. }, [type]);
  83. const handleSearch = () => {
  84. loadData();
  85. };
  86. const handleTypeChange = (item: any, index: number) => {
  87. setTypeIndex(index);
  88. setType(item.value);
  89. };
  90. const handleRoomPress = (item: RoomItem) => {
  91. if (item.mode === 'YFS_PRO') {
  92. router.push({ pathname: '/award-detail-yfs', params: { id: item.id } } as any);
  93. } else {
  94. router.push({ pathname: '/weal/detail', params: { id: item.id } } as any);
  95. }
  96. };
  97. const isItemType = (type: string) => {
  98. const map: Record<string, string> = {
  99. COMMON: '福利房',
  100. PASSWORD: '口令房',
  101. EUROPEAN_GAS: '欧气房',
  102. ACHIEVEMENT: '成就房',
  103. };
  104. return map[type] || type;
  105. };
  106. const headerBg = scrollTop > 0 ? '#333' : 'transparent';
  107. return (
  108. <View style={styles.container}>
  109. <StatusBar barStyle="light-content" />
  110. <ImageBackground source={{ uri: Images.mine.kaixinMineBg }} style={styles.background} resizeMode="cover">
  111. {/* 顶部导航 */}
  112. <View style={[styles.header, { paddingTop: insets.top, backgroundColor: headerBg }]}>
  113. <TouchableOpacity style={styles.backBtn} onPress={() => router.back()}>
  114. <Text style={styles.backText}>←</Text>
  115. </TouchableOpacity>
  116. <Text style={styles.title}>房间</Text>
  117. <View style={styles.placeholder} />
  118. </View>
  119. {/* 头部背景 */}
  120. <ImageBackground source={{ uri: Images.mine.kaixinMineHeadBg }} style={styles.headBg} resizeMode="cover" />
  121. <ScrollView
  122. style={styles.scrollView}
  123. showsVerticalScrollIndicator={false}
  124. onScroll={(e) => setScrollTop(e.nativeEvent.contentOffset.y)}
  125. scrollEventThrottle={16}
  126. refreshControl={<RefreshControl refreshing={refreshing} onRefresh={() => loadData(true)} tintColor="#fff" />}
  127. >
  128. <View style={{ height: 90 + insets.top }} />
  129. {/* 搜索栏和功能按钮 */}
  130. <View style={styles.topSection}>
  131. <ImageBackground source={{ uri: Images.welfare.roomInputBg }} style={styles.searchBox} resizeMode="stretch">
  132. <Image source={{ uri: Images.home.search2 }} style={styles.searchIcon} contentFit="contain" />
  133. <TextInput
  134. style={styles.searchInput}
  135. placeholder="搜索"
  136. placeholderTextColor="#6C6C6C"
  137. value={searchVal}
  138. onChangeText={setSearchVal}
  139. onSubmitEditing={handleSearch}
  140. returnKeyType="search"
  141. />
  142. </ImageBackground>
  143. <View style={styles.funcBtns}>
  144. <TouchableOpacity style={styles.funcItem} onPress={() => router.push('/weal/create' as any)}>
  145. <Image source={{ uri: Images.welfare.roomIcon0 }} style={styles.funcIcon} contentFit="contain" />
  146. <Text style={styles.funcText}>创建</Text>
  147. </TouchableOpacity>
  148. <TouchableOpacity style={styles.funcItem} onPress={() => router.push('/weal/record' as any)}>
  149. <Image source={{ uri: Images.welfare.roomIcon1 }} style={styles.funcIcon} contentFit="contain" />
  150. <Text style={styles.funcText}>我的</Text>
  151. </TouchableOpacity>
  152. <TouchableOpacity style={styles.funcItem} onPress={() => ruleRef.current?.show()}>
  153. <Image source={{ uri: Images.welfare.roomIcon2 }} style={styles.funcIcon} contentFit="contain" />
  154. <Text style={styles.funcText}>玩法</Text>
  155. </TouchableOpacity>
  156. </View>
  157. </View>
  158. {/* 类型切换 */}
  159. <View style={styles.typeContainer}>
  160. <ScrollView horizontal showsHorizontalScrollIndicator={false} style={styles.typeSection}>
  161. {roomTab.map((item, index) => (
  162. <TouchableOpacity
  163. key={index}
  164. style={styles.typeItem}
  165. onPress={() => handleTypeChange(item, index)}
  166. >
  167. <Text style={[styles.typeText, typeIndex === index && styles.typeTextActive]}>{item.name}</Text>
  168. {typeIndex === index && (
  169. <>
  170. <Image source={{ uri: Images.mine.typeSelectIconT }} style={styles.typeSelectIconT} />
  171. <Image source={{ uri: Images.mine.typeSelectIconB }} style={styles.typeSelectIconB} />
  172. </>
  173. )}
  174. </TouchableOpacity>
  175. ))}
  176. </ScrollView>
  177. </View>
  178. {/* 房间列表 */}
  179. {loading ? (
  180. <ActivityIndicator size="large" color="#fff" style={{ marginTop: 50 }} />
  181. ) : list.length === 0 ? (
  182. <View style={styles.emptyBox}>
  183. <Text style={styles.emptyText}>暂无房间</Text>
  184. </View>
  185. ) : (
  186. <View style={styles.roomList}>
  187. {list.map((item) => (
  188. <TouchableOpacity key={item.id} onPress={() => handleRoomPress(item)} activeOpacity={0.8}>
  189. <ImageBackground source={{ uri: Images.welfare.roomItemBg }} style={styles.roomItem} resizeMode="stretch">
  190. {/* 官方标签 */}
  191. {item.officialFlag === 1 && (
  192. <View style={styles.officialBadge}>
  193. <Image source={{ uri: Images.welfare.official }} style={styles.officialImg} contentFit="contain" />
  194. </View>
  195. )}
  196. {/* 成就房标签 */}
  197. {item.type === 'ACHIEVEMENT' && (
  198. <View style={styles.mustBeBadge}>
  199. <Image source={{ uri: Images.welfare.mustBe }} style={styles.mustBeImg} contentFit="contain" />
  200. </View>
  201. )}
  202. {/* 商品图片 */}
  203. <ImageBackground source={{ uri: Images.welfare.roomItemImgBg }} style={styles.roomCover} resizeMode="contain">
  204. {item.luckRoomGoodsList?.[0]?.spu?.cover && (
  205. <Image source={{ uri: item.luckRoomGoodsList[0].spu.cover }} style={styles.roomCoverImg} contentFit="cover" />
  206. )}
  207. </ImageBackground>
  208. {/* 房间信息 */}
  209. <View style={styles.roomInfo}>
  210. <View style={styles.roomTop}>
  211. <Text style={styles.roomName} numberOfLines={1}>{item.name}</Text>
  212. <Text style={styles.roomTypeLabel}>{isItemType(item.type)}</Text>
  213. </View>
  214. <View style={styles.roomBottom}>
  215. <View style={styles.userInfo}>
  216. {item.user?.avatar && (
  217. <Image source={{ uri: item.user.avatar }} style={styles.userAvatar} contentFit="cover" />
  218. )}
  219. <Text style={styles.userName} numberOfLines={1}>{item.user?.username || '匿名'}</Text>
  220. </View>
  221. <Text style={styles.goodsNum}>共{item.goodsQuantity}件赠品</Text>
  222. <View style={styles.participantBox}>
  223. <Image source={{ uri: Images.welfare.participationIcon }} style={styles.participantIcon} contentFit="contain" />
  224. <Text style={styles.participantNum}>{item.participatingList?.length || 0}</Text>
  225. </View>
  226. </View>
  227. </View>
  228. </ImageBackground>
  229. </TouchableOpacity>
  230. ))}
  231. </View>
  232. )}
  233. <View style={{ height: 100 }} />
  234. </ScrollView>
  235. <RuleModal ref={ruleRef} />
  236. </ImageBackground>
  237. </View>
  238. );
  239. }
  240. const { width: SCREEN_WIDTH } = Dimensions.get('window');
  241. const styles = StyleSheet.create({
  242. container: { flex: 1, backgroundColor: '#1a1a2e' },
  243. background: { flex: 1 },
  244. header: {
  245. flexDirection: 'row',
  246. alignItems: 'center',
  247. justifyContent: 'space-between',
  248. paddingHorizontal: 15,
  249. paddingBottom: 10,
  250. position: 'absolute',
  251. top: 0,
  252. left: 0,
  253. right: 0,
  254. zIndex: 100,
  255. height: 90,
  256. },
  257. backBtn: { width: 40, height: 40, justifyContent: 'center', alignItems: 'center' },
  258. backText: { color: '#fff', fontSize: 20 },
  259. title: { color: '#fff', fontSize: 16, fontWeight: 'bold' },
  260. placeholder: { width: 40 },
  261. headBg: { position: 'absolute', top: 0, left: 0, right: 0, height: 215 },
  262. scrollView: { flex: 1 },
  263. // 顶部搜索和功能区
  264. topSection: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 15, marginTop: 10, marginBottom: 15 },
  265. searchBox: { flex: 1, height: 37, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 15 },
  266. searchIcon: { width: 19, height: 19, marginRight: 8 },
  267. searchInput: { flex: 1, fontSize: 12, color: '#000', padding: 0 },
  268. funcBtns: { flexDirection: 'row', marginLeft: 10 },
  269. funcItem: { alignItems: 'center', marginLeft: 12 },
  270. funcIcon: { width: 16, height: 16 },
  271. funcText: { fontSize: 10, color: '#DFDFDF', marginTop: 2 },
  272. // 类型切换
  273. typeContainer: { paddingHorizontal: 15, marginBottom: 40 },
  274. typeSection: {},
  275. typeItem: { marginRight: 15, width: 55, height: 38, justifyContent: 'center', alignItems: 'center', position: 'relative' },
  276. typeText: { fontSize: 12, color: '#DFDFDF' },
  277. typeTextActive: {
  278. color: '#e79018',
  279. fontWeight: 'bold',
  280. fontSize: 15,
  281. textShadowColor: '#000',
  282. textShadowOffset: { width: 1, height: 1 },
  283. textShadowRadius: 1
  284. },
  285. typeSelectIconT: {
  286. position: 'absolute',
  287. right: -5,
  288. top: 5,
  289. zIndex: 2,
  290. width: 28,
  291. height: 10,
  292. },
  293. typeSelectIconB: {
  294. position: 'absolute',
  295. left: 0,
  296. bottom: 0,
  297. zIndex: 2,
  298. width: 24,
  299. height: 14,
  300. },
  301. // 房间列表
  302. roomList: { paddingHorizontal: 0, alignItems: 'center' },
  303. roomItem: { width: SCREEN_WIDTH - 12, height: 84, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 16, marginBottom: 6, position: 'relative' },
  304. officialBadge: { position: 'absolute', right: 0, top: 0, width: 48, height: 22 },
  305. officialImg: { width: '100%', height: '100%' },
  306. mustBeBadge: { position: 'absolute', left: 0, top: 0, width: 51, height: 51 },
  307. mustBeImg: { width: '100%', height: '100%' },
  308. roomCover: { width: 58, height: 58, justifyContent: 'center', alignItems: 'center', marginRight: 14 },
  309. roomCoverImg: { width: 44, height: 44 },
  310. roomInfo: { flex: 1 },
  311. roomTop: { flexDirection: 'row', alignItems: 'center', marginBottom: 10 },
  312. roomName: {
  313. flex: 1,
  314. color: '#fff',
  315. fontSize: 14,
  316. fontWeight: '400',
  317. marginRight: 10,
  318. textShadowColor: '#000',
  319. textShadowOffset: { width: 1, height: 1 },
  320. textShadowRadius: 1
  321. },
  322. roomTypeLabel: { color: '#2E0000', fontSize: 12 },
  323. roomBottom: { flexDirection: 'row', alignItems: 'center' },
  324. userInfo: { flexDirection: 'row', alignItems: 'center', width: '45%' },
  325. userAvatar: { width: 24, height: 24, borderRadius: 2, backgroundColor: '#FFDD00', borderWidth: 1.5, borderColor: '#000', marginRight: 5 },
  326. userName: { color: '#2E0000', fontSize: 12, fontWeight: 'bold', maxWidth: 60 },
  327. goodsNum: { color: '#2E0000', fontSize: 10, width: '35%' },
  328. participantBox: { flexDirection: 'row', alignItems: 'center', width: '20%' },
  329. participantIcon: { width: 14, height: 14 },
  330. participantNum: { color: '#2E0000', fontSize: 12, marginLeft: 3 },
  331. emptyBox: { alignItems: 'center', paddingTop: 50 },
  332. emptyText: { color: '#999', fontSize: 14 },
  333. });