import React, { useEffect, useState } from 'react'; import { useParams, useNavigate, useLocation } from 'react-router-dom'; import { Button, message, Result, Spin } from 'antd'; import { WechatOutlined, AlipayCircleOutlined, CheckCircleOutlined } from '@ant-design/icons'; import { QRCodeSVG } from 'qrcode.react'; import { getOrder, initiatePayment, confirmPayment, nativePay, queryOrderStatus } from '../api'; import './Payment.css'; const Payment = () => { const { orderId: initialOrderId } = useParams(); const navigate = useNavigate(); const location = useLocation(); const [currentOrderId, setCurrentOrderId] = useState(location.state?.order_id || initialOrderId); const [order, setOrder] = useState(location.state?.orderInfo || null); const [codeUrl, setCodeUrl] = useState(location.state?.codeUrl || null); const [loading, setLoading] = useState(!location.state?.orderInfo && !location.state?.codeUrl); const [paying, setPaying] = useState(!!location.state?.codeUrl); const [paySuccess, setPaySuccess] = useState(false); const [paymentMethod, setPaymentMethod] = useState('wechat'); useEffect(() => { if (codeUrl && !paying) { setPaying(true); } }, [codeUrl]); useEffect(() => { console.log('Payment page state:', { currentOrderId, order, codeUrl, paying }); if (!order && !codeUrl) { fetchOrder(); } }, [currentOrderId]); useEffect(() => { if (paying && !codeUrl && order) { handlePay(); } }, [paying, codeUrl, order]); // 轮询订单状态 useEffect(() => { let timer; if (paying && !paySuccess) { timer = setInterval(async () => { try { const response = await queryOrderStatus(currentOrderId); if (response.data.status === 'paid') { setPaySuccess(true); setPaying(false); clearInterval(timer); } } catch (error) { console.error('Check payment status failed:', error); } }, 3000); } return () => clearInterval(timer); }, [paying, paySuccess, currentOrderId]); const fetchOrder = async () => { try { const response = await getOrder(currentOrderId); setOrder(response.data); } catch (error) { console.error('Failed to fetch order:', error); // Fallback if getOrder API fails (404/405), we might show basic info or error // Assuming for now it works or we handle it message.error('无法获取订单信息,请重试'); } finally { setLoading(false); } }; const handlePay = async () => { if (paymentMethod === 'alipay') { message.info('暂未开通支付宝支付,请使用微信支付'); return; } if (codeUrl) { setPaying(true); return; } if (!order) { message.error('正在加载订单信息,请稍后...'); return; } setPaying(true); try { const orderData = { goodid: order.config || order.goodid, quantity: order.quantity, customer_name: order.customer_name, phone_number: order.phone_number, shipping_address: order.shipping_address, ref_code: order.ref_code }; const response = await nativePay(orderData); setCodeUrl(response.data.code_url); if (response.data.order_id) { setCurrentOrderId(response.data.order_id); } message.success('支付二维码已生成'); } catch (error) { console.error(error); message.error('生成支付二维码失败,请重试'); setPaying(false); } }; if (loading) return
正在加载订单信息...
; if (paySuccess) { return (
} title={支付成功} subTitle={订单 {currentOrderId} 已完成支付,我们将尽快为您发货。} extra={[ , ]} />
); } return (
收银台
{order ? ( <>
¥{order.total_price}

订单编号: {order.id}

商品名称: {order.config_name || 'AI 硬件设备'}

收货人: {order.customer_name}

) : (

订单 ID: {currentOrderId}

无法加载详情,但您可以尝试支付。

)}
选择支付方式:
setPaymentMethod('wechat')} > 微信支付
setPaymentMethod('alipay')} > 支付宝
{paying && (
{codeUrl ? ( <>

请使用微信扫码支付

支付完成后将自动跳转

) : (
正在生成支付二维码...
)}
)} {!paying && ( )}
); }; export default Payment;