import { PRODUCT_STATUS_ENUM } from '@/constants'; import { productcontrollerProductbysku, productcontrollerSearchproducts, } from '@/servers/api/product'; import { sitecontrollerAll } from '@/servers/api/site'; import { wpproductcontrollerGetwpproducts, wpproductcontrollerSetconstitution, wpproductcontrollerSyncproducts, wpproductcontrollerUpdateproduct, wpproductcontrollerUpdatevariation, } from '@/servers/api/wpProduct'; import { EditOutlined } from '@ant-design/icons'; import { ActionType, DrawerForm, PageContainer, ProColumns, ProForm, ProFormDigit, ProFormList, ProFormSelect, ProFormText, ProTable, } from '@ant-design/pro-components'; import { App, Button, Divider, Form } from 'antd'; import { useRef } from 'react'; const List: React.FC = () => { const actionRef = useRef(); const columns: ProColumns[] = [ { title: '名称', dataIndex: 'name', }, { title: '站点', dataIndex: 'siteId', valueType: 'select', request: async () => { const { data = [] } = await sitecontrollerAll(); return data.map((item) => ({ label: item.siteName, value: item.id, })); }, }, { title: 'sku', dataIndex: 'sku', hideInSearch: true, }, { title: '产品状态', dataIndex: 'status', valueType: 'select', valueEnum: PRODUCT_STATUS_ENUM, }, { title: '常规价格', dataIndex: 'regular_price', hideInSearch: true, }, { title: '销售价格', dataIndex: 'sale_price', hideInSearch: true, }, { title: '更新时间', dataIndex: 'updatedAt', valueType: 'dateTime', hideInSearch: true, }, { title: '创建时间', dataIndex: 'createdAt', valueType: 'dateTime', hideInSearch: true, }, { title: '操作', dataIndex: 'option', valueType: 'option', render: (_, record) => ( <> {record.type === 'simple' && record.sku ? ( <> ) : ( <> )} ), }, ]; const varColumns: ProColumns[] = [ { title: '变体名', dataIndex: 'name', }, { title: 'sku', dataIndex: 'sku', hideInSearch: true, }, { title: '常规价格', dataIndex: 'regular_price', hideInSearch: true, }, { title: '销售价格', dataIndex: 'sale_price', hideInSearch: true, }, { title: '操作', dataIndex: 'option', valueType: 'option', render: (_, record) => ( <> {record.sku ? ( <> ) : ( <> )} ), }, ]; return ( headerTitle="查询表格" actionRef={actionRef} rowKey="id" request={async (params) => { const { data, success } = await wpproductcontrollerGetwpproducts( params, ); return { total: data?.total || 0, data: data?.items || [], success, }; }} columns={columns} toolBarRender={() => []} expandable={{ rowExpandable: (record) => record.type === 'variable', expandedRowRender: (record) => ( rowKey="id" dataSource={record.variations} pagination={false} search={false} options={false} columns={varColumns} /> ), }} /> ); }; const SyncForm: React.FC<{ tableRef: React.MutableRefObject; }> = ({ tableRef }) => { const { message } = App.useApp(); return ( title="同步产品" trigger={ } autoFocusFirstInput drawerProps={{ destroyOnClose: true, }} onFinish={async (values) => { try { const { success, message: errMsg } = await wpproductcontrollerSyncproducts(values); if (!success) { throw new Error(errMsg); } message.success('同步成功'); tableRef.current?.reload(); return true; } catch (error: any) { message.error(error.message); } }} > { const { data = [] } = await sitecontrollerAll(); return data.map((item) => ({ label: item.siteName, value: item.id, })); }} /> ); }; const UpdateForm: React.FC<{ tableRef: React.MutableRefObject; values: API.WpProductDTO; }> = ({ tableRef, values: initialValues }) => { const { message } = App.useApp(); return ( title="编辑产品" initialValues={initialValues} trigger={ } autoFocusFirstInput drawerProps={{ destroyOnClose: true, }} onFinish={async (values) => { const { siteId, ...params } = values; try { const { success, message: errMsg } = await wpproductcontrollerUpdateproduct( { productId: initialValues.externalProductId, siteId, }, params, ); if (!success) { throw new Error(errMsg); } message.success('提交成功'); tableRef.current?.reload(); return true; } catch (error: any) { message.error(error.message); } }} > { const { data = [] } = await sitecontrollerAll(); return data.map((item) => ({ label: item.siteName, value: item.id, })); }} name="siteId" disabled /> {initialValues.type === 'simple' ? ( <> ) : ( <> )} ); }; const UpdateVaritation: React.FC<{ tableRef: React.MutableRefObject; values: API.VariationDTO; }> = ({ tableRef, values: initialValues }) => { const { message } = App.useApp(); return ( title="编辑变体" initialValues={initialValues} trigger={ } autoFocusFirstInput drawerProps={{ destroyOnClose: true, }} onFinish={async (values) => { const { ...params } = values; try { const { success, message: errMsg } = await wpproductcontrollerUpdatevariation( { siteId: initialValues.siteId, productId: initialValues.externalProductId, variationId: initialValues.externalVariationId, }, params, ); if (!success) { throw new Error(errMsg); } message.success('提交成功'); tableRef.current?.reload(); return true; } catch (error: any) { message.error(error.message); } }} > ); }; const SetComponent: React.FC<{ tableRef: React.MutableRefObject; values: API.VariationDTO | API.WpProductDTO; isProduct: boolean; }> = ({ tableRef, values: { id, constitution, name }, isProduct = false }) => { const { message } = App.useApp(); const [form] = Form.useForm(); const fetchInitialValues = async () => { const initData = await Promise.all( constitution?.map?.(async (item) => { const { data } = await productcontrollerProductbysku({ sku: item.sku as string, }); return { quantity: item.quantity, sku: { label: data?.name, value: item.sku, }, }; }) || [], ); form.setFieldsValue({ constitution: initData, }); }; return ( title={name} form={form} trigger={ } autoFocusFirstInput drawerProps={{ destroyOnClose: true, }} onFinish={async ({ constitution }) => { try { const { success, message: errMsg } = await wpproductcontrollerSetconstitution( { id, }, { isProduct, constitution, }, ); if (!success) { throw new Error(errMsg); } message.success('提交成功'); tableRef.current?.reload(); return true; } catch (error: any) { message.error(error.message); } }} onOpenChange={(visiable) => { if (visiable) fetchInitialValues(); }} > name="constitution" rules={[ { required: true, message: '至少需要一个商品', validator: (_, value) => value && value.length > 0 ? Promise.resolve() : Promise.reject('至少需要一个商品'), }, ]} creatorButtonProps={{ children: '新增' }} > {(fields, idx, { remove }) => (
{ if (keyWords.length < 3) return []; try { const { data } = await productcontrollerSearchproducts({ name: keyWords, }); const arr = data?.map((item) => { return { label: item.name, value: item.sku, }; }) || []; return arr; } catch (error) { console.log(error); return []; } }} name="sku" label="产品" width="lg" placeholder="请选择产品" tooltip="至少输入3个字符" fieldProps={{ showSearch: true, filterOption: false, }} transform={(value) => { return value?.value || value; }} debounceTime={300} // 防抖,减少请求频率 rules={[{ required: true, message: '请选择产品' }]} />
)}
); }; export default List;