room.tsx 13 KB

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