store_choose.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { Ionicons } from '@expo/vector-icons';
  2. import { useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. FlatList,
  7. Image,
  8. ImageBackground,
  9. StyleSheet,
  10. Text,
  11. TouchableOpacity,
  12. View,
  13. } from 'react-native';
  14. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  15. import { Images } from '@/constants/images';
  16. import { getStore } from '@/services/award';
  17. import event from '@/utils/event';
  18. const StoreChooseScreen = () => {
  19. const router = useRouter();
  20. const insets = useSafeAreaInsets();
  21. const [list, setList] = useState<any[]>([]);
  22. const [selectedIds, setSelectedIds] = useState<string[]>([]);
  23. const [loading, setLoading] = useState(false);
  24. const [refreshing, setRefreshing] = useState(false);
  25. const [page, setPage] = useState(1);
  26. const [hasMore, setHasMore] = useState(true);
  27. const [tabIndex, setTabIndex] = useState(0);
  28. const tabs = [
  29. { title: '全部', value: '' },
  30. { title: '普通', value: 'D' },
  31. { title: '隐藏', value: 'C' },
  32. { title: '欧皇', value: 'B' },
  33. { title: '超神', value: 'A' },
  34. ];
  35. const loadData = useCallback(async (isRefresh = false) => {
  36. if (loading) return;
  37. const curPage = isRefresh ? 1 : page;
  38. setLoading(true);
  39. console.log('Fetching store data:', curPage, tabs[tabIndex].value);
  40. try {
  41. const data = await getStore(curPage, 20, 0, tabs[tabIndex].value);
  42. console.log('Store data received:', data);
  43. let records = [];
  44. if (Array.isArray(data)) {
  45. records = data;
  46. } else if (data && data.records) {
  47. records = data.records;
  48. }
  49. console.log('Parsed records length:', records.length);
  50. if (records.length > 0) {
  51. setList(prev => (isRefresh ? records : [...prev, ...records]));
  52. setHasMore(records.length === 20); // Assuming page size is 20
  53. setPage(curPage + 1);
  54. } else {
  55. setList(prev => (isRefresh ? [] : prev));
  56. setHasMore(false);
  57. }
  58. } catch (err) {
  59. console.error('Fetch store error:', err);
  60. // Alert.alert('Error', '加载数据失败'); // Optional: show alert
  61. } finally {
  62. setLoading(false);
  63. setRefreshing(false);
  64. }
  65. }, [loading, page, tabIndex]);
  66. useEffect(() => {
  67. loadData(true);
  68. }, [tabIndex]);
  69. const onRefresh = () => {
  70. setRefreshing(true);
  71. loadData(true);
  72. };
  73. const onEndReached = () => {
  74. if (hasMore && !loading) {
  75. loadData();
  76. }
  77. };
  78. const toggleSelect = (id: string) => {
  79. setSelectedIds(prev =>
  80. prev.includes(id) ? prev.filter(i => i !== id) : [...prev, id]
  81. );
  82. };
  83. const handleConfirm = () => {
  84. if (selectedIds.length === 0) {
  85. router.back();
  86. return;
  87. }
  88. const selectedGoods = list.filter(item => selectedIds.includes(item.id));
  89. event.emit(event.keys.STORE_CHOOSE, selectedGoods);
  90. router.back();
  91. };
  92. const renderItem = ({ item }: { item: any }) => {
  93. const isSelected = selectedIds.includes(item.id);
  94. return (
  95. <TouchableOpacity
  96. style={styles.card}
  97. onPress={() => toggleSelect(item.id)}
  98. >
  99. <Image source={{ uri: item.spu.cover }} style={styles.goodsImg} resizeMode="contain" />
  100. <View style={styles.info}>
  101. <Text style={styles.goodsName} numberOfLines={2}>{item.spu.name}</Text>
  102. <View style={styles.tagRow}>
  103. <Text style={styles.magicText}>{item.magicAmount} 果实</Text>
  104. </View>
  105. </View>
  106. <View style={[styles.checkbox, isSelected && styles.checkboxSelected]}>
  107. {isSelected && <Ionicons name="checkmark" size={12} color="#000" />}
  108. </View>
  109. </TouchableOpacity>
  110. );
  111. };
  112. return (
  113. <View style={styles.container}>
  114. <ImageBackground
  115. source={{ uri: Images.mine.kaixinMineBg }}
  116. style={styles.background}
  117. resizeMode="cover"
  118. >
  119. <View style={[styles.header, { paddingTop: insets.top }]}>
  120. <TouchableOpacity style={styles.backBtn} onPress={() => router.back()}>
  121. <Ionicons name="chevron-back" size={24} color="#fff" />
  122. </TouchableOpacity>
  123. <Text style={styles.title}>选择商品</Text>
  124. <View style={styles.placeholder} />
  125. </View>
  126. <View style={styles.tabBar}>
  127. {tabs.map((tab, index) => (
  128. <TouchableOpacity
  129. key={tab.value}
  130. style={[styles.tabItem, tabIndex === index && styles.tabItemActive]}
  131. onPress={() => setTabIndex(index)}
  132. >
  133. <Text style={[styles.tabText, tabIndex === index && styles.tabTextActive]}>
  134. {tab.title}
  135. </Text>
  136. </TouchableOpacity>
  137. ))}
  138. </View>
  139. <FlatList
  140. data={list}
  141. renderItem={renderItem}
  142. keyExtractor={(item) => item.id.toString()}
  143. contentContainerStyle={styles.listContent}
  144. onRefresh={onRefresh}
  145. refreshing={refreshing}
  146. onEndReached={onEndReached}
  147. onEndReachedThreshold={0.5}
  148. ListEmptyComponent={
  149. !loading ? (
  150. <View style={styles.emptyContainer as any}>
  151. <Text style={styles.emptyText as any}>暂无商品</Text>
  152. </View>
  153. ) : null
  154. }
  155. ListFooterComponent={() => loading && (
  156. <ActivityIndicator size="small" color="#fff" style={{ marginVertical: 20 }} />
  157. )}
  158. />
  159. <View style={[styles.footer as any, { paddingBottom: Math.max(insets.bottom, 20) }]}>
  160. <TouchableOpacity style={styles.submitBtn as any} onPress={handleConfirm}>
  161. <ImageBackground
  162. source={{ uri: Images.common.butBgL }}
  163. style={styles.submitBtnBg as any}
  164. resizeMode="stretch"
  165. >
  166. <Text style={styles.submitBtnText}>确认选择放入赠品池</Text>
  167. </ImageBackground>
  168. </TouchableOpacity>
  169. </View>
  170. </ImageBackground>
  171. </View>
  172. );
  173. };
  174. const styles = StyleSheet.create({
  175. container: {
  176. flex: 1,
  177. },
  178. background: {
  179. flex: 1,
  180. },
  181. header: {
  182. flexDirection: 'row',
  183. alignItems: 'center',
  184. justifyContent: 'space-between',
  185. paddingHorizontal: 15,
  186. height: 90,
  187. },
  188. backBtn: {
  189. width: 40,
  190. height: 40,
  191. justifyContent: 'center',
  192. },
  193. title: {
  194. fontSize: 18,
  195. fontWeight: 'bold',
  196. color: '#fff',
  197. },
  198. placeholder: {
  199. width: 40,
  200. },
  201. tabBar: {
  202. flexDirection: 'row',
  203. justifyContent: 'space-around',
  204. paddingVertical: 10,
  205. backgroundColor: 'rgba(0,0,0,0.3)',
  206. },
  207. tabItem: {
  208. paddingVertical: 6,
  209. paddingHorizontal: 12,
  210. borderRadius: 15,
  211. },
  212. tabItemActive: {
  213. backgroundColor: 'rgba(248, 214, 104, 0.2)',
  214. },
  215. tabText: {
  216. color: '#ccc',
  217. fontSize: 14,
  218. },
  219. tabTextActive: {
  220. color: '#F8D668',
  221. fontWeight: 'bold',
  222. },
  223. listContent: {
  224. padding: 15,
  225. paddingBottom: 100,
  226. },
  227. card: {
  228. flexDirection: 'row',
  229. backgroundColor: '#fff',
  230. borderRadius: 8,
  231. padding: 10,
  232. marginBottom: 12,
  233. alignItems: 'center',
  234. position: 'relative',
  235. },
  236. goodsImg: {
  237. width: 80,
  238. height: 80,
  239. borderRadius: 4,
  240. },
  241. info: {
  242. flex: 1,
  243. marginLeft: 12,
  244. height: 80,
  245. justifyContent: 'space-around',
  246. },
  247. goodsName: {
  248. fontSize: 14,
  249. color: '#333',
  250. fontWeight: 'bold',
  251. },
  252. tagRow: {
  253. flexDirection: 'row',
  254. alignItems: 'center',
  255. },
  256. magicText: {
  257. color: '#666',
  258. fontSize: 12,
  259. },
  260. checkbox: {
  261. width: 20,
  262. height: 20,
  263. borderRadius: 10,
  264. borderWidth: 1,
  265. borderColor: '#ddd',
  266. justifyContent: 'center',
  267. alignItems: 'center',
  268. position: 'absolute',
  269. right: 10,
  270. top: 10,
  271. },
  272. checkboxSelected: {
  273. backgroundColor: '#F8D668',
  274. borderColor: '#F8D668',
  275. },
  276. footer: {
  277. position: 'absolute',
  278. bottom: 0,
  279. left: 0,
  280. right: 0,
  281. alignItems: 'center',
  282. paddingTop: 10,
  283. backgroundColor: 'rgba(0,0,0,0.5)',
  284. },
  285. submitBtn: {
  286. width: 240,
  287. height: 60,
  288. },
  289. submitBtnBg: {
  290. width: '100%',
  291. height: '100%',
  292. justifyContent: 'center',
  293. alignItems: 'center',
  294. },
  295. submitBtnText: {
  296. fontSize: 15,
  297. fontWeight: '800',
  298. color: '#fff',
  299. },
  300. emptyContainer: {
  301. alignItems: 'center',
  302. paddingTop: 100,
  303. },
  304. emptyText: {
  305. color: '#999',
  306. fontSize: 14,
  307. },
  308. });
  309. export default StoreChooseScreen;