import React, { useEffect, useState } from 'react'; import { useParams, useNavigate, useSearchParams } from 'react-router-dom'; import { Button, Row, Col, Tag, Statistic, Modal, Form, Input, InputNumber, message, Spin, Descriptions, Radio, Alert } from 'antd'; import { ShoppingCartOutlined, SafetyCertificateOutlined, ThunderboltOutlined, EyeOutlined, StarOutlined } from '@ant-design/icons'; import { getConfigs, createOrder, nativePay } from '../api'; import ModelViewer from '../components/ModelViewer'; import LoginModal from '../components/LoginModal'; import { useAuth } from '../context/AuthContext'; import './ProductDetail.css'; const ProductDetail = () => { const { id } = useParams(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const [product, setProduct] = useState(null); const [loading, setLoading] = useState(true); const [isModalOpen, setIsModalOpen] = useState(false); const [loginVisible, setLoginVisible] = useState(false); const [submitting, setSubmitting] = useState(false); const [form] = Form.useForm(); const { user, login } = useAuth(); // 优先从 URL 获取,如果没有则从 localStorage 获取,不再默认绑定 flw666 const refCode = searchParams.get('ref') || localStorage.getItem('ref_code'); useEffect(() => { // 自动填充用户信息 if (user) { form.setFieldsValue({ phone_number: user.phone_number, // 如果后端返回了地址信息,这里也可以填充 // shipping_address: user.shipping_address }); } }, [isModalOpen, user]); // 当弹窗打开或用户状态变化时填充 useEffect(() => { console.log('[ProductDetail] Current ref_code:', refCode); }, [refCode]); useEffect(() => { fetchProduct(); }, [id]); const fetchProduct = async () => { try { const response = await getConfigs(); const found = response.data.find(p => String(p.id) === id); if (found) { setProduct(found); } else { message.error('未找到该产品'); navigate('/'); } } catch (error) { console.error('Failed to fetch product:', error); message.error('加载失败'); } finally { setLoading(false); } }; const handleBuy = async (values) => { setSubmitting(true); try { const isPickup = values.delivery_method === 'pickup'; const orderData = { goodid: product.id, quantity: values.quantity, customer_name: values.customer_name, phone_number: values.phone_number, // 如果是自提,手动设置地址,否则使用表单中的地址 shipping_address: isPickup ? '线下自提' : values.shipping_address, ref_code: refCode }; console.log('提交订单数据:', orderData); // 调试日志 const response = await nativePay(orderData); message.success('订单已创建,请完成支付'); navigate(`/payment/${response.data.order_id}`, { state: { codeUrl: response.data.code_url, order_id: response.data.order_id, orderInfo: { ...orderData, id: response.data.order_id, config_name: product.name, total_price: product.price * values.quantity } } }); } catch (error) { console.error(error); message.error('创建订单失败,请检查填写信息'); } finally { setSubmitting(false); } }; const getModelPaths = (p) => { if (!p) return null; // 优先使用后台配置的 3D 模型 URL if (p.model_3d_url) { return { obj: p.model_3d_url }; } return null; }; const modelPaths = getModelPaths(product); const renderIcon = (feature) => { if (feature.display_icon) { return {feature.title}; } const iconProps = { style: { fontSize: 60, color: '#00b96b', marginBottom: 20 } }; switch(feature.icon_name) { case 'SafetyCertificate': return ; case 'Eye': return ; case 'Thunderbolt': return ; default: return ; } }; if (loading) return
; if (!product) return null; return (
{/* Hero Section */}
{modelPaths ? ( ) : product.static_image_url ? ( {product.name} ) : ( )}

{product.name}

{product.description}

{product.chip_type} {product.has_camera && 高清摄像头} {product.has_microphone && 阵列麦克风}
{product.stock < 5 && product.stock > 0 && ( )} {product.stock === 0 && ( )}
{/* Feature Section */}
{product.features && product.features.length > 0 ? ( product.features.map((feature, index) => (
{renderIcon(feature)}
{feature.title}
{feature.description}
)) ) : ( // Fallback content if no features are configured <>
工业级安全标准
采用军工级加密芯片,保障您的数据隐私安全。无论是边缘计算还是云端同步,全程加密传输,让 AI 应用无后顾之忧。
超清视觉感知
搭载 4K 高清摄像头与 AI 视觉算法,实时捕捉每一个细节。支持人脸识别、物体检测、姿态分析等多种视觉任务。
极致性能释放
{product.chip_type} 强劲核心,提供高达 XX TOPS 的算力支持。低功耗设计,满足 24 小时全天候运行需求。
)} {product.display_detail_image ? (
产品详情
) : (
产品详情长图展示区域 (请在后台配置)
)}
{/* Login Modal */} setLoginVisible(false)} onLoginSuccess={(userData) => { login(userData); setLoginVisible(false); setIsModalOpen(true); }} /> {/* Order Modal */} setIsModalOpen(false)} footer={null} destroyOnHidden >
快递配送 线下自提 prevValues.delivery_method !== currentValues.delivery_method} > {({ getFieldValue }) => getFieldValue('delivery_method') === 'shipping' ? ( ) : (

自提地址:昆明市云纺国际商厦B座1406

请在工作日 9:00 - 18:00 期间前往提货

) }
); }; export default ProductDetail;