From 232db047f1a7833d42748c14c0b133b5f8de798f Mon Sep 17 00:00:00 2001 From: lizhuoran <625237490@qq.com> Date: Mon, 9 Mar 2026 10:08:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BA=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ActivityLogExport: 日志导出类 - 支持 Excel (XLSX) 格式导出 - 支持 CSV 格式导出 - 自动格式化中文字段名和值 - 支持根据筛选条件导出数据 --- app/Exports/ActivityLogExport.php | 113 ++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 app/Exports/ActivityLogExport.php diff --git a/app/Exports/ActivityLogExport.php b/app/Exports/ActivityLogExport.php new file mode 100644 index 0000000..faa16bb --- /dev/null +++ b/app/Exports/ActivityLogExport.php @@ -0,0 +1,113 @@ +query = $query; + } + + /** + * 查询数据 + */ + public function query() + { + return $this->query; + } + + /** + * 表头 + */ + public function headings(): array + { + return [ + '操作时间', + '操作用户', + '操作类型', + '对象类型', + '对象ID', + '日志名称', + '变更详情', + ]; + } + + /** + * 数据映射 + */ + public function map($activity): array + { + // 格式化操作类型 + $description = match ($activity->description) { + 'created' => '创建', + 'updated' => '更新', + 'deleted' => '删除', + default => $activity->description, + }; + + // 格式化对象类型 + $subjectType = '-'; + if ($activity->subject_type) { + $className = class_basename($activity->subject_type); + $subjectType = match ($className) { + 'SystemSetting' => '系统设置', + 'User' => '用户', + 'Document' => '文档', + 'Group' => '分组', + 'Terminal' => '终端', + 'SopTemplate' => 'SOP模板', + default => $className, + }; + } + + // 格式化变更详情 + $changes = ''; + if (is_array($activity->properties)) { + $changesArray = []; + if (isset($activity->properties['attributes'])) { + $changesArray[] = '新值: ' . json_encode($activity->properties['attributes'], JSON_UNESCAPED_UNICODE); + } + if (isset($activity->properties['old'])) { + $changesArray[] = '旧值: ' . json_encode($activity->properties['old'], JSON_UNESCAPED_UNICODE); + } + $changes = implode(' | ', $changesArray); + } + + return [ + $activity->created_at->format('Y-m-d H:i:s'), + $activity->causer?->name ?? '系统', + $description, + $subjectType, + $activity->subject_id ?? '-', + $activity->log_name ?? 'default', + $changes, + ]; + } + + /** + * 样式设置 + */ + public function styles(Worksheet $sheet) + { + return [ + // 表头样式 + 1 => [ + 'font' => ['bold' => true], + 'fill' => [ + 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID, + 'startColor' => ['rgb' => 'E2E8F0'], + ], + ], + ]; + } +}