BookSystem/frontend/src/modules/admin/views/logs/login-log-crud.ts
jayhgq 19d6db6ecc feat: 添加登录日志和操作日志功能
1. 新增日志相关数据库模型、API路由、前端页面与权限配置
2. 为现有业务接口添加操作日志记录功能
3. 优化导入顺序与权限初始化数据
2026-06-12 22:41:41 +08:00

125 lines
3.2 KiB
TypeScript

import type { CreateCrudOptions } from "@fast-crud/fast-crud";
import { dict } from "@fast-crud/fast-crud";
import dayjs from "dayjs";
import { http } from "@/modules/admin/api/http";
export type LoginLogRow = {
id: number;
username: string;
login_time: string;
ip_address: string;
user_agent: string;
status: string;
failure_reason: string | null;
};
export function buildLoginLogCrudOptions(opts: {
onClean: () => void;
}): CreateCrudOptions<LoginLogRow> {
return () => ({
crudOptions: {
pagination: { show: false },
search: {
show: true,
options: { layout: "inline" },
expand: false,
},
request: {
pageRequest: async (query) => {
const { data } = await http.post<LoginLogRow[]>(
"/login-logs/search",
query
);
return data;
},
transformQuery: ({ form }) => {
const params: Record<string, unknown> = {};
if (form?.username) params.username = form.username;
if (form?.status) params.status = form.status;
return params;
},
transformRes: ({ res }) => {
const records = Array.isArray(res) ? res : [];
return {
currentPage: 1,
pageSize: Math.max(records.length, 1),
total: records.length,
records,
};
},
},
rowHandle: { show: false },
toolbar: {
buttons: {
clean: {
text: "清理旧日志",
type: "warning",
click: () => opts.onClean(),
},
},
},
columns: {
id: {
title: "ID",
key: "id",
type: "number",
column: { width: 70 },
form: { show: false },
},
username: {
title: "用户名",
key: "username",
type: "text",
search: { show: true },
},
status: {
title: "状态",
key: "status",
type: "dict-select",
search: { show: true },
dict: dict({
data: [
{ value: "success", label: "成功" },
{ value: "failure", label: "失败" },
],
value: "value",
label: "label",
}),
column: { width: 80 },
},
failure_reason: {
title: "失败原因",
key: "failure_reason",
type: "text",
column: { width: 160 },
},
ip_address: {
title: "IP地址",
key: "ip_address",
type: "text",
column: { width: 150 },
},
user_agent: {
title: "User-Agent",
key: "user_agent",
type: "text",
column: { width: 300 },
},
login_time: {
title: "登录时间",
key: "login_time",
type: "text",
column: { width: 170 },
form: { show: false },
valueBuilder({ value, row, key }) {
if (!row || value == null) return;
(row as Record<string, unknown>)[key as string] = dayjs(
value
).format("YYYY-MM-DD HH:mm:ss");
},
},
},
},
});
}