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