|   | 
| import React, { useState, useRef, useEffect } from 'react'; | 
| import { Button, message, Modal, Tag } from 'antd'; | 
| import { | 
|     FooterToolbar, | 
|     PageContainer, | 
|     ProTable, | 
|     LightFilter, | 
| } from '@ant-design/pro-components'; | 
| import { PlusOutlined, ExportOutlined } from '@ant-design/icons'; | 
| import Http from '@/utils/http'; | 
| import Edit from './components/edit' | 
| import { TextFilter, SelectFilter, DatetimeRangeFilter, LinkFilter } from '@/components/TableSearch' | 
| import { repairBug } from '@/utils/common-util'; | 
| import { statusMap } from '@/utils/enum-util' | 
|   | 
| const TABLE_KEY = "pro-table-host"; | 
|   | 
| const handleSave = async (val) => { | 
|     const hide = message.loading('正在添加'); | 
|     try { | 
|         const resp = await Http.doPost('api/host/save', val); | 
|         if (resp.code === 200) { | 
|             message.success('添加成功'); | 
|             return true; | 
|         } else { | 
|             message.error(resp.msg); | 
|             return false; | 
|         } | 
|     } catch (error) { | 
|         message.error('添加失败请重试!'); | 
|         return false; | 
|     } finally { | 
|         hide(); | 
|     } | 
| }; | 
|   | 
| const handleUpdate = async (val) => { | 
|     const hide = message.loading('正在更新'); | 
|     try { | 
|         const resp = await Http.doPost('api/host/update', val); | 
|         if (resp.code === 200) { | 
|             message.success('更新成功'); | 
|             return true; | 
|         } else { | 
|             message.error(resp.msg); | 
|             return false; | 
|         } | 
|     } catch (error) { | 
|         message.error('配置失败请重试!'); | 
|         return false; | 
|     } finally { | 
|         hide(); | 
|     } | 
| }; | 
|   | 
| const handleRemove = async (rows) => { | 
|     if (!rows) return true; | 
|     const hide = message.loading('正在删除'); | 
|     try { | 
|         const resp = await Http.doPost('api/host/remove/' + rows.map((row) => row.id).join(',')); | 
|         if (resp.code === 200) { | 
|             message.success('删除成功'); | 
|             return true; | 
|         } else { | 
|             message.error(resp.msg); | 
|             return false; | 
|         } | 
|     } catch (error) { | 
|         message.error('删除失败,请重试'); | 
|         return false; | 
|     } finally { | 
|         hide(); | 
|     } | 
| }; | 
|   | 
| const handleExport = async () => { | 
|     const hide = message.loading('正在导出'); | 
|     try { | 
|         const resp = await Http.doPostBlob('api/host/export'); | 
|         const blob = new Blob([resp], { type: 'application/vnd.ms-excel' }); | 
|         window.location.href = window.URL.createObjectURL(blob); | 
|         message.success('导出成功'); | 
|         return true; | 
|     } catch (error) { | 
|         message.error('导出失败,请重试'); | 
|         return false; | 
|     } finally { | 
|         hide(); | 
|     } | 
| }; | 
|   | 
|   | 
| const Main = () => { | 
|     const formTableRef = useRef(); | 
|     const actionRef = useRef(); | 
|     const [selectedRows, setSelectedRows] = useState([]); | 
|     const [modalVisible, setModalVisible] = useState(false); | 
|     const [currentRow, setCurrentRow] = useState(); | 
|     const [searchParam, setSearchParam] = useState({}); | 
|   | 
|     useEffect(() => { | 
|   | 
|     }, []); | 
|   | 
|     const columns = [ | 
|         { | 
|             title: 'No', | 
|             dataIndex: 'index', | 
|             valueType: 'indexBorder', | 
|             width: 48, | 
|         }, | 
|         { | 
|             title: '名称', | 
|             dataIndex: 'name', | 
|             valueType: 'text', | 
|             hidden: false, | 
|             width: 140, | 
|             copyable: true, | 
|             filterDropdown: (props) => <TextFilter | 
|                 name='name' | 
|                 {...props} | 
|                 actionRef={actionRef} | 
|                 setSearchParam={setSearchParam} | 
|             />, | 
|         }, | 
|         { | 
|             title: '状态', | 
|             dataIndex: 'status$', | 
|             valueType: 'text', | 
|             hidden: false, | 
|             width: 140, | 
|             filterDropdown: (props) => <SelectFilter | 
|                 name='status' | 
|                 {...props} | 
|                 actionRef={actionRef} | 
|                 setSearchParam={setSearchParam} | 
|                 data={[ | 
|                     { label: '正常', value: 1 }, | 
|                     { label: '禁用', value: 0 }, | 
|                 ]} | 
|             />, | 
|             render: (_, record) => { | 
|                 const status = statusMap[record.status] | 
|                 return <Tag color={status.color}>{status.text}</Tag> | 
|             }, | 
|         }, | 
|         { | 
|             title: '添加时间', | 
|             dataIndex: 'createTime$', | 
|             valueType: 'text', | 
|             hidden: false, | 
|             width: 140, | 
|             filterDropdown: (props) => <DatetimeRangeFilter | 
|                 name='createTime' | 
|                 {...props} | 
|                 actionRef={actionRef} | 
|                 setSearchParam={setSearchParam} | 
|             />, | 
|         }, | 
|         { | 
|             title: '修改时间', | 
|             dataIndex: 'updateTime$', | 
|             valueType: 'text', | 
|             hidden: false, | 
|             width: 140, | 
|             filterDropdown: (props) => <DatetimeRangeFilter | 
|                 name='updateTime' | 
|                 {...props} | 
|                 actionRef={actionRef} | 
|                 setSearchParam={setSearchParam} | 
|             />, | 
|         }, | 
|         { | 
|             title: '备注', | 
|             dataIndex: 'memo', | 
|             valueType: 'text', | 
|             hidden: false, | 
|             width: 140, | 
|             filterDropdown: (props) => <TextFilter | 
|                 name='memo' | 
|                 {...props} | 
|                 actionRef={actionRef} | 
|                 setSearchParam={setSearchParam} | 
|             />, | 
|         }, | 
|   | 
|         { | 
|             title: '操作', | 
|             dataIndex: 'option', | 
|             width: 140, | 
|             valueType: 'option', | 
|             render: (_, record) => [ | 
|                 <Button | 
|                     type="link" | 
|                     key="edit" | 
|                     onClick={() => { | 
|                         setModalVisible(true); | 
|                         setCurrentRow(record); | 
|                     }} | 
|                 > | 
|                     编辑 | 
|                 </Button>, | 
|                 <Button | 
|                     type="link" | 
|                     danger | 
|                     key="batchRemove" | 
|                     onClick={async () => { | 
|                         Modal.confirm({ | 
|                             title: '删除', | 
|                             content: '确定删除该项吗?', | 
|                             onOk: async () => { | 
|                                 const success = await handleRemove([record]); | 
|                                 if (success) { | 
|                                     if (actionRef.current) { | 
|                                         actionRef.current.reload(); | 
|                                     } | 
|                                 } | 
|                             }, | 
|                         }); | 
|                     }} | 
|                 > | 
|                     删除 | 
|                 </Button>, | 
|             ], | 
|         }, | 
|     ]; | 
|   | 
|     return ( | 
|         <PageContainer | 
|             header={{ | 
|                 breadcrumb: {}, | 
|             }} | 
|         > | 
|             <div style={{ width: '100%', float: 'right' }}> | 
|                 <ProTable | 
|                     key="host" | 
|                     rowKey="id" | 
|                     actionRef={actionRef} | 
|                     formRef={formTableRef} | 
|                     columns={columns} | 
|                     cardBordered | 
|                     scroll={{ x: 1300 }} | 
|                     dateFormatter="string" | 
|                     pagination={{ pageSize: 20 }} | 
|                     search={false} | 
|                     toolbar={{ | 
|                         search: { | 
|                             onSearch: (value) => { | 
|                                 setSearchParam(prevState => ({ | 
|                                     ...prevState, | 
|                                     condition: value | 
|                                 })); | 
|                                 actionRef.current?.reload(); | 
|                             }, | 
|                         }, | 
|                         filter: ( | 
|                             <LightFilter | 
|                                 onValuesChange={(val) => { | 
|                                 }} | 
|                             > | 
|                             </LightFilter> | 
|                         ), | 
|                         actions: [ | 
|                             <Button | 
|                                 type="primary" | 
|                                 key="save" | 
|                                 onClick={async () => { | 
|                                     setModalVisible(true) | 
|                                 }} | 
|                             > | 
|                                 <PlusOutlined /> | 
|                                 添加 | 
|                             </Button>, | 
|                             <Button | 
|                                 key="export" | 
|                                 onClick={async () => { | 
|                                     handleExport(); | 
|                                 }} | 
|                             > | 
|                                 <ExportOutlined /> | 
|                                 导出 | 
|                             </Button>, | 
|                         ], | 
|                     }} | 
|                     request={(params, sorter, filter) => | 
|                         Http.doPostPromise('/api/host/page', { ...params, ...searchParam }, (res) => { | 
|                             return { | 
|                                 data: res.data.records, | 
|                                 total: res.data.total, | 
|                                 success: true, | 
|                             } | 
|                         }) | 
|                     } | 
|                     rowSelection={{ | 
|                         onChange: (ids, rows) => { | 
|                             setSelectedRows(rows); | 
|                         } | 
|                     }} | 
|                     columnsState={{ | 
|                         persistenceKey: TABLE_KEY, | 
|                         persistenceType: 'localStorage', | 
|                         defaultValue: { | 
|                             option: { fixed: 'right', disable: true }, | 
|                         }, | 
|                         onChange(value) { | 
|                         }, | 
|                     }} | 
|                 /> | 
|             </div> | 
|   | 
|             {selectedRows?.length > 0 && ( | 
|                 <FooterToolbar | 
|                     extra={ | 
|                         <div> | 
|                             已选择 | 
|                             <a style={{ fontWeight: 600 }}>{selectedRows.length}</a> | 
|                             项 | 
|                         </div> | 
|                     } | 
|                 > | 
|                     <Button | 
|                         key="remove" | 
|                         danger | 
|                         onClick={async () => { | 
|                             Modal.confirm({ | 
|                                 title: '删除', | 
|                                 content: '确定删除该项吗?', | 
|                                 onOk: async () => { | 
|                                     const success = await handleRemove(selectedRows); | 
|                                     if (success) { | 
|                                         setSelectedRows([]); | 
|                                         actionRef.current?.reloadAndRest?.(); | 
|                                     } | 
|                                 }, | 
|                             }); | 
|                         }} | 
|                     > | 
|                         批量删除 | 
|                     </Button> | 
|                 </FooterToolbar> | 
|             )} | 
|   | 
|             <Edit | 
|                 open={modalVisible} | 
|                 values={currentRow || {}} | 
|                 onCancel={ | 
|                     () => { | 
|                         setModalVisible(false); | 
|                         setCurrentRow(undefined); | 
|                     } | 
|                 } | 
|                 onSubmit={async (values) => { | 
|                     let ok = false; | 
|                     if (values.id) { | 
|                         ok = await handleUpdate({ ...values }) | 
|                     } else { | 
|                         ok = await handleSave({ ...values }) | 
|                     } | 
|                     if (ok) { | 
|                         setModalVisible(false); | 
|                         setCurrentRow(undefined); | 
|                         if (actionRef.current) { | 
|                             actionRef.current.reload(); | 
|                         } | 
|                     } | 
|                 } | 
|                 } | 
|             /> | 
|         </PageContainer> | 
|     ); | 
| }; | 
|   | 
| export default Main; |