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 { return () => ({ crudOptions: { pagination: { show: false }, search: { show: true, options: { layout: "inline" }, expand: false, }, request: { pageRequest: async (query) => { const { data } = await http.post( "/login-logs/search", query ); return data; }, transformQuery: ({ form }) => { const params: Record = {}; 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)[key as string] = dayjs( value ).format("YYYY-MM-DD HH:mm:ss"); }, }, }, }, }); }