index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { Stack, useRouter } from 'expo-router';
  2. import React, { useEffect, useState } from 'react';
  3. import { FlatList, ImageBackground, RefreshControl, SafeAreaView, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
  4. import services from '../../services/api';
  5. const TABS = [
  6. { title: '全部', value: '' },
  7. { title: '收入', value: 'IN' },
  8. { title: '支出', value: 'OUT' },
  9. ];
  10. // Define types
  11. interface TransactionItem {
  12. itemId: string;
  13. itemDesc: string;
  14. createTime: string;
  15. type: string;
  16. money: number;
  17. }
  18. export default function WalletScreen() {
  19. const router = useRouter();
  20. const [balance, setBalance] = useState('0.00');
  21. const [activeTab, setActiveTab] = useState(TABS[0]);
  22. const [transactions, setTransactions] = useState<TransactionItem[]>([]);
  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 fetchWalletInfo = async () => {
  28. try {
  29. const res = await services.wallet.info('CASH');
  30. if (res && res.balance) {
  31. setBalance(res.balance);
  32. }
  33. } catch (error) {
  34. console.error('Failed to fetch wallet info', error);
  35. }
  36. };
  37. const fetchTransactions = async (pageNum = 1, isRefresh = false) => {
  38. if (loading) return;
  39. setLoading(true);
  40. try {
  41. const res = await services.wallet.bill(pageNum, 20, 'CASH', activeTab.value);
  42. if (res && res.records) {
  43. if (isRefresh) {
  44. setTransactions(res.records);
  45. } else {
  46. setTransactions(prev => [...prev, ...res.records]);
  47. }
  48. setHasMore(res.records.length >= 20);
  49. setPage(pageNum);
  50. } else {
  51. if (isRefresh) setTransactions([]);
  52. setHasMore(false);
  53. }
  54. } catch (error) {
  55. console.error('Failed to fetch transactions', error);
  56. } finally {
  57. setLoading(false);
  58. setRefreshing(false);
  59. }
  60. };
  61. useEffect(() => {
  62. fetchWalletInfo();
  63. fetchTransactions(1, true);
  64. }, [activeTab]);
  65. const onRefresh = () => {
  66. setRefreshing(true);
  67. fetchWalletInfo();
  68. fetchTransactions(1, true);
  69. };
  70. const loadMore = () => {
  71. if (hasMore && !loading) {
  72. fetchTransactions(page + 1);
  73. }
  74. };
  75. const renderItem = ({ item }: { item: TransactionItem }) => (
  76. <View style={styles.cell}>
  77. <View>
  78. <Text style={styles.itemDesc}>{item.itemDesc}</Text>
  79. <Text style={styles.itemTime}>{item.createTime}</Text>
  80. </View>
  81. <Text style={[styles.itemAmount, item.type === 'IN' ? styles.amountIn : styles.amountOut]}>
  82. {item.type === 'IN' ? '+' : '-'}{item.money}
  83. </Text>
  84. </View>
  85. );
  86. return (
  87. <ImageBackground
  88. source={{ uri: 'https://cdn.acetoys.cn/kai_xin_ma_te/supermart/mine/kaixinMineBg.png' }}
  89. style={styles.container}
  90. resizeMode="cover"
  91. >
  92. <Stack.Screen options={{
  93. title: '钱包',
  94. headerTitleStyle: { color: '#fff' },
  95. headerStyle: { backgroundColor: 'transparent' },
  96. headerTransparent: true,
  97. headerTintColor: '#fff',
  98. }} />
  99. <SafeAreaView style={{ flex: 1, marginTop: 100 }}>
  100. {/* Info Card */}
  101. <View style={styles.infoCard}>
  102. <View style={styles.balanceContainer}>
  103. <Text style={styles.balanceAmount}>{balance}</Text>
  104. <Text style={styles.balanceLabel}>余额</Text>
  105. </View>
  106. <View style={styles.actionButtons}>
  107. <TouchableOpacity
  108. style={styles.btnWithdraw}
  109. onPress={() => router.push('/wallet/withdraw')}
  110. >
  111. <Text style={styles.btnWithdrawText}>我要提现</Text>
  112. </TouchableOpacity>
  113. <TouchableOpacity
  114. style={styles.btnRecharge}
  115. onPress={() => router.push('/wallet/recharge')}
  116. >
  117. <Text style={styles.btnRechargeText}>充值</Text>
  118. </TouchableOpacity>
  119. </View>
  120. </View>
  121. {/* Tabs */}
  122. <View style={styles.tabsContainer}>
  123. {TABS.map((tab, index) => (
  124. <TouchableOpacity
  125. key={index}
  126. style={styles.tabItem}
  127. onPress={() => setActiveTab(tab)}
  128. >
  129. <Text style={[styles.tabText, activeTab.value === tab.value && styles.activeTabText]}>
  130. {tab.title}
  131. </Text>
  132. </TouchableOpacity>
  133. ))}
  134. </View>
  135. {/* Transaction List */}
  136. <View style={styles.listContainer}>
  137. <FlatList
  138. data={transactions}
  139. renderItem={renderItem}
  140. keyExtractor={(item) => item.itemId || Math.random().toString()}
  141. contentContainerStyle={styles.listContent}
  142. refreshControl={
  143. <RefreshControl refreshing={refreshing} onRefresh={onRefresh} tintColor="#333" />
  144. }
  145. onEndReached={loadMore}
  146. onEndReachedThreshold={0.5}
  147. // ListFooterComponent={loading && !refreshing ? <ActivityIndicator style={{marginTop: 10}} /> : null}
  148. ListEmptyComponent={!loading ? <View style={styles.emptyContainer}><Text style={styles.emptyText}>暂无记录</Text></View> : null}
  149. />
  150. </View>
  151. </SafeAreaView>
  152. </ImageBackground>
  153. );
  154. }
  155. const styles = StyleSheet.create({
  156. container: {
  157. flex: 1,
  158. backgroundColor: '#f5f5f5',
  159. },
  160. infoCard: {
  161. marginHorizontal: 14,
  162. marginTop: 12,
  163. marginBottom: 15,
  164. borderRadius: 10,
  165. height: 139,
  166. backgroundColor: '#3d3f41',
  167. alignItems: 'center',
  168. justifyContent: 'center',
  169. },
  170. balanceContainer: {
  171. alignItems: 'center',
  172. marginTop: 10,
  173. },
  174. balanceAmount: {
  175. fontSize: 30,
  176. fontWeight: 'bold',
  177. color: '#fff',
  178. },
  179. balanceLabel: {
  180. fontSize: 12,
  181. color: 'rgba(255,255,255,0.8)',
  182. marginTop: 2,
  183. marginBottom: 15,
  184. },
  185. actionButtons: {
  186. flexDirection: 'row',
  187. justifyContent: 'space-around',
  188. width: '75%',
  189. },
  190. btnWithdraw: {
  191. width: 90,
  192. height: 32,
  193. borderRadius: 16,
  194. borderWidth: 1,
  195. borderColor: '#fff',
  196. justifyContent: 'center',
  197. alignItems: 'center',
  198. },
  199. btnWithdrawText: {
  200. color: '#fff',
  201. fontSize: 14,
  202. },
  203. btnRecharge: {
  204. width: 90,
  205. height: 32,
  206. borderRadius: 16,
  207. backgroundColor: '#fff',
  208. justifyContent: 'center',
  209. alignItems: 'center',
  210. },
  211. btnRechargeText: {
  212. color: '#3d3f41',
  213. fontSize: 14,
  214. },
  215. tabsContainer: {
  216. flexDirection: 'row',
  217. justifyContent: 'flex-start',
  218. paddingHorizontal: 15,
  219. marginBottom: 10,
  220. },
  221. tabItem: {
  222. width: '32%',
  223. paddingVertical: 10,
  224. alignItems: 'center',
  225. justifyContent: 'center',
  226. },
  227. tabText: {
  228. color: '#3d3f41',
  229. fontSize: 14,
  230. },
  231. activeTabText: {
  232. fontWeight: 'bold',
  233. fontSize: 15,
  234. // Add underline or color change if needed, logic in Uniapp was mostly bold
  235. },
  236. listContainer: {
  237. flex: 1,
  238. marginHorizontal: 14,
  239. marginBottom: 14,
  240. borderRadius: 10,
  241. backgroundColor: '#fff',
  242. overflow: 'hidden',
  243. },
  244. listContent: {
  245. paddingBottom: 20,
  246. },
  247. cell: {
  248. flexDirection: 'row',
  249. justifyContent: 'space-between',
  250. alignItems: 'center',
  251. paddingVertical: 18,
  252. paddingHorizontal: 15,
  253. borderBottomWidth: 1,
  254. borderBottomColor: '#eee',
  255. },
  256. itemDesc: {
  257. fontSize: 14,
  258. fontWeight: 'bold',
  259. color: '#666666',
  260. },
  261. itemTime: {
  262. fontSize: 10,
  263. color: '#999',
  264. marginTop: 4,
  265. },
  266. itemAmount: {
  267. fontSize: 16,
  268. },
  269. amountIn: {
  270. color: '#ff9600', // Uniapp color-theme
  271. },
  272. amountOut: {
  273. color: '#666666', // Uniapp color-1
  274. },
  275. emptyContainer: {
  276. padding: 60,
  277. alignItems: 'center',
  278. },
  279. emptyText: {
  280. color: '#999',
  281. },
  282. });