86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame^] | 1 | |
| 2 | import React, { useState, useRef, useEffect } from 'react'; |
| 3 | import { useIntl, FormattedMessage, useAccess } from '@umijs/max'; |
| 4 | import type { FormInstance } from 'antd'; |
| 5 | import { Button, message, Modal } from 'antd'; |
| 6 | import { ActionType, FooterToolbar, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components'; |
| 7 | import { PlusOutlined, DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons'; |
| 8 | import { getOperlogList, removeOperlog, addOperlog, updateOperlog, exportOperlog } from '@/services/monitor/operlog'; |
| 9 | import UpdateForm from './detail'; |
| 10 | import { getDictValueEnum } from '@/services/system/dict'; |
| 11 | import DictTag from '@/components/DictTag'; |
| 12 | |
| 13 | /** |
| 14 | * 添加节点 |
| 15 | * |
| 16 | * @param fields |
| 17 | */ |
| 18 | const handleAdd = async (fields: API.Monitor.Operlog) => { |
| 19 | const hide = message.loading('正在添加'); |
| 20 | try { |
| 21 | const resp = await addOperlog({ ...fields }); |
| 22 | hide(); |
| 23 | if (resp.code === 200) { |
| 24 | message.success('添加成功'); |
| 25 | } else { |
| 26 | message.error(resp.msg); |
| 27 | } |
| 28 | return true; |
| 29 | } catch (error) { |
| 30 | hide(); |
| 31 | message.error('添加失败请重试!'); |
| 32 | return false; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * 更新节点 |
| 38 | * |
| 39 | * @param fields |
| 40 | */ |
| 41 | const handleUpdate = async (fields: API.Monitor.Operlog) => { |
| 42 | const hide = message.loading('正在更新'); |
| 43 | try { |
| 44 | const resp = await updateOperlog(fields); |
| 45 | hide(); |
| 46 | if (resp.code === 200) { |
| 47 | message.success('更新成功'); |
| 48 | } else { |
| 49 | message.error(resp.msg); |
| 50 | } |
| 51 | return true; |
| 52 | } catch (error) { |
| 53 | hide(); |
| 54 | message.error('配置失败请重试!'); |
| 55 | return false; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * 删除节点 |
| 61 | * |
| 62 | * @param selectedRows |
| 63 | */ |
| 64 | const handleRemove = async (selectedRows: API.Monitor.Operlog[]) => { |
| 65 | const hide = message.loading('正在删除'); |
| 66 | if (!selectedRows) return true; |
| 67 | try { |
| 68 | const resp = await removeOperlog(selectedRows.map((row) => row.operId).join(',')); |
| 69 | hide(); |
| 70 | if (resp.code === 200) { |
| 71 | message.success('删除成功,即将刷新'); |
| 72 | } else { |
| 73 | message.error(resp.msg); |
| 74 | } |
| 75 | return true; |
| 76 | } catch (error) { |
| 77 | hide(); |
| 78 | message.error('删除失败,请重试'); |
| 79 | return false; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * 导出数据 |
| 86 | * |
| 87 | * |
| 88 | */ |
| 89 | const handleExport = async () => { |
| 90 | const hide = message.loading('正在导出'); |
| 91 | try { |
| 92 | await exportOperlog(); |
| 93 | hide(); |
| 94 | message.success('导出成功'); |
| 95 | return true; |
| 96 | } catch (error) { |
| 97 | hide(); |
| 98 | message.error('导出失败,请重试'); |
| 99 | return false; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | const OperlogTableList: React.FC = () => { |
| 105 | const formTableRef = useRef<FormInstance>(); |
| 106 | |
| 107 | const [modalVisible, setModalVisible] = useState<boolean>(false); |
| 108 | |
| 109 | const actionRef = useRef<ActionType>(); |
| 110 | const [currentRow, setCurrentRow] = useState<API.Monitor.Operlog>(); |
| 111 | const [selectedRows, setSelectedRows] = useState<API.Monitor.Operlog[]>([]); |
| 112 | |
| 113 | const [businessTypeOptions, setBusinessTypeOptions] = useState<any>([]); |
| 114 | const [operatorTypeOptions, setOperatorTypeOptions] = useState<any>([]); |
| 115 | const [statusOptions, setStatusOptions] = useState<any>([]); |
| 116 | |
| 117 | const access = useAccess(); |
| 118 | |
| 119 | /** 国际化配置 */ |
| 120 | const intl = useIntl(); |
| 121 | |
| 122 | useEffect(() => { |
| 123 | getDictValueEnum('sys_oper_type', true).then((data) => { |
| 124 | setBusinessTypeOptions(data); |
| 125 | }); |
| 126 | getDictValueEnum('sys_oper_type', true).then((data) => { |
| 127 | setOperatorTypeOptions(data); |
| 128 | }); |
| 129 | getDictValueEnum('sys_common_status', true).then((data) => { |
| 130 | setStatusOptions(data); |
| 131 | }); |
| 132 | }, []); |
| 133 | |
| 134 | const columns: ProColumns<API.Monitor.Operlog>[] = [ |
| 135 | { |
| 136 | title: <FormattedMessage id="monitor.operlog.oper_id" defaultMessage="日志主键" />, |
| 137 | dataIndex: 'operId', |
| 138 | valueType: 'text', |
| 139 | hideInSearch: true, |
| 140 | }, |
| 141 | { |
| 142 | title: <FormattedMessage id="monitor.operlog.title" defaultMessage="操作模块" />, |
| 143 | dataIndex: 'title', |
| 144 | valueType: 'text', |
| 145 | }, |
| 146 | { |
| 147 | title: <FormattedMessage id="monitor.operlog.business_type" defaultMessage="业务类型" />, |
| 148 | dataIndex: 'businessType', |
| 149 | valueType: 'select', |
| 150 | valueEnum: businessTypeOptions, |
| 151 | render: (_, record) => { |
| 152 | return (<DictTag enums={businessTypeOptions} value={record.businessType} />); |
| 153 | }, |
| 154 | }, |
| 155 | { |
| 156 | title: <FormattedMessage id="monitor.operlog.request_method" defaultMessage="请求方式" />, |
| 157 | dataIndex: 'requestMethod', |
| 158 | valueType: 'text', |
| 159 | }, |
| 160 | { |
| 161 | title: <FormattedMessage id="monitor.operlog.operator_type" defaultMessage="操作类别" />, |
| 162 | dataIndex: 'operatorType', |
| 163 | valueType: 'select', |
| 164 | valueEnum: operatorTypeOptions, |
| 165 | render: (_, record) => { |
| 166 | return (<DictTag enums={operatorTypeOptions} value={record.operatorType} />); |
| 167 | }, |
| 168 | }, |
| 169 | { |
| 170 | title: <FormattedMessage id="monitor.operlog.oper_name" defaultMessage="操作人员" />, |
| 171 | dataIndex: 'operName', |
| 172 | valueType: 'text', |
| 173 | }, |
| 174 | { |
| 175 | title: <FormattedMessage id="monitor.operlog.oper_ip" defaultMessage="主机地址" />, |
| 176 | dataIndex: 'operIp', |
| 177 | valueType: 'text', |
| 178 | }, |
| 179 | { |
| 180 | title: <FormattedMessage id="monitor.operlog.oper_location" defaultMessage="操作地点" />, |
| 181 | dataIndex: 'operLocation', |
| 182 | valueType: 'text', |
| 183 | }, |
| 184 | { |
| 185 | title: <FormattedMessage id="monitor.operlog.status" defaultMessage="操作状态" />, |
| 186 | dataIndex: 'status', |
| 187 | valueType: 'select', |
| 188 | valueEnum: statusOptions, |
| 189 | render: (_, record) => { |
| 190 | return (<DictTag key="status" enums={statusOptions} value={record.status} />); |
| 191 | }, |
| 192 | }, |
| 193 | { |
| 194 | title: <FormattedMessage id="monitor.operlog.oper_time" defaultMessage="操作时间" />, |
| 195 | dataIndex: 'operTime', |
| 196 | valueType: 'dateTime', |
| 197 | }, |
| 198 | { |
| 199 | title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="操作" />, |
| 200 | dataIndex: 'option', |
| 201 | width: '120px', |
| 202 | valueType: 'option', |
| 203 | render: (_, record) => [ |
| 204 | <Button |
| 205 | type="link" |
| 206 | size="small" |
| 207 | key="edit" |
| 208 | hidden={!access.hasPerms('system:operlog:edit')} |
| 209 | onClick={() => { |
| 210 | setModalVisible(true); |
| 211 | setCurrentRow(record); |
| 212 | }} |
| 213 | > |
| 214 | 详细 |
| 215 | </Button>, |
| 216 | ], |
| 217 | }, |
| 218 | ]; |
| 219 | |
| 220 | return ( |
| 221 | <PageContainer> |
| 222 | <div style={{ width: '100%', float: 'right' }}> |
| 223 | <ProTable<API.Monitor.Operlog> |
| 224 | headerTitle={intl.formatMessage({ |
| 225 | id: 'pages.searchTable.title', |
| 226 | defaultMessage: '信息', |
| 227 | })} |
| 228 | actionRef={actionRef} |
| 229 | formRef={formTableRef} |
| 230 | rowKey="operId" |
| 231 | key="operlogList" |
| 232 | search={{ |
| 233 | labelWidth: 120, |
| 234 | }} |
| 235 | toolBarRender={() => [ |
| 236 | <Button |
| 237 | type="primary" |
| 238 | key="add" |
| 239 | hidden={!access.hasPerms('system:operlog:add')} |
| 240 | onClick={async () => { |
| 241 | setCurrentRow(undefined); |
| 242 | setModalVisible(true); |
| 243 | }} |
| 244 | > |
| 245 | <PlusOutlined /> <FormattedMessage id="pages.searchTable.new" defaultMessage="新建" /> |
| 246 | </Button>, |
| 247 | <Button |
| 248 | type="primary" |
| 249 | key="remove" |
| 250 | danger |
| 251 | hidden={selectedRows?.length === 0 || !access.hasPerms('system:operlog:remove')} |
| 252 | onClick={async () => { |
| 253 | Modal.confirm({ |
| 254 | title: '是否确认删除所选数据项?', |
| 255 | icon: <ExclamationCircleOutlined />, |
| 256 | content: '请谨慎操作', |
| 257 | async onOk() { |
| 258 | const success = await handleRemove(selectedRows); |
| 259 | if (success) { |
| 260 | setSelectedRows([]); |
| 261 | actionRef.current?.reloadAndRest?.(); |
| 262 | } |
| 263 | }, |
| 264 | onCancel() { }, |
| 265 | }); |
| 266 | }} |
| 267 | > |
| 268 | <DeleteOutlined /> |
| 269 | <FormattedMessage id="pages.searchTable.delete" defaultMessage="删除" /> |
| 270 | </Button>, |
| 271 | <Button |
| 272 | type="primary" |
| 273 | key="export" |
| 274 | hidden={!access.hasPerms('system:operlog:export')} |
| 275 | onClick={async () => { |
| 276 | handleExport(); |
| 277 | }} |
| 278 | > |
| 279 | <PlusOutlined /> |
| 280 | <FormattedMessage id="pages.searchTable.export" defaultMessage="导出" /> |
| 281 | </Button>, |
| 282 | ]} |
| 283 | request={(params) => |
| 284 | getOperlogList({ ...params } as API.Monitor.OperlogListParams).then((res) => { |
| 285 | const result = { |
| 286 | data: res.rows, |
| 287 | total: res.total, |
| 288 | success: true, |
| 289 | }; |
| 290 | return result; |
| 291 | }) |
| 292 | } |
| 293 | columns={columns} |
| 294 | rowSelection={{ |
| 295 | onChange: (_, selectedRows) => { |
| 296 | setSelectedRows(selectedRows); |
| 297 | }, |
| 298 | }} |
| 299 | /> |
| 300 | </div> |
| 301 | {selectedRows?.length > 0 && ( |
| 302 | <FooterToolbar |
| 303 | extra={ |
| 304 | <div> |
| 305 | <FormattedMessage id="pages.searchTable.chosen" defaultMessage="已选择" /> |
| 306 | <a style={{ fontWeight: 600 }}>{selectedRows.length}</a> |
| 307 | <FormattedMessage id="pages.searchTable.item" defaultMessage="项" /> |
| 308 | </div> |
| 309 | } |
| 310 | > |
| 311 | <Button |
| 312 | key="remove" |
| 313 | danger |
| 314 | hidden={!access.hasPerms('system:operlog:del')} |
| 315 | onClick={async () => { |
| 316 | Modal.confirm({ |
| 317 | title: '删除', |
| 318 | content: '确定删除该项吗?', |
| 319 | okText: '确认', |
| 320 | cancelText: '取消', |
| 321 | onOk: async () => { |
| 322 | const success = await handleRemove(selectedRows); |
| 323 | if (success) { |
| 324 | setSelectedRows([]); |
| 325 | actionRef.current?.reloadAndRest?.(); |
| 326 | } |
| 327 | }, |
| 328 | }); |
| 329 | }} |
| 330 | > |
| 331 | <FormattedMessage id="pages.searchTable.batchDeletion" defaultMessage="批量删除" /> |
| 332 | </Button> |
| 333 | </FooterToolbar> |
| 334 | )} |
| 335 | <UpdateForm |
| 336 | onSubmit={async (values) => { |
| 337 | let success = false; |
| 338 | if (values.operId) { |
| 339 | success = await handleUpdate({ ...values } as API.Monitor.Operlog); |
| 340 | } else { |
| 341 | success = await handleAdd({ ...values } as API.Monitor.Operlog); |
| 342 | } |
| 343 | if (success) { |
| 344 | setModalVisible(false); |
| 345 | setCurrentRow(undefined); |
| 346 | if (actionRef.current) { |
| 347 | actionRef.current.reload(); |
| 348 | } |
| 349 | } |
| 350 | }} |
| 351 | onCancel={() => { |
| 352 | setModalVisible(false); |
| 353 | setCurrentRow(undefined); |
| 354 | }} |
| 355 | open={modalVisible} |
| 356 | values={currentRow || {}} |
| 357 | businessTypeOptions={businessTypeOptions} |
| 358 | operatorTypeOptions={operatorTypeOptions} |
| 359 | statusOptions={statusOptions} |
| 360 | /> |
| 361 | </PageContainer> |
| 362 | ); |
| 363 | }; |
| 364 | |
| 365 | export default OperlogTableList; |