添加登录日志

This commit is contained in:
xie7654
2025-07-02 17:17:17 +08:00
parent 6cd05925ff
commit eace8a524d
19 changed files with 443 additions and 23 deletions

View File

@@ -98,6 +98,10 @@
"user": {
"name": "User",
"title": "User Management"
},
"login_log": {
"name": "login log",
"title": "login log"
},
"status": "Status",
"remark": "Remarks",

View File

@@ -100,6 +100,10 @@
"name": "用户",
"title": "用户管理"
},
"login_log": {
"name": "登录日志",
"title": "登录日志"
},
"status": "状态",
"remark": "备注",
"creator": "创建人",

View File

@@ -0,0 +1,23 @@
import { BaseModel } from '#/models/base';
export namespace SystemLoginLogApi {
export interface SystemLoginLog {
id: number;
remark: string;
creator: string;
modifier: string;
update_time: string;
create_time: string;
is_deleted: boolean;
username: string;
result: number;
user_ip: string;
user_agent: string;
}
}
export class SystemLoginLogModel extends BaseModel<SystemLoginLogApi.SystemLoginLog> {
constructor() {
super('/system/login_log/');
}
}

View File

@@ -24,7 +24,7 @@ const MOCK_USER_OPTIONS: BasicOption[] = [
},
{
label: 'User',
value: 'jack',
value: 'xj',
},
];

View File

@@ -0,0 +1,95 @@
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
import type { VbenFormSchema } from '#/adapter/form';
import type { SystemLoginLogApi } from '#/models/system/login_log';
import { z } from '#/adapter/form';
import { $t } from '#/locales';
import { format_datetime } from '#/utils/date';
/**
* 获取编辑表单的字段配置
*/
export function useSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'username',
label: 'username',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['username']))
.max(100, $t('ui.formRules.maxLength', ['username', 100])),
},
{
component: 'InputNumber',
fieldName: 'result',
label: 'result',
},
{
component: 'Input',
fieldName: 'user_ip',
label: 'user ip',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['user ip']))
.max(100, $t('ui.formRules.maxLength', ['user ip', 100])),
},
{
component: 'Input',
fieldName: 'user_agent',
label: 'user agent',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['user agent']))
.max(100, $t('ui.formRules.maxLength', ['user agent', 100])),
},
{
component: 'Input',
fieldName: 'remark',
label: 'remark',
rules: z
.string()
.min(1, $t('ui.formRules.required', ['remark']))
.max(100, $t('ui.formRules.maxLength', ['remark', 100])),
},
];
}
/**
* 获取表格列配置
* @description 使用函数的形式返回列数据而不是直接export一个Array常量是为了响应语言切换时重新翻译表头
*/
export function useColumns(): VxeTableGridOptions<SystemLoginLogApi.SystemLoginLog>['columns'] {
return [
{
field: 'id',
title: 'ID',
},
{
field: 'username',
title: '用户名',
},
{
cellRender: {
name: 'CellTag',
},
field: 'result_text',
title: '登录结果',
},
{
field: 'user_ip',
title: '登录地址',
},
{
field: 'user_agent',
title: '浏览器',
},
{
field: 'create_time',
title: $t('system.createTime'),
width: 150,
formatter: ({ cellValue }) => format_datetime(cellValue),
},
];
}

View File

@@ -0,0 +1,47 @@
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { Page } from '@vben/common-ui';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { SystemLoginLogModel } from '#/models/system/login_log';
import { useColumns } from './data';
const formModel = new SystemLoginLogModel();
const [Grid] = useVbenVxeGrid({
gridEvents: {},
gridOptions: {
columns: useColumns(),
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: true,
},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await formModel.list({
page: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
toolbarConfig: {
custom: true,
export: false,
refresh: { code: 'query' },
zoom: true,
},
} as VxeTableGridOptions,
});
</script>
<template>
<Page auto-content-height>
<Grid table-title="系统访问记录" />
</Page>
</template>

View File

@@ -0,0 +1,79 @@
<script lang="ts" setup>
import type { SystemLoginLogApi } from '#/models/system/login_log';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { Button } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { $t } from '#/locales';
import { SystemLoginLogModel } from '#/models/system/login_log';
import { useSchema } from '../data';
const emit = defineEmits(['success']);
const formModel = new SystemLoginLogModel();
const formData = ref<SystemLoginLogApi.SystemLoginLog>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', [$t('system.login_log.name')])
: $t('ui.actionTitle.create', [$t('system.login_log.name')]);
});
const [Form, formApi] = useVbenForm({
layout: 'horizontal',
schema: useSchema(),
showDefaultActions: false,
});
function resetForm() {
formApi.resetForm();
formApi.setValues(formData.value || {});
}
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (valid) {
modalApi.lock();
const data = await formApi.getValues();
try {
await (formData.value?.id
? formModel.update(formData.value.id, data)
: formModel.create(data));
await modalApi.close();
emit('success');
} finally {
modalApi.lock(false);
}
}
},
onOpenChange(isOpen) {
if (isOpen) {
const data = modalApi.getData<SystemLoginLogApi.SystemLoginLog>();
if (data) {
formData.value = data;
formApi.setValues(formData.value);
}
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form />
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
</div>
</template>
</Modal>
</template>
<style lang="css" scoped></style>

View File

@@ -102,7 +102,7 @@ export function useColumns(
cellRender: {
attrs: {
nameField: 'name',
nameTitle: $t('system.{model_name_snake}.name'),
nameTitle: $t('system.post.name'),
onClick: onActionClick,
},
name: 'CellOperation',