room.tsx 13 KB

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