CheckoutModal.tsx 8.2 KB

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