import { productcontrollerCreateproduct, productcontrollerDeleteproduct, productcontrollerGetcategorieall, productcontrollerGetflavorsall, productcontrollerGetproductlist, productcontrollerGetstrengthall, productcontrollerUpdateproductnamecn, } from '@/servers/api/product'; import { PlusOutlined } from '@ant-design/icons'; import { ActionType, DrawerForm, PageContainer, ProColumns, ProForm, ProFormSelect, ProFormText, ProFormTextArea, ProTable, } from '@ant-design/pro-components'; import { App, Button, Popconfirm } from 'antd'; import React, { useRef } from 'react'; const NameCn: React.FC<{ id: number; value: string; tableRef: React.MutableRefObject; }> = ({value,tableRef, id}) => { const { message } = App.useApp(); const [editable, setEditable] = React.useState(false); if (!editable) return
setEditable(true)}>{value||'-'}
; return { if(!e.target.value) return setEditable(false) const { success, message: errMsg } = await productcontrollerUpdateproductnamecn({ id, nameCn: e.target.value, }) setEditable(false) if (!success) { return message.error(errMsg) } tableRef?.current?.reload() }} /> } const List: React.FC = () => { const actionRef = useRef(); const { message } = App.useApp(); const columns: ProColumns[] = [ { title: '名称', dataIndex: 'name', }, { title: '中文名', dataIndex: 'nameCn', render: (_, record) => { return ( ) }, }, { title: '产品描述', dataIndex: 'description', hideInSearch: true, }, { title: '产品分类', dataIndex: 'categoryName', }, { title: '强度', dataIndex: 'strengthName', }, { title: '口味', dataIndex: 'flavorsName', }, { title: '湿度', dataIndex: 'humidity', }, { title: 'sku', dataIndex: 'sku', hideInSearch: true, }, { title: '更新时间', dataIndex: 'updatedAt', valueType: 'dateTime', hideInSearch: true, }, { title: '创建时间', dataIndex: 'createdAt', valueType: 'dateTime', hideInSearch: true, }, { title: '操作', dataIndex: 'option', valueType: 'option', render: (_, record) => ( <> { try { const { success, message: errMsg } = await productcontrollerDeleteproduct({ id: record.id }); if (!success) { throw new Error(errMsg); } actionRef.current?.reload(); } catch (error: any) { message.error(error.message); } }} > ), }, ]; return ( headerTitle="查询表格" actionRef={actionRef} rowKey="id" toolBarRender={() => []} request={async (params) => { const { data, success } = await productcontrollerGetproductlist( params, ); return { total: data?.total || 0, data: data?.items || [], success, }; }} columns={columns} editable={{ type: 'single', onSave: async (key, record, originRow) => { console.log('保存数据:', record); }, }} rowSelection={{ onChange: (_, selectedRows) => setSelectedRows(selectedRows), }} /> ); }; const CreateForm: React.FC<{ tableRef: React.MutableRefObject; }> = ({ tableRef }) => { const { message } = App.useApp(); return ( title="新建" trigger={ } autoFocusFirstInput drawerProps={{ destroyOnHidden: true, }} onFinish={async (values) => { try { const { success, message: errMsg } = await productcontrollerCreateproduct(values); if (!success) { throw new Error(errMsg); } tableRef.current?.reload(); message.success('提交成功'); return true; } catch (error: any) { message.error(error.message); } }} > { const { data = [] } = await productcontrollerGetcategorieall(); return data.map((item) => ({ label: item.name, value: item.id, })); }} rules={[{ required: true, message: '请选择产品分类' }]} /> { const { data = [] } = await productcontrollerGetstrengthall(); return data.map((item) => ({ label: item.name, value: item.id, })); }} rules={[{ required: true, message: '请选择强度' }]} /> { const { data = [] } = await productcontrollerGetflavorsall(); return data.map((item) => ({ label: item.name, value: item.id, })); }} rules={[{ required: true, message: '请选择口味' }]} /> ); }; export default List;