feat: 操作日志

This commit is contained in:
dap 2024-10-05 12:12:14 +08:00
parent 82e63dbcf6
commit 51c63c1b69
4 changed files with 2373 additions and 2428 deletions

View File

@ -4,6 +4,8 @@ import { setupVbenVxeTable, useVbenVxeGrid } from '@vben/plugins/vxe-table';
import { Button, Image } from 'ant-design-vue';
import { renderDict } from '#/utils/render';
import { useVbenForm } from './form';
setupVbenVxeTable({
@ -16,15 +18,15 @@ setupVbenVxeTable({
proxyConfig: {
autoLoad: true,
response: {
result: 'items',
result: 'rows',
total: 'total',
list: 'items',
list: 'rows',
},
showActiveMsg: true,
showResponseMsg: false,
},
round: true,
size: 'small',
size: 'medium',
},
});
@ -48,6 +50,23 @@ setupVbenVxeTable({
},
});
/**
* dict渲染 props: { field: 参数名, dictName: 字典名 }
*/
vxeUI.renderer.add('DictTag', {
renderDefault(renderOpts, params) {
const { props } = renderOpts;
const field = props?.field;
const dictName = props?.dictName;
if (!field || !dictName) {
console.warn('DictTag: field or dictName is not provided');
return 'error';
}
const { row } = params;
return renderDict(row[field], dictName);
},
});
// 这里可以自行扩展 vxe-table 的全局配置,比如自定义格式化
// vxeUI.formats.add
},

View File

@ -1,6 +1,4 @@
import type { ColumnType } from 'ant-design-vue/es/table';
import type { FormSchemaGetter } from '#/adapter';
import type { FormSchemaGetter, VxeGridProps } from '#/adapter';
import type { DescItem } from '#/components/description';
import { DictEnum } from '@vben/constants';
@ -50,66 +48,48 @@ export const querySchema: FormSchemaGetter = () => [
component: 'RangePicker',
fieldName: 'createTime',
label: '操作时间',
componentProps: {
valueFormat: 'YYYY-MM-DD HH:mm:ss',
},
},
];
export const columns: ColumnType[] = [
export const columns: VxeGridProps['columns'] = [
{ type: 'checkbox', width: 60 },
{ field: 'title', title: '系统模块' },
{
align: 'center',
dataIndex: 'title',
title: '系统模块',
},
{
align: 'center',
customRender({ value }) {
return renderDict(value, DictEnum.SYS_OPER_TYPE);
},
dataIndex: 'businessType',
title: '操作类型',
},
{
align: 'center',
dataIndex: 'operName',
title: '操作人员',
},
{
align: 'center',
dataIndex: 'operIp',
title: 'IP地址',
},
{
align: 'center',
dataIndex: 'operLocation',
title: 'IP信息',
},
{
align: 'center',
customRender({ value }) {
return renderDict(value, DictEnum.SYS_COMMON_STATUS);
field: 'businessType',
cellRender: {
name: 'DictTag',
props: { field: 'businessType', dictName: DictEnum.SYS_OPER_TYPE },
},
dataIndex: 'status',
},
{ field: 'operName', title: '操作人员' },
{ field: 'operIp', title: 'IP地址' },
{ field: 'operLocation', title: 'IP信息' },
{
field: 'status',
title: '操作状态',
},
{
align: 'center',
dataIndex: 'operTime',
sorter: true,
title: '操作日期',
},
{
align: 'center',
customRender({ text }) {
return `${text} ms`;
cellRender: {
name: 'DictTag',
props: { field: 'status', dictName: DictEnum.SYS_COMMON_STATUS },
},
dataIndex: 'costTime',
sorter: true,
},
{ field: 'operTime', title: '操作日期' },
{
field: 'costTime',
title: '操作耗时',
formatter({ cellValue }) {
return `${cellValue} ms`;
},
},
{
align: 'center',
dataIndex: 'action',
field: 'action',
fixed: 'right',
slots: { default: 'action' },
title: '操作',
width: 120,
},
];

View File

@ -3,70 +3,160 @@ import type { Recordable } from '@vben/types';
import type { OperationLog } from '#/api/monitor/operlog/model';
import { onMounted, ref } from 'vue';
import { computed } from 'vue';
import { Page, useVbenDrawer } from '@vben/common-ui';
import { Page, useVbenDrawer, type VbenFormProps } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { Card, Table } from 'ant-design-vue';
import { Modal, Space } from 'ant-design-vue';
import dayjs from 'dayjs';
import { useVbenForm } from '#/adapter';
import { operLogList } from '#/api/monitor/operlog';
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter';
import {
operLogClean,
operLogDelete,
operLogExport,
operLogList,
} from '#/api/monitor/operlog';
import { downloadExcel } from '#/utils/file/download';
import { confirmDeleteModal } from '#/utils/modal';
import { columns, querySchema } from './data';
import operationPreviewDrawer from './OperationPreviewDrawer.vue';
const [QueryForm] = useVbenForm({
//
collapsed: false,
//
commonConfig: {
//
componentProps: {
class: 'w-full',
const formOptions: VbenFormProps = {
schema: querySchema(),
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};
const gridOptions: VxeGridProps<OperationLog> = {
checkboxConfig: {
//
highlight: true,
//
reserve: true,
//
trigger: 'row',
},
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
//
if (formValues?.createTime) {
formValues.params = {
beginTime: dayjs(formValues.createTime[0]).format(
'YYYY-MM-DD 00:00:00',
),
endTime: dayjs(formValues.createTime[1]).format(
'YYYY-MM-DD 23:59:59',
),
};
Reflect.deleteProperty(formValues, 'createTime');
} else {
Reflect.deleteProperty(formValues, 'params');
}
return await operLogList({
pageNum: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
schema: querySchema(),
//
showCollapseButton: true,
submitButtonOptions: {
text: '查询',
rowConfig: {
isHover: true,
keyField: 'operId',
},
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
});
round: true,
align: 'center',
showOverflow: true,
};
const dataSource = ref<OperationLog[]>([]);
onMounted(async () => {
const resp = await operLogList({ pageNum: 1, pageSize: 30 });
dataSource.value = resp.rows;
});
const [BasicTable, tableApi] = useVbenVxeGrid({ formOptions, gridOptions });
const [OperationPreviewDrawer, drawerApi] = useVbenDrawer({
connectedComponent: operationPreviewDrawer,
});
/**
* 预览
* @param record 操作日志记录
*/
function handlePreview(record: Recordable<any>) {
drawerApi.setData({ record });
drawerApi.open();
}
/**
* 清空全部日志
*/
function handleClear() {
confirmDeleteModal({
onValidated: async () => {
await operLogClean();
},
});
}
/**
* TODO: 选中条件的判断
*/
const checked = computed(() => {
return true;
});
/**
* 删除日志
*/
async function handleDelete() {
const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: any) => row.operId);
Modal.confirm({
title: '提示',
okType: 'danger',
content: `确认删除选中的${ids.length}条操作日志吗?`,
onOk: async () => {
await operLogDelete(ids);
await tableApi.reload();
},
});
}
</script>
<template>
<Page>
<Card>
<QueryForm />
</Card>
<div class="bg-background"></div>
<Table :columns="columns" :data-source="dataSource">
<template #bodyCell="{ record, column }">
<template v-if="column.dataIndex === 'action'">
<a-button size="small" type="link" @click="handlePreview(record)">
{{ $t('pages.common.preview') }}
</a-button>
</template>
<Page :auto-content-height="true">
<BasicTable>
<template #toolbar-actions>
<span class="pl-[7px] text-[16px]">操作日志列表</span>
</template>
</Table>
<template #toolbar-tools>
<Space>
<a-button @click="handleClear">
{{ $t('pages.common.clear') }}
</a-button>
<a-button @click="downloadExcel(operLogExport, '操作日志', {})">
{{ $t('pages.common.export') }}
</a-button>
<a-button
:disabled="!checked"
danger
type="primary"
@click="handleDelete"
>
{{ $t('pages.common.delete') }}
</a-button>
</Space>
</template>
<template #action="{ row }">
<a-button size="small" type="link" @click="handlePreview(row)">
{{ $t('pages.common.preview') }}
</a-button>
</template>
</BasicTable>
<OperationPreviewDrawer />
</Page>
</template>

File diff suppressed because it is too large Load Diff