index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. headerBackTitleVisible: false,
  99. }} />
  100. <SafeAreaView style={{ flex: 1, marginTop: 100 }}>
  101. {/* Info Card */}
  102. <View style={styles.infoCard}>
  103. <View style={styles.balanceContainer}>
  104. <Text style={styles.balanceAmount}>{balance}</Text>
  105. <Text style={styles.balanceLabel}>余额</Text>
  106. </View>
  107. <View style={styles.actionButtons}>
  108. <TouchableOpacity
  109. style={styles.btnWithdraw}
  110. onPress={() => router.push('/wallet/withdraw')}
  111. >
  112. <Text style={styles.btnWithdrawText}>我要提现</Text>
  113. </TouchableOpacity>
  114. <TouchableOpacity
  115. style={styles.btnRecharge}
  116. onPress={() => router.push('/wallet/recharge')}
  117. >
  118. <Text style={styles.btnRechargeText}>充值</Text>
  119. </TouchableOpacity>
  120. </View>
  121. </View>
  122. {/* Tabs */}
  123. <View style={styles.tabsContainer}>
  124. {TABS.map((tab, index) => (
  125. <TouchableOpacity
  126. key={index}
  127. style={styles.tabItem}
  128. onPress={() => setActiveTab(tab)}
  129. >
  130. <Text style={[styles.tabText, activeTab.value === tab.value && styles.activeTabText]}>
  131. {tab.title}
  132. </Text>
  133. </TouchableOpacity>
  134. ))}
  135. </View>
  136. {/* Transaction List */}
  137. <View style={styles.listContainer}>
  138. <FlatList
  139. data={transactions}
  140. renderItem={renderItem}
  141. keyExtractor={(item) => item.itemId || Math.random().toString()}
  142. contentContainerStyle={styles.listContent}
  143. refreshControl={
  144. <RefreshControl refreshing={refreshing} onRefresh={onRefresh} tintColor="#333" />
  145. }
  146. onEndReached={loadMore}
  147. onEndReachedThreshold={0.5}
  148. // ListFooterComponent={loading && !refreshing ? <ActivityIndicator style={{marginTop: 10}} /> : null}
  149. ListEmptyComponent={!loading ? <View style={styles.emptyContainer}><Text style={styles.emptyText}>暂无记录</Text></View> : null}
  150. />
  151. </View>
  152. </SafeAreaView>
  153. </ImageBackground>
  154. );
  155. }
  156. const styles = StyleSheet.create({
  157. container: {
  158. flex: 1,
  159. backgroundColor: '#f5f5f5',
  160. },
  161. infoCard: {
  162. marginHorizontal: 14,
  163. marginTop: 12,
  164. marginBottom: 15,
  165. borderRadius: 10,
  166. height: 139,
  167. backgroundColor: '#3d3f41',
  168. alignItems: 'center',
  169. justifyContent: 'center',
  170. },
  171. balanceContainer: {
  172. alignItems: 'center',
  173. marginTop: 10,
  174. },
  175. balanceAmount: {
  176. fontSize: 30,
  177. fontWeight: 'bold',
  178. color: '#fff',
  179. },
  180. balanceLabel: {
  181. fontSize: 12,
  182. color: 'rgba(255,255,255,0.8)',
  183. marginTop: 2,
  184. marginBottom: 15,
  185. },
  186. actionButtons: {
  187. flexDirection: 'row',
  188. justifyContent: 'space-around',
  189. width: '75%',
  190. },
  191. btnWithdraw: {
  192. width: 90,
  193. height: 32,
  194. borderRadius: 16,
  195. borderWidth: 1,
  196. borderColor: '#fff',
  197. justifyContent: 'center',
  198. alignItems: 'center',
  199. },
  200. btnWithdrawText: {
  201. color: '#fff',
  202. fontSize: 14,
  203. },
  204. btnRecharge: {
  205. width: 90,
  206. height: 32,
  207. borderRadius: 16,
  208. backgroundColor: '#fff',
  209. justifyContent: 'center',
  210. alignItems: 'center',
  211. },
  212. btnRechargeText: {
  213. color: '#3d3f41',
  214. fontSize: 14,
  215. },
  216. tabsContainer: {
  217. flexDirection: 'row',
  218. justifyContent: 'flex-start',
  219. paddingHorizontal: 15,
  220. marginBottom: 10,
  221. },
  222. tabItem: {
  223. width: '32%',
  224. paddingVertical: 10,
  225. alignItems: 'center',
  226. justifyContent: 'center',
  227. },
  228. tabText: {
  229. color: '#3d3f41',
  230. fontSize: 14,
  231. },
  232. activeTabText: {
  233. fontWeight: 'bold',
  234. fontSize: 15,
  235. // Add underline or color change if needed, logic in Uniapp was mostly bold
  236. },
  237. listContainer: {
  238. flex: 1,
  239. marginHorizontal: 14,
  240. marginBottom: 14,
  241. borderRadius: 10,
  242. backgroundColor: '#fff',
  243. overflow: 'hidden',
  244. },
  245. listContent: {
  246. paddingBottom: 20,
  247. },
  248. cell: {
  249. flexDirection: 'row',
  250. justifyContent: 'space-between',
  251. alignItems: 'center',
  252. paddingVertical: 18,
  253. paddingHorizontal: 15,
  254. borderBottomWidth: 1,
  255. borderBottomColor: '#eee',
  256. },
  257. itemDesc: {
  258. fontSize: 14,
  259. fontWeight: 'bold',
  260. color: '#666666',
  261. },
  262. itemTime: {
  263. fontSize: 10,
  264. color: '#999',
  265. marginTop: 4,
  266. },
  267. itemAmount: {
  268. fontSize: 16,
  269. },
  270. amountIn: {
  271. color: '#ff9600', // Uniapp color-theme
  272. },
  273. amountOut: {
  274. color: '#666666', // Uniapp color-1
  275. },
  276. emptyContainer: {
  277. padding: 60,
  278. alignItems: 'center',
  279. },
  280. emptyText: {
  281. color: '#999',
  282. },
  283. });