import { ordercontrollerSyncorders } from '@/servers/api/order'; import { sitecontrollerCreate, sitecontrollerDisable, sitecontrollerList, sitecontrollerUpdate, } from '@/servers/api/site'; import { subscriptioncontrollerSync } from '@/servers/api/subscription'; import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components'; import { Button, message, notification, Popconfirm, Space, Tag } from 'antd'; import React, { useRef, useState } from 'react'; import EditSiteForm from '../Shop/EditSiteForm'; // 引入重构后的表单组件 // 区域数据项类型 interface AreaItem { code: string; name: string; } // 仓库数据项类型 interface StockPointItem { id: number; name: string; } // 站点数据项类型(前端不包含密钥字段,后端列表不返回密钥) export interface SiteItem { id: number; name: string; description?: string; apiUrl?: string; websiteUrl?: string; // 网站地址 type?: 'woocommerce' | 'shopyy'; skuPrefix?: string; isDisabled: number; areas?: AreaItem[]; stockPoints?: StockPointItem[]; } const SiteList: React.FC = () => { const actionRef = useRef(); const [open, setOpen] = useState(false); const [editing, setEditing] = useState(null); const [selectedRowKeys, setSelectedRowKeys] = useState([]); const handleSync = async (ids: number[]) => { if (!ids.length) return; const hide = message.loading('正在同步...', 0); const stats = { orders: { success: 0, fail: 0 }, subscriptions: { success: 0, fail: 0 }, }; try { for (const id of ids) { // 同步订单 const orderRes = await ordercontrollerSyncorders({ siteId: id }); if (orderRes.success) { stats.orders.success += 1; } else { stats.orders.fail += 1; } // 同步订阅 const subRes = await subscriptioncontrollerSync({ siteId: id }); if (subRes.success) { stats.subscriptions.success += 1; } else { stats.subscriptions.fail += 1; } } hide(); notification.success({ message: '同步完成', description: (

订单: 成功 {stats.orders.success}, 失败 {stats.orders.fail}

订阅: 成功 {stats.subscriptions.success}, 失败{' '} {stats.subscriptions.fail}

), duration: null, // 不自动关闭 }); setSelectedRowKeys([]); actionRef.current?.reload(); } catch (error: any) { hide(); message.error(error.message || '同步失败'); } }; // 表格列定义 const columns: ProColumns[] = [ { title: 'ID', dataIndex: 'id', width: 80, sorter: true, hideInSearch: true, }, { title: '名称', dataIndex: 'name', width: 220 }, { title: '描述', dataIndex: 'description', width: 220, hideInSearch: true }, { title: 'API 地址', dataIndex: 'apiUrl', width: 280, hideInSearch: true }, { title: '网站地址', dataIndex: 'websiteUrl', width: 280, hideInSearch: true, render: (text) => ( {text} ), }, { title: 'SKU 前缀', dataIndex: 'skuPrefix', width: 160, hideInSearch: true, }, { title: '平台', dataIndex: 'type', width: 140, valueType: 'select', request: async () => [ { label: 'WooCommerce', value: 'woocommerce' }, { label: 'Shopyy', value: 'shopyy' }, ], }, { title: '关联仓库', dataIndex: 'stockPoints', width: 200, hideInSearch: true, render: (_, row) => { if (!row.stockPoints || row.stockPoints.length === 0) { return ; } return ( {row.stockPoints.map((sp) => ( {sp.name} ))} ); }, }, { title: '状态', dataIndex: 'isDisabled', width: 120, hideInSearch: true, render: (_, row) => ( {row.isDisabled ? '已禁用' : '启用中'} ), }, { title: '操作', dataIndex: 'actions', width: 240, fixed: 'right', hideInSearch: true, render: (_, row) => ( { try { await sitecontrollerDisable( { id: String(row.id) }, { disabled: !row.isDisabled }, ); message.success('更新成功'); actionRef.current?.reload(); } catch (e: any) { message.error(e?.message || '更新失败'); } }} > ), }, ]; // 表格数据请求 const tableRequest = async (params: Record) => { try { const { current, pageSize, name, type } = params; const resp = await sitecontrollerList({ current, pageSize, keyword: name || undefined, type: type || undefined, }); // 假设 resp 直接就是后端返回的结构,包含 items 和 total return { data: (resp?.data?.items ?? []) as SiteItem[], total: resp?.data?.total ?? 0, success: true, }; } catch (e: any) { message.error(e?.message || '获取失败'); return { data: [], total: 0, success: false }; } }; const handleFinish = async (values: any) => { try { if (editing) { await sitecontrollerUpdate({ id: String(editing.id) }, values); message.success('更新成功'); } else { await sitecontrollerCreate(values); message.success('创建成功'); } setOpen(false); actionRef.current?.reload(); return true; } catch (error: any) { message.error(error.message || '操作失败'); return false; } }; return ( <> scroll={{ x: 'max-content' }} actionRef={actionRef} rowKey="id" columns={columns} request={tableRequest} rowSelection={{ selectedRowKeys, onChange: setSelectedRowKeys, }} toolBarRender={() => [ , , ]} /> { setOpen(visible); if (!visible) { setEditing(null); } }} initialValues={editing} isEdit={!!editing} onFinish={handleFinish} /> ); }; export default SiteList;