index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { Image } from 'expo-image';
  2. import { useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. ImageBackground,
  7. RefreshControl,
  8. ScrollView,
  9. StatusBar,
  10. StyleSheet,
  11. Text,
  12. TouchableOpacity,
  13. View
  14. } from 'react-native';
  15. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  16. import { Images } from '@/constants/images';
  17. import { getOrders, OrderItem } from '@/services/mall';
  18. const tabs = [
  19. { label: '全部', value: 'all' },
  20. { label: '待付款', value: 'to_pay' },
  21. { label: '待补款', value: 'to_payLeft' },
  22. { label: '待发货', value: 'to_delivery' },
  23. { label: '待收货', value: 'to_receive' },
  24. { label: '已完成', value: 'complete' },
  25. { label: '未完成', value: 'uncomplete' },
  26. ];
  27. export default function OrdersScreen() {
  28. const router = useRouter();
  29. const insets = useSafeAreaInsets();
  30. const [loading, setLoading] = useState(true);
  31. const [refreshing, setRefreshing] = useState(false);
  32. const [activeTab, setActiveTab] = useState(0);
  33. const [orders, setOrders] = useState<OrderItem[]>([]);
  34. const [page, setPage] = useState(1);
  35. const loadData = useCallback(async (tab?: number, refresh = false) => {
  36. if (refresh) {
  37. setRefreshing(true);
  38. setPage(1);
  39. } else {
  40. setLoading(true);
  41. }
  42. try {
  43. const data = await getOrders(refresh ? 1 : page, 10, tab);
  44. if (data?.records) {
  45. setOrders(refresh ? data.records : [...orders, ...data.records]);
  46. }
  47. } catch (error) {
  48. console.error('加载订单失败:', error);
  49. }
  50. setLoading(false);
  51. setRefreshing(false);
  52. }, [page, orders]);
  53. useEffect(() => {
  54. loadData(tabs[activeTab].value, true);
  55. }, [activeTab]);
  56. const onRefresh = () => {
  57. loadData(tabs[activeTab].value, true);
  58. };
  59. const switchTab = (index: number) => {
  60. if (index !== activeTab) {
  61. setActiveTab(index);
  62. setOrders([]);
  63. }
  64. };
  65. const goToDetail = (tradeNo: string) => {
  66. router.push(`/orders/${tradeNo}` as any);
  67. };
  68. const goBack = () => {
  69. router.back();
  70. };
  71. return (
  72. <View style={styles.container}>
  73. <StatusBar barStyle="light-content" />
  74. <ImageBackground
  75. source={{ uri: Images.mine.kaixinMineBg }}
  76. style={styles.background}
  77. resizeMode="cover"
  78. >
  79. {/* 顶部导航 */}
  80. <View style={[styles.header, { paddingTop: insets.top }]}>
  81. <TouchableOpacity style={styles.backBtn} onPress={goBack}>
  82. <Text style={styles.backText}>←</Text>
  83. </TouchableOpacity>
  84. <Text style={styles.headerTitle}>我的订单</Text>
  85. <View style={styles.placeholder} />
  86. </View>
  87. {/* Tab 栏 */}
  88. <View style={styles.tabBar}>
  89. {tabs.map((tab, index) => (
  90. <TouchableOpacity
  91. key={index}
  92. style={[styles.tabItem, activeTab === index && styles.tabItemActive]}
  93. onPress={() => switchTab(index)}
  94. >
  95. <Text style={[styles.tabText, activeTab === index && styles.tabTextActive]}>
  96. {tab.label}
  97. </Text>
  98. </TouchableOpacity>
  99. ))}
  100. </View>
  101. <ScrollView
  102. style={styles.scrollView}
  103. showsVerticalScrollIndicator={false}
  104. refreshControl={
  105. <RefreshControl refreshing={refreshing} onRefresh={onRefresh} tintColor="#fff" />
  106. }
  107. >
  108. {loading && orders.length === 0 ? (
  109. <View style={styles.loadingContainer}>
  110. <ActivityIndicator size="large" color="#fff" />
  111. </View>
  112. ) : orders.length === 0 ? (
  113. <View style={styles.emptyContainer}>
  114. <Text style={styles.emptyText}>暂无订单</Text>
  115. </View>
  116. ) : (
  117. orders.map((item) => (
  118. <TouchableOpacity
  119. key={item.tradeNo}
  120. style={styles.orderItem}
  121. onPress={() => goToDetail(item.tradeNo)}
  122. >
  123. <ImageBackground
  124. source={{ uri: Images.common.itemBg }}
  125. style={styles.orderItemBg}
  126. resizeMode="stretch"
  127. >
  128. <View style={styles.orderHeader}>
  129. <Text style={styles.orderNo}>订单号:{item.tradeNo}</Text>
  130. <Text style={styles.orderStatus}>{item.statusText}</Text>
  131. </View>
  132. <View style={styles.orderContent}>
  133. <Image source={item.goodsCover} style={styles.goodsImage} contentFit="cover" />
  134. <View style={styles.goodsInfo}>
  135. <Text style={styles.goodsName} numberOfLines={2}>{item.goodsName}</Text>
  136. <View style={styles.goodsBottom}>
  137. <Text style={styles.goodsPrice}>¥{item.paymentAmount}</Text>
  138. <Text style={styles.goodsQty}>x{item.quantity}</Text>
  139. </View>
  140. </View>
  141. </View>
  142. <View style={styles.orderFooter}>
  143. <Text style={styles.orderTime}>{item.createTime}</Text>
  144. <View style={styles.orderActions}>
  145. {item.status === 1 && (
  146. <TouchableOpacity style={styles.actionBtn}>
  147. <Text style={styles.actionBtnText}>去支付</Text>
  148. </TouchableOpacity>
  149. )}
  150. {item.status === 3 && (
  151. <TouchableOpacity style={styles.actionBtn}>
  152. <Text style={styles.actionBtnText}>确认收货</Text>
  153. </TouchableOpacity>
  154. )}
  155. </View>
  156. </View>
  157. </ImageBackground>
  158. </TouchableOpacity>
  159. ))
  160. )}
  161. <View style={{ height: 20 }} />
  162. </ScrollView>
  163. </ImageBackground>
  164. </View>
  165. );
  166. }
  167. const styles = StyleSheet.create({
  168. container: {
  169. flex: 1,
  170. backgroundColor: '#1a1a2e',
  171. },
  172. background: {
  173. flex: 1,
  174. },
  175. header: {
  176. flexDirection: 'row',
  177. alignItems: 'center',
  178. justifyContent: 'space-between',
  179. paddingHorizontal: 15,
  180. paddingBottom: 10,
  181. },
  182. backBtn: {
  183. width: 40,
  184. height: 40,
  185. justifyContent: 'center',
  186. alignItems: 'center',
  187. },
  188. backText: {
  189. color: '#fff',
  190. fontSize: 24,
  191. },
  192. headerTitle: {
  193. color: '#fff',
  194. fontSize: 18,
  195. fontWeight: '600',
  196. },
  197. placeholder: {
  198. width: 40,
  199. },
  200. tabBar: {
  201. flexDirection: 'row',
  202. backgroundColor: 'rgba(255,255,255,0.1)',
  203. marginHorizontal: 15,
  204. borderRadius: 8,
  205. paddingVertical: 5,
  206. },
  207. tabItem: {
  208. flex: 1,
  209. alignItems: 'center',
  210. paddingVertical: 8,
  211. },
  212. tabItemActive: {
  213. borderBottomWidth: 2,
  214. borderBottomColor: '#ff6b00',
  215. },
  216. tabText: {
  217. color: '#999',
  218. fontSize: 14,
  219. },
  220. tabTextActive: {
  221. color: '#ff6b00',
  222. fontWeight: '600',
  223. },
  224. scrollView: {
  225. flex: 1,
  226. paddingHorizontal: 15,
  227. },
  228. loadingContainer: {
  229. paddingTop: 100,
  230. alignItems: 'center',
  231. },
  232. emptyContainer: {
  233. paddingTop: 100,
  234. alignItems: 'center',
  235. },
  236. emptyText: {
  237. color: '#999',
  238. fontSize: 14,
  239. },
  240. orderItem: {
  241. marginTop: 10,
  242. },
  243. orderItemBg: {
  244. borderRadius: 12,
  245. overflow: 'hidden',
  246. },
  247. orderHeader: {
  248. flexDirection: 'row',
  249. justifyContent: 'space-between',
  250. alignItems: 'center',
  251. padding: 12,
  252. borderBottomWidth: 1,
  253. borderBottomColor: 'rgba(0,0,0,0.1)',
  254. },
  255. orderNo: {
  256. color: '#666',
  257. fontSize: 12,
  258. },
  259. orderStatus: {
  260. color: '#ff6b00',
  261. fontSize: 13,
  262. fontWeight: '500',
  263. },
  264. orderContent: {
  265. flexDirection: 'row',
  266. padding: 12,
  267. },
  268. goodsImage: {
  269. width: 80,
  270. height: 80,
  271. borderRadius: 8,
  272. backgroundColor: '#fff',
  273. },
  274. goodsInfo: {
  275. flex: 1,
  276. marginLeft: 12,
  277. justifyContent: 'space-between',
  278. },
  279. goodsName: {
  280. color: '#333',
  281. fontSize: 14,
  282. lineHeight: 20,
  283. },
  284. goodsBottom: {
  285. flexDirection: 'row',
  286. justifyContent: 'space-between',
  287. alignItems: 'center',
  288. },
  289. goodsPrice: {
  290. color: '#ff6b00',
  291. fontSize: 16,
  292. fontWeight: '600',
  293. },
  294. goodsQty: {
  295. color: '#666',
  296. fontSize: 13,
  297. },
  298. orderFooter: {
  299. flexDirection: 'row',
  300. justifyContent: 'space-between',
  301. alignItems: 'center',
  302. padding: 12,
  303. borderTopWidth: 1,
  304. borderTopColor: 'rgba(0,0,0,0.1)',
  305. },
  306. orderTime: {
  307. color: '#999',
  308. fontSize: 12,
  309. },
  310. orderActions: {
  311. flexDirection: 'row',
  312. },
  313. actionBtn: {
  314. paddingHorizontal: 16,
  315. paddingVertical: 6,
  316. backgroundColor: '#ff6b00',
  317. borderRadius: 15,
  318. marginLeft: 10,
  319. },
  320. actionBtnText: {
  321. color: '#fff',
  322. fontSize: 13,
  323. },
  324. });