checkout.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { Image } from 'expo-image';
  2. import { useLocalSearchParams, useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. Alert,
  7. ImageBackground,
  8. Platform,
  9. ScrollView,
  10. StatusBar,
  11. StyleSheet,
  12. Text,
  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 { Address, getDefaultAddress } from '@/services/address';
  19. import { takeApply, takePreview } from '@/services/award';
  20. interface GroupedGoods {
  21. total: number;
  22. data: { id: string; cover: string; spuId: string; level: string };
  23. }
  24. export default function CheckoutScreen() {
  25. const router = useRouter();
  26. const insets = useSafeAreaInsets();
  27. const { ids } = useLocalSearchParams<{ ids: string }>();
  28. const [loading, setLoading] = useState(true);
  29. const [submitting, setSubmitting] = useState(false);
  30. const [address, setAddress] = useState<Address | null>(null);
  31. const [expressAmount, setExpressAmount] = useState(0);
  32. const [goodsList, setGoodsList] = useState<GroupedGoods[]>([]);
  33. const [inventoryIds, setInventoryIds] = useState<string[]>([]);
  34. const showAlert = (msg: string) => {
  35. if (Platform.OS === 'web') window.alert(msg);
  36. else Alert.alert('提示', msg);
  37. };
  38. const loadData = useCallback(async () => {
  39. if (!ids) return;
  40. setLoading(true);
  41. try {
  42. const idList = ids.split(',');
  43. setInventoryIds(idList);
  44. // 获取默认地址
  45. const addr = await getDefaultAddress();
  46. setAddress(addr);
  47. // 获取提货预览
  48. const res = await takePreview(idList, addr?.id || '');
  49. if (res) {
  50. setExpressAmount(res.expressAmount || 0);
  51. // 合并相同商品
  52. const goodsMap: Record<string, GroupedGoods> = {};
  53. (res.itemList || []).forEach((item: any) => {
  54. const key = `${item.spuId}_${item.level}`;
  55. if (goodsMap[key]) {
  56. goodsMap[key].total += 1;
  57. } else {
  58. goodsMap[key] = { total: 1, data: item };
  59. }
  60. });
  61. setGoodsList(Object.values(goodsMap));
  62. }
  63. } catch (e) {
  64. console.error('加载提货信息失败:', e);
  65. }
  66. setLoading(false);
  67. }, [ids]);
  68. useEffect(() => {
  69. loadData();
  70. }, [loadData]);
  71. const goToAddress = () => {
  72. router.push('/address?type=1' as any);
  73. };
  74. const handleSubmit = async () => {
  75. if (!address) {
  76. showAlert('请选择收货地址');
  77. return;
  78. }
  79. if (submitting) return;
  80. setSubmitting(true);
  81. try {
  82. const res = await takeApply(inventoryIds, address.id, 'ALIPAY_H5');
  83. if (res) {
  84. showAlert('提货成功');
  85. router.back();
  86. }
  87. } catch (e) {
  88. console.error('提货失败:', e);
  89. }
  90. setSubmitting(false);
  91. };
  92. if (loading) {
  93. return (
  94. <View style={styles.loadingContainer}>
  95. <ActivityIndicator size="large" color="#fff" />
  96. </View>
  97. );
  98. }
  99. return (
  100. <View style={styles.container}>
  101. <StatusBar barStyle="light-content" />
  102. <ImageBackground source={{ uri: Images.mine.kaixinMineBg }} style={styles.background} resizeMode="cover">
  103. <Image source={{ uri: Images.mine.kaixinMineHeadBg }} style={styles.headerBg} contentFit="cover" />
  104. {/* 顶部导航 */}
  105. <View style={[styles.header, { paddingTop: insets.top }]}>
  106. <TouchableOpacity style={styles.backBtn} onPress={() => router.back()}>
  107. <Text style={styles.backIcon}>‹</Text>
  108. </TouchableOpacity>
  109. <Text style={styles.title}>提货</Text>
  110. <View style={styles.placeholder} />
  111. </View>
  112. <ScrollView style={[styles.content, { paddingTop: insets.top + 50 }]} showsVerticalScrollIndicator={false}>
  113. {/* 商品列表 */}
  114. <View style={styles.goodsSection}>
  115. <ScrollView horizontal showsHorizontalScrollIndicator={false}>
  116. {goodsList.map((goods, idx) => (
  117. <View key={idx} style={styles.goodsItem}>
  118. <Image source={{ uri: goods.data.cover }} style={styles.goodsImg} contentFit="contain" />
  119. <View style={styles.goodsCount}>
  120. <Text style={styles.goodsCountText}>x{goods.total}</Text>
  121. </View>
  122. </View>
  123. ))}
  124. </ScrollView>
  125. </View>
  126. {/* 运费 */}
  127. <View style={styles.feeRow}>
  128. <Text style={styles.feeLabel}>运费</Text>
  129. <Text style={styles.feeValue}>¥{expressAmount}</Text>
  130. </View>
  131. {/* 收货地址 */}
  132. <TouchableOpacity style={styles.addressSection} onPress={goToAddress}>
  133. {!address ? (
  134. <Text style={styles.noAddress}>请填写收货地址</Text>
  135. ) : (
  136. <View style={styles.addressInfo}>
  137. <Text style={styles.addressName}>{address.contactName} {address.contactNo}</Text>
  138. <Text style={styles.addressDetail}>{address.province}{address.city}{address.district}{address.address}</Text>
  139. </View>
  140. )}
  141. <Text style={styles.arrowIcon}>›</Text>
  142. </TouchableOpacity>
  143. </ScrollView>
  144. {/* 底部按钮 */}
  145. <View style={[styles.bottomBar, { paddingBottom: insets.bottom + 10 }]}>
  146. <TouchableOpacity style={styles.submitBtn} onPress={handleSubmit} disabled={submitting}>
  147. <ImageBackground source={{ uri: Images.common.loginBtn }} style={styles.submitBtnBg} resizeMode="contain">
  148. <Text style={styles.submitBtnText}>{submitting ? '提交中...' : '确定发货'}</Text>
  149. </ImageBackground>
  150. </TouchableOpacity>
  151. </View>
  152. </ImageBackground>
  153. </View>
  154. );
  155. }
  156. const styles = StyleSheet.create({
  157. container: { flex: 1, backgroundColor: '#1a1a2e' },
  158. background: { flex: 1 },
  159. loadingContainer: { flex: 1, backgroundColor: '#1a1a2e', justifyContent: 'center', alignItems: 'center' },
  160. headerBg: { position: 'absolute', top: 0, left: 0, width: '100%', height: 160 },
  161. header: { position: 'absolute', top: 0, left: 0, right: 0, zIndex: 100, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 10, paddingBottom: 10 },
  162. backBtn: { width: 40, height: 40, justifyContent: 'center', alignItems: 'center' },
  163. backIcon: { fontSize: 32, color: '#fff', fontWeight: 'bold' },
  164. title: { color: '#fff', fontSize: 16, fontWeight: 'bold' },
  165. placeholder: { width: 40 },
  166. content: { flex: 1, paddingHorizontal: 15 },
  167. goodsSection: { backgroundColor: 'rgba(255,255,255,0.1)', borderRadius: 10, padding: 15, marginBottom: 15 },
  168. goodsItem: { width: 79, height: 103, backgroundColor: '#fff', borderRadius: 6, marginRight: 10, alignItems: 'center', justifyContent: 'center', position: 'relative', borderWidth: 1, borderColor: '#eaeaea' },
  169. goodsImg: { width: 73, height: 85 },
  170. goodsCount: { position: 'absolute', top: 0, right: 0, backgroundColor: '#ff6b00', borderRadius: 2, paddingHorizontal: 4, paddingVertical: 2 },
  171. goodsCountText: { color: '#fff', fontSize: 10, fontWeight: 'bold' },
  172. feeRow: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', backgroundColor: 'rgba(255,255,255,0.1)', borderRadius: 10, padding: 15, marginBottom: 15 },
  173. feeLabel: { color: '#fff', fontSize: 14 },
  174. feeValue: { color: '#ff6b00', fontSize: 16, fontWeight: 'bold' },
  175. addressSection: { flexDirection: 'row', alignItems: 'center', backgroundColor: 'rgba(255,255,255,0.1)', borderRadius: 10, padding: 15, marginBottom: 15 },
  176. noAddress: { flex: 1, color: '#fff', fontSize: 16, fontWeight: 'bold' },
  177. addressInfo: { flex: 1 },
  178. addressName: { color: '#fff', fontSize: 14, fontWeight: 'bold' },
  179. addressDetail: { color: '#aaa', fontSize: 12, marginTop: 4 },
  180. arrowIcon: { color: '#fff', fontSize: 20, marginLeft: 10 },
  181. bottomBar: { paddingHorizontal: 15, paddingTop: 10 },
  182. submitBtn: { alignItems: 'center' },
  183. submitBtnBg: { width: 260, height: 60, justifyContent: 'center', alignItems: 'center' },
  184. submitBtnText: { color: '#000', fontSize: 16, fontWeight: 'bold' },
  185. });