TransferModal.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import { Image } from 'expo-image';
  2. import React, { useEffect, useState } from 'react';
  3. import {
  4. Alert,
  5. Modal,
  6. Platform,
  7. ScrollView,
  8. StyleSheet,
  9. Text,
  10. TextInput,
  11. TouchableOpacity,
  12. View,
  13. } from 'react-native';
  14. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  15. import { Images } from '@/constants/images';
  16. import { transferOrderSubmit } from '@/services/user';
  17. interface GroupedGoods {
  18. total: number;
  19. data: {
  20. id: string;
  21. cover: string;
  22. spuId: string;
  23. level: string;
  24. };
  25. }
  26. interface TransferModalProps {
  27. visible: boolean;
  28. selectedItems: Array<{
  29. id: string;
  30. spu: { id: string; name: string; cover: string };
  31. level: string;
  32. }>;
  33. onClose: () => void;
  34. onSuccess: () => void;
  35. }
  36. export default function TransferModal({
  37. visible,
  38. selectedItems,
  39. onClose,
  40. onSuccess,
  41. }: TransferModalProps) {
  42. const insets = useSafeAreaInsets();
  43. const [recipientId, setRecipientId] = useState('');
  44. const [checked, setChecked] = useState(true);
  45. const [submitting, setSubmitting] = useState(false);
  46. const [goodsList, setGoodsList] = useState<GroupedGoods[]>([]);
  47. const showAlert = (msg: string) => {
  48. if (Platform.OS === 'web') window.alert(msg);
  49. else Alert.alert('提示', msg);
  50. };
  51. useEffect(() => {
  52. if (visible && selectedItems.length > 0) {
  53. // 合并同款商品
  54. const goodsMap: Record<string, GroupedGoods> = {};
  55. selectedItems.forEach((item) => {
  56. const key = `${item.spu.id}_${item.level}`;
  57. if (goodsMap[key]) {
  58. goodsMap[key].total += 1;
  59. } else {
  60. goodsMap[key] = {
  61. total: 1,
  62. data: {
  63. id: item.id,
  64. cover: item.spu.cover,
  65. spuId: item.spu.id,
  66. level: item.level,
  67. },
  68. };
  69. }
  70. });
  71. setGoodsList(Object.values(goodsMap));
  72. setRecipientId('');
  73. }
  74. }, [visible, selectedItems]);
  75. const handleSubmit = async () => {
  76. if (!recipientId.trim()) {
  77. showAlert('请输入受赠人ID');
  78. return;
  79. }
  80. if (!checked) {
  81. showAlert('请阅读并同意《转赠风险及责任声明》');
  82. return;
  83. }
  84. if (submitting) return;
  85. setSubmitting(true);
  86. try {
  87. const inventoryIds = selectedItems.map((item) => item.id);
  88. const levels = selectedItems.map((item) => item.level);
  89. const res: any = await transferOrderSubmit({
  90. inventoryIds,
  91. levels,
  92. toUserShortId: recipientId.trim(),
  93. });
  94. if (res && res.success) {
  95. showAlert('转赠成功');
  96. onClose();
  97. onSuccess();
  98. } else {
  99. showAlert(res?.msg || res?.message || '转赠失败');
  100. }
  101. } catch (err) {
  102. console.error('转赠失败:', err);
  103. showAlert('转赠失败,请稍后再试');
  104. }
  105. setSubmitting(false);
  106. };
  107. return (
  108. <Modal
  109. visible={visible}
  110. transparent
  111. animationType="slide"
  112. onRequestClose={onClose}
  113. >
  114. <View style={styles.overlay}>
  115. <TouchableOpacity
  116. style={styles.overlayBg}
  117. onPress={onClose}
  118. activeOpacity={1}
  119. />
  120. <View style={[styles.container, { paddingBottom: insets.bottom + 20 }]}>
  121. {/* 标题 */}
  122. <View style={styles.header}>
  123. <Text style={styles.title}>转赠</Text>
  124. <TouchableOpacity style={styles.closeBtn} onPress={onClose}>
  125. <Text style={styles.closeBtnText}>×</Text>
  126. </TouchableOpacity>
  127. </View>
  128. {/* 商品列表 */}
  129. <View style={styles.goodsSection}>
  130. <ScrollView horizontal showsHorizontalScrollIndicator={false}>
  131. {goodsList.map((goods, idx) => (
  132. <View key={idx} style={styles.goodsItem}>
  133. <Image
  134. source={{ uri: goods.data.cover }}
  135. style={styles.goodsImg}
  136. contentFit="contain"
  137. />
  138. <View style={styles.goodsCount}>
  139. <Text style={styles.goodsCountText}>x{goods.total}</Text>
  140. </View>
  141. </View>
  142. ))}
  143. </ScrollView>
  144. </View>
  145. {/* 受赠人ID输入 */}
  146. <View style={styles.inputSection}>
  147. <Text style={styles.inputLabel}>受赠人ID</Text>
  148. <TextInput
  149. style={styles.input}
  150. value={recipientId}
  151. onChangeText={setRecipientId}
  152. placeholder="请输入受赠人ID"
  153. placeholderTextColor="#999"
  154. keyboardType="default"
  155. />
  156. </View>
  157. {/* 转赠声明 */}
  158. <View style={styles.agreementRow}>
  159. <TouchableOpacity style={styles.agreementText} onPress={() => {}}>
  160. <Text style={styles.agreementLabel}>已阅读并同意</Text>
  161. <Text style={styles.agreementLink}>《转赠风险及责任声明》</Text>
  162. </TouchableOpacity>
  163. <TouchableOpacity
  164. style={[styles.radio, checked && styles.radioChecked]}
  165. onPress={() => setChecked(!checked)}
  166. >
  167. {checked && <Text style={styles.radioIcon}>✓</Text>}
  168. </TouchableOpacity>
  169. </View>
  170. {/* 提交按钮 */}
  171. <TouchableOpacity
  172. style={styles.submitBtn}
  173. onPress={handleSubmit}
  174. disabled={submitting}
  175. activeOpacity={0.7}
  176. >
  177. <Text style={styles.submitBtnText}>
  178. {submitting ? '提交中...' : '立即转赠'}
  179. </Text>
  180. </TouchableOpacity>
  181. <View style={styles.fill} />
  182. </View>
  183. </View>
  184. </Modal>
  185. );
  186. }
  187. const styles = StyleSheet.create({
  188. overlay: { flex: 1, justifyContent: 'flex-end' },
  189. overlayBg: {
  190. position: 'absolute',
  191. top: 0,
  192. left: 0,
  193. right: 0,
  194. bottom: 0,
  195. backgroundColor: 'rgba(0,0,0,0.5)',
  196. },
  197. container: {
  198. backgroundColor: '#fff',
  199. borderTopLeftRadius: 15,
  200. borderTopRightRadius: 15,
  201. paddingHorizontal: 14,
  202. },
  203. header: {
  204. flexDirection: 'row',
  205. alignItems: 'center',
  206. justifyContent: 'center',
  207. paddingTop: 22,
  208. paddingBottom: 5,
  209. position: 'relative',
  210. },
  211. title: { fontSize: 16, fontWeight: 'bold', color: '#000' },
  212. closeBtn: {
  213. position: 'absolute',
  214. right: 0,
  215. top: 15,
  216. width: 24,
  217. height: 24,
  218. backgroundColor: '#ebebeb',
  219. borderRadius: 12,
  220. justifyContent: 'center',
  221. alignItems: 'center',
  222. },
  223. closeBtnText: { fontSize: 18, color: '#a2a2a2', lineHeight: 20 },
  224. goodsSection: { paddingVertical: 10 },
  225. goodsItem: {
  226. width: 79,
  227. height: 103,
  228. backgroundColor: '#fff',
  229. borderRadius: 6,
  230. marginRight: 8,
  231. alignItems: 'center',
  232. justifyContent: 'center',
  233. position: 'relative',
  234. borderWidth: 1,
  235. borderColor: '#eaeaea',
  236. },
  237. goodsImg: { width: 73, height: 85 },
  238. goodsCount: {
  239. position: 'absolute',
  240. top: 0,
  241. right: 0,
  242. backgroundColor: '#ff6b00',
  243. borderRadius: 2,
  244. paddingHorizontal: 4,
  245. paddingVertical: 2,
  246. },
  247. goodsCountText: { color: '#fff', fontSize: 10, fontWeight: 'bold' },
  248. inputSection: {
  249. flexDirection: 'row',
  250. alignItems: 'center',
  251. justifyContent: 'space-between',
  252. backgroundColor: '#f8f8f8',
  253. borderRadius: 6,
  254. paddingHorizontal: 10,
  255. paddingVertical: 10,
  256. marginVertical: 15,
  257. shadowColor: '#000',
  258. shadowOffset: { width: 0, height: 1 },
  259. shadowOpacity: 0.05,
  260. shadowRadius: 3,
  261. },
  262. inputLabel: {
  263. fontSize: 14,
  264. fontWeight: '500',
  265. color: '#333',
  266. },
  267. input: {
  268. flex: 1,
  269. marginLeft: 10,
  270. textAlign: 'right',
  271. height: 30,
  272. fontSize: 14,
  273. color: '#666',
  274. },
  275. agreementRow: {
  276. flexDirection: 'row',
  277. alignItems: 'center',
  278. justifyContent: 'space-between',
  279. paddingVertical: 10,
  280. },
  281. agreementText: {
  282. flexDirection: 'row',
  283. alignItems: 'center',
  284. },
  285. agreementLabel: {
  286. fontSize: 12,
  287. color: '#333',
  288. },
  289. agreementLink: {
  290. fontSize: 12,
  291. color: '#ff9600',
  292. },
  293. radio: {
  294. width: 20,
  295. height: 20,
  296. borderRadius: 10,
  297. borderWidth: 2,
  298. borderColor: '#ccc',
  299. justifyContent: 'center',
  300. alignItems: 'center',
  301. },
  302. radioChecked: {
  303. backgroundColor: '#ff9600',
  304. borderColor: '#ff9600',
  305. },
  306. radioIcon: {
  307. color: '#fff',
  308. fontSize: 12,
  309. fontWeight: 'bold',
  310. },
  311. submitBtn: {
  312. backgroundColor: '#ff9600',
  313. borderRadius: 22,
  314. height: 44,
  315. justifyContent: 'center',
  316. alignItems: 'center',
  317. marginTop: 5,
  318. },
  319. submitBtnText: {
  320. color: '#fff',
  321. fontSize: 16,
  322. fontWeight: 'bold',
  323. },
  324. fill: { height: 25 },
  325. });