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 && ( )}
setWechatForm({ ...wechatForm, app_id: e.target.value })} />
setWechatForm({ ...wechatForm, merchant_id: e.target.value })} />
setWechatForm({ ...wechatForm, api_key: e.target.value })} />
setWechatForm({ ...wechatForm, api_v3_key: e.target.value })} />
setWechatForm({ ...wechatForm, cert_path: e.target.value })} />
setWechatForm({ ...wechatForm, notify_url: e.target.value })} />
{/* Alipay Card */}

支付宝

{alipay ? statusBadge(alipay.status) : 未配置} {alipay && ( )}
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 })} />
{/* 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' && ( )} {order.status === 'refunded' && ( 已退款 )} {order.status === 'pending' && ( 等待支付 )}
); }; export default Payment;