import React, { useState, useEffect } from 'react'; import { adminAPI } from '@/api'; import type { PaymentConfig, Order } from '@/types'; const SVGIcon: React.FC<{ children: React.ReactNode; size?: number }> = ({ children, size = 16 }) => ( {children} ); const statusBadge = (status: string) => { const map: Record = { active: { cls: 'badge-success', text: '已启用' }, inactive: { cls: 'badge-neutral', text: '未启用' }, paid: { cls: 'badge-success', text: '已支付' }, pending: { cls: 'badge-warning', text: '待支付' }, refunded: { cls: 'badge-error', text: '已退款' }, }; const s = map[status] || { cls: 'badge-neutral', text: status }; return {s.text}; }; const paymentMethodBadge = (method: string) => { return method === 'wechat' ? 微信支付 : 支付宝; }; const Payment: React.FC = () => { const [configs, setConfigs] = useState([]); const [orders, setOrders] = useState([]); const [saving, setSaving] = useState>({}); const [msg, setMsg] = useState>({}); const wechat = configs.find(c => c.type === 'wechat'); const alipay = configs.find(c => c.type === 'alipay'); const [wechatForm, setWechatForm] = useState({ app_id: '', merchant_id: '', api_key: '', api_v3_key: '', cert_path: '', cert_serial_no: '', notify_url: '', }); const [alipayForm, setAlipayForm] = useState({ app_id: '', app_private_key: '', alipay_public_key: '', gateway_url: 'https://openapi.alipay.com/gateway.do', notify_url: '', return_url: '', }); useEffect(() => { loadConfigs(); loadOrders(); }, []); useEffect(() => { if (wechat) { setWechatForm({ app_id: wechat.app_id || '', merchant_id: wechat.merchant_id || '', api_key: wechat.api_key || '', api_v3_key: wechat.api_v3_key || '', cert_path: wechat.cert_path || '/etc/certs/apiclient_cert.pem', cert_serial_no: wechat.cert_serial_no || '', notify_url: wechat.notify_url || '', }); } if (alipay) { setAlipayForm({ app_id: alipay.app_id || '', app_private_key: alipay.app_private_key || '', alipay_public_key: alipay.alipay_public_key || '', gateway_url: alipay.gateway_url || 'https://openapi.alipay.com/gateway.do', notify_url: alipay.notify_url || '', return_url: alipay.return_url || '', }); } }, [configs]); const loadConfigs = async () => { try { const res = await adminAPI.getPaymentConfigs(); const list = res?.data || res; if (Array.isArray(list)) { setConfigs(list); } } catch (e) { console.error('加载支付配置失败', e); } }; const loadOrders = async () => { try { const res = await adminAPI.getAdminOrders({ page: 1, page_size: 20 }); const data: any = res?.data || res; if (data?.items) { setOrders(data.items); } else if (Array.isArray(data)) { setOrders(data); } } catch (e) { console.error('加载订单失败', e); } }; const handleSaveConfig = async (id: string, data: any) => { setSaving(prev => ({ ...prev, [id]: true })); setMsg(prev => ({ ...prev, [id]: { type: '', text: '' } })); try { await adminAPI.updatePaymentConfig(id, data); setMsg(prev => ({ ...prev, [id]: { type: 'success', text: '保存成功' } })); loadConfigs(); } catch (e: any) { setMsg(prev => ({ ...prev, [id]: { type: 'error', text: e.message || '保存失败' } })); } finally { setSaving(prev => ({ ...prev, [id]: false })); } }; const handleToggleConfig = async (type: string) => { const config = type === 'wechat' ? wechat : alipay; if (!config) return; const newStatus = config.status === 'active' ? 'inactive' : 'active'; await handleSaveConfig(config.id, { status: newStatus }); }; const handleRefund = async (orderId: string) => { try { await adminAPI.refundOrder(orderId); loadOrders(); } catch (e) { console.error('退款失败', e); } }; const formatDate = (d: string) => { const dt = new Date(d); return dt.toLocaleDateString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }); }; return ( 支付渠道 {Object.entries(msg).map(([id, m]) => m.text ? ( {m.text} ) : null)} {/* WeChat Pay Card */} 微信支付 {wechat ? statusBadge(wechat.status) : 未配置} {wechat && ( handleToggleConfig('wechat')} style={{ width: 44, height: 24, borderRadius: 12, border: 'none', background: wechat.status === 'active' ? 'var(--color-accent)' : 'var(--color-text-disabled)', cursor: 'pointer', position: 'relative', transition: 'background var(--transition-fast)', }} > )} AppID setWechatForm({ ...wechatForm, app_id: e.target.value })} /> 商户号 setWechatForm({ ...wechatForm, merchant_id: e.target.value })} /> API v2 密钥 setWechatForm({ ...wechatForm, api_key: e.target.value })} /> API v3 密钥 setWechatForm({ ...wechatForm, api_v3_key: e.target.value })} /> 证书路径 setWechatForm({ ...wechatForm, cert_path: e.target.value })} /> 回调通知地址 setWechatForm({ ...wechatForm, notify_url: e.target.value })} /> wechat && handleSaveConfig(wechat.id, { app_id: wechatForm.app_id, merchant_id: wechatForm.merchant_id, api_key: wechatForm.api_key, api_v3_key: wechatForm.api_v3_key, cert_path: wechatForm.cert_path, notify_url: wechatForm.notify_url, })}> {saving[wechat?.id || ''] ? '保存中...' : '保存配置'} {/* Alipay Card */} 支付宝 {alipay ? statusBadge(alipay.status) : 未配置} {alipay && ( handleToggleConfig('alipay')} style={{ width: 44, height: 24, borderRadius: 12, border: 'none', background: alipay.status === 'active' ? 'var(--color-accent)' : 'var(--color-text-disabled)', cursor: 'pointer', position: 'relative', transition: 'background var(--transition-fast)', }} > )} AppID setAlipayForm({ ...alipayForm, app_id: e.target.value })} /> 接口地址 setAlipayForm({ ...alipayForm, gateway_url: e.target.value })} /> 应用私钥 setAlipayForm({ ...alipayForm, app_private_key: e.target.value })} /> 支付宝公钥 setAlipayForm({ ...alipayForm, alipay_public_key: e.target.value })} /> 异步回调地址 setAlipayForm({ ...alipayForm, notify_url: e.target.value })} /> 同步跳转地址 setAlipayForm({ ...alipayForm, return_url: e.target.value })} /> alipay && handleSaveConfig(alipay.id, { app_id: alipayForm.app_id, app_private_key: alipayForm.app_private_key, alipay_public_key: alipayForm.alipay_public_key, gateway_url: alipayForm.gateway_url, notify_url: alipayForm.notify_url, return_url: alipayForm.return_url, })}> {saving[alipay?.id || ''] ? '保存中...' : '保存配置'} {/* Order Management */} 订单管理 导出 订单号 用户 类型 项目 金额 支付方式 状态 时间 操作 {orders.map(order => ( {order.id} {order.userId} {order.orderType === 'membership' ? '会员' : order.orderType === 'app' ? '应用' : '服务'} {order.itemName} {order.amount.toFixed(2)} 元 {paymentMethodBadge(order.paymentMethod)} {statusBadge(order.status)} {formatDate(order.createdAt)} {order.status === 'paid' && ( handleRefund(order.id)}> 退款 )} {order.status === 'refunded' && ( 已退款 )} {order.status === 'pending' && ( 等待支付 )} ))} ); }; export default Payment;