From 9459461fe69383fe45adac57c564a8ef870028d5 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Wed, 8 Nov 2023 14:31:22 +0800 Subject: [PATCH] Optimize sort and filter in log page --- src/ant-design.d.ts | 9 ++-- src/global.d.ts | 8 +-- src/pages/system/Log.tsx | 112 +++++++++++++++++++++++++++------------ 3 files changed, 90 insertions(+), 39 deletions(-) diff --git a/src/ant-design.d.ts b/src/ant-design.d.ts index e9d563c..25931c6 100644 --- a/src/ant-design.d.ts +++ b/src/ant-design.d.ts @@ -1,14 +1,17 @@ import * as React from 'react' import { CustomIconComponentProps } from '@ant-design/icons/es/components/Icon' import { TablePaginationConfig } from 'antd/lib' -import { FilterValue } from 'antd/es/table/interface' +import { ColumnsType, FilterValue, SorterResult, SortOrder } from 'antd/es/table/interface' declare global { type IconComponent = | React.ComponentType> | React.ForwardRefExoticComponent - type PaginationConfig = TablePaginationConfig + type _TablePaginationConfig = TablePaginationConfig - type FilterVal = FilterValue + type _ColumnsType = ColumnsType + type _FilterValue = FilterValue + type _SorterResult = SorterResult + type _SortOrder = SortOrder } diff --git a/src/global.d.ts b/src/global.d.ts index 47e0779..e7171ea 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -157,12 +157,14 @@ interface PageVo { interface PageParam { currentPage?: number pageSize?: number + sortField?: string + sortOrder?: string } interface TableParams { - pagination?: PaginationConfig - sortField?: string - sortOrder?: string + pagination?: _TablePaginationConfig + sortField?: React.Key | readonly React.Key[] + sortOrder?: _SortOrder filters?: Record } diff --git a/src/pages/system/Log.tsx b/src/pages/system/Log.tsx index 24d6056..c311814 100644 --- a/src/pages/system/Log.tsx +++ b/src/pages/system/Log.tsx @@ -1,6 +1,4 @@ import React, { useState } from 'react' -import { ColumnsType, FilterValue, SorterResult } from 'antd/es/table/interface' -import { TablePaginationConfig } from 'antd/lib' import FitFullScreen from '@/components/common/FitFullScreen.tsx' import Card from '@/components/common/Card' import { r_getSysLog } from '@/services/system.tsx' @@ -20,7 +18,7 @@ const Log: React.FC = () => { } }) - const dataColumns: ColumnsType = [ + const dataColumns: _ColumnsType = [ { title: '类型', dataIndex: 'logType', @@ -30,7 +28,11 @@ const Log: React.FC = () => { ) : ( {value} ), - align: 'center' + align: 'center', + filters: [ + { text: 'Info', value: 'INFO' }, + { text: 'Error', value: 'ERROR' } + ] }, { title: '操作者', @@ -43,59 +45,93 @@ const Log: React.FC = () => { Anonymous ) }, - { - title: '操作时间', - dataIndex: 'operateTime', - render: (value: string) => getLocalTime(value), - align: 'center' - }, { title: '请求方式', dataIndex: 'requestMethod', - align: 'center' + align: 'center', + filters: [ + { text: 'GET', value: 'GET' }, + { text: 'POST', value: 'POST' }, + { text: 'PUT', value: 'PUT' }, + { text: 'PATCH', value: 'PATCH' }, + { text: 'DELETE', value: 'DELETE' }, + { text: 'HEAD', value: 'HEAD' }, + { text: 'OPTIONS', value: 'OPTIONS' } + ] }, { title: '请求 Url', render: (_value, record) => `${record.requestServerAddress}${record.requestUri}${ record.requestParams ? `?${record.requestParams}` : '' - }` + }`, + onCell: () => ({ + style: { + wordBreak: 'break-word' + } + }) }, { title: '请求 IP', dataIndex: 'requestIp', align: 'center' }, + { + title: '开始时间', + dataIndex: 'startTime', + render: (value: string) => getLocalTime(value), + align: 'center', + sorter: true + }, + { + title: '执行时间', + dataIndex: 'executeTime', + render: (value, record) => ( + + {`${value}ms`} + + ), + align: 'center', + sorter: true + }, { title: '异常', dataIndex: 'exception', render: (value: boolean, record) => (value ? record.exceptionInfo : '无'), align: 'center' }, - { - title: '执行时间', - dataIndex: 'executeTime', - render: (value, record) => ( - - {`${value}ms`} - - ), - align: 'center' - }, { title: '用户代理', - dataIndex: 'userAgent' + dataIndex: 'userAgent', + onCell: () => ({ + style: { + wordBreak: 'break-word' + } + }) } ] const handleOnTableChange = ( - pagination: TablePaginationConfig, - filters: Record, - sorter: SorterResult | SorterResult[] + pagination: _TablePaginationConfig, + filters: Record, + sorter: _SorterResult | _SorterResult[] ) => { - setTableParams({ pagination, filters, ...sorter }) + if (Array.isArray(sorter)) { + setTableParams({ + pagination, + filters, + sortField: sorter.map((value) => value.field).join(',') + }) + } else { + setTableParams({ + pagination, + filters, + sortField: sorter.field, + sortOrder: sorter.order + }) + } if (pagination.pageSize !== tableParams.pagination?.pageSize) { setLogData([]) @@ -107,10 +143,20 @@ const Log: React.FC = () => { return } - void r_getSysLog({ - currentPage: tableParams.pagination?.current, - pageSize: tableParams.pagination?.pageSize - }) + void r_getSysLog( + tableParams.sortField && tableParams.sortOrder + ? { + currentPage: tableParams.pagination?.current, + pageSize: tableParams.pagination?.pageSize, + sortField: tableParams.sortField as string, + sortOrder: tableParams.sortOrder, + ...tableParams.filters + } + : { + currentPage: tableParams.pagination?.current, + pageSize: tableParams.pagination?.pageSize + } + ) .then((res) => { const data = res.data if (data.code === DATABASE_SELECT_SUCCESS) {