修改支付配置页面逻辑错误
This commit is contained in:
@@ -207,6 +207,10 @@ export async function updatePaymentConfig(id: string, value: string): Promise<vo
|
||||
await api.put(`/admin/payment-configs/${id}`, { value });
|
||||
}
|
||||
|
||||
export async function batchUpdatePaymentConfigs(configs: Record<string, string>): Promise<void> {
|
||||
await api.put('/admin/payment-configs/batch', configs);
|
||||
}
|
||||
|
||||
export async function getAdminNotifications(): Promise<{ total: number; items: any[] }> {
|
||||
return api.get('/admin/notifications');
|
||||
}
|
||||
|
||||
@@ -5,18 +5,10 @@ import {
|
||||
import {
|
||||
SaveOutlined, WechatOutlined, AlipayCircleOutlined, DollarOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { getPaymentConfigs, updatePaymentConfig } from '../api';
|
||||
|
||||
interface PaymentConfig {
|
||||
id: string;
|
||||
key: string;
|
||||
value: string;
|
||||
description?: string;
|
||||
}
|
||||
import { getPaymentConfigs, batchUpdatePaymentConfigs } from '../api';
|
||||
|
||||
const AdminPaymentConfig: React.FC = () => {
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [configs, setConfigs] = useState<PaymentConfig[]>([]);
|
||||
const [wechatEnabled, setWechatEnabled] = useState(false);
|
||||
const [alipayEnabled, setAlipayEnabled] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
@@ -24,9 +16,8 @@ const AdminPaymentConfig: React.FC = () => {
|
||||
const load = async () => {
|
||||
try {
|
||||
const data = await getPaymentConfigs();
|
||||
setConfigs(data);
|
||||
const map: Record<string, string> = {};
|
||||
data.forEach((c: PaymentConfig) => { map[c.key] = c.value; });
|
||||
data.forEach((c: any) => { map[c.key] = c.value; });
|
||||
form.setFieldsValue({
|
||||
wechat_mch_id: map['payment_wechat_mch_id'] || '',
|
||||
wechat_api_key: map['payment_wechat_api_key'] || '',
|
||||
@@ -51,25 +42,19 @@ const AdminPaymentConfig: React.FC = () => {
|
||||
try {
|
||||
const values = await form.validateFields();
|
||||
setSaving(true);
|
||||
const updates: [string, string][] = [
|
||||
['payment_wechat_enabled', String(wechatEnabled)],
|
||||
['payment_wechat_mch_id', values.wechat_mch_id || ''],
|
||||
['payment_wechat_api_key', values.wechat_api_key || ''],
|
||||
['payment_wechat_cert_path', values.wechat_cert_path || ''],
|
||||
['payment_wechat_notify_url', values.wechat_notify_url || ''],
|
||||
['payment_alipay_enabled', String(alipayEnabled)],
|
||||
['payment_alipay_app_id', values.alipay_app_id || ''],
|
||||
['payment_alipay_private_key', values.alipay_private_key || ''],
|
||||
['payment_alipay_public_key', values.alipay_public_key || ''],
|
||||
['payment_alipay_notify_url', values.alipay_notify_url || ''],
|
||||
['payment_alipay_gateway', values.alipay_gateway || ''],
|
||||
];
|
||||
for (const [key, value] of updates) {
|
||||
const cfg = configs.find(c => c.key === key);
|
||||
if (cfg) {
|
||||
await updatePaymentConfig(cfg.id, value);
|
||||
}
|
||||
}
|
||||
await batchUpdatePaymentConfigs({
|
||||
payment_wechat_enabled: String(wechatEnabled),
|
||||
payment_wechat_mch_id: values.wechat_mch_id || '',
|
||||
payment_wechat_api_key: values.wechat_api_key || '',
|
||||
payment_wechat_cert_path: values.wechat_cert_path || '',
|
||||
payment_wechat_notify_url: values.wechat_notify_url || '',
|
||||
payment_alipay_enabled: String(alipayEnabled),
|
||||
payment_alipay_app_id: values.alipay_app_id || '',
|
||||
payment_alipay_private_key: values.alipay_private_key || '',
|
||||
payment_alipay_public_key: values.alipay_public_key || '',
|
||||
payment_alipay_notify_url: values.alipay_notify_url || '',
|
||||
payment_alipay_gateway: values.alipay_gateway || '',
|
||||
});
|
||||
message.success('支付配置已保存');
|
||||
load();
|
||||
} catch {
|
||||
|
||||
@@ -412,6 +412,34 @@ async def list_payment_configs(
|
||||
]
|
||||
|
||||
|
||||
@router.put("/payment-configs/batch")
|
||||
async def batch_update_payment_configs(
|
||||
req: dict[str, str],
|
||||
admin: User = Depends(get_admin_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""Batch upsert payment configs. Creates missing keys, updates existing ones."""
|
||||
from app.utils.id_gen import generate_id
|
||||
|
||||
for key, value in req.items():
|
||||
if not key.startswith("payment_"):
|
||||
continue
|
||||
result = await db.execute(
|
||||
select(SystemConfig).where(SystemConfig.key == key).limit(1)
|
||||
)
|
||||
config = result.scalar_one_or_none()
|
||||
if config:
|
||||
config.value = value
|
||||
else:
|
||||
db.add(SystemConfig(
|
||||
id=generate_id(),
|
||||
key=key,
|
||||
value=value,
|
||||
))
|
||||
await db.flush()
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.put("/payment-configs/{config_id}")
|
||||
async def update_payment_config(
|
||||
config_id: str,
|
||||
|
||||
Reference in New Issue
Block a user