import { Controller, Get, Del, Inject, Query, Param, All } from "@midwayjs/core"; import { Context } from "@midwayjs/koa"; import { LogService } from "../service/log.service"; @Controller("/api/logs") export class LogController { @Inject() ctx!: Context; @Inject() logService!: LogService; @Get("/") async list(@Query() query: { page?: number; pageSize?: number; userId?: string; module?: string; action?: string; startDate?: string; endDate?: string }) { const result = await this.logService.list(query); return result; } @Get("/:id") async detail(@Param("id") id: string) { const result = await this.logService.detail(id); return result; } @All("/export") async export(@Query() query: any) { try { const csvContent = await this.logService.exportLogs(query); this.ctx.set("Content-Type", "text/csv;charset=utf-8"); this.ctx.set("Content-Disposition", `attachment; filename=operation_logs_${Date.now()}.csv`); this.ctx.body = csvContent; return; } catch (error: any) { return { code: 500, message: error?.message || "导出失败" }; } } @Del("/clear") async clear() { const result = await this.logService.cleanExpiredLogs(0); return { code: 200, message: `成功清理 ${result} 条日志`, data: { deletedCount: result } }; } }