diff --git a/app/Filament/Actions/ExportSopTemplateAction.php b/app/Filament/Actions/ExportSopTemplateAction.php new file mode 100644 index 0000000..4584869 --- /dev/null +++ b/app/Filament/Actions/ExportSopTemplateAction.php @@ -0,0 +1,51 @@ +label('导出模板'); + + $this->icon('heroicon-o-arrow-down-tray'); + + $this->color('info'); + + $this->action(function (SopTemplate $record) { + $service = app(SopTemplateService::class); + $json = $service->exportToJson($record); + + $fileName = sprintf( + 'sop_template_%s_%s.json', + $record->id, + now()->format('YmdHis') + ); + + Notification::make() + ->title('导出成功') + ->body('SOP 模板已成功导出') + ->success() + ->send(); + + return Response::streamDownload(function () use ($json) { + echo $json; + }, $fileName, [ + 'Content-Type' => 'application/json', + ]); + }); + } +} diff --git a/app/Filament/Actions/ImportSopTemplateAction.php b/app/Filament/Actions/ImportSopTemplateAction.php new file mode 100644 index 0000000..141a228 --- /dev/null +++ b/app/Filament/Actions/ImportSopTemplateAction.php @@ -0,0 +1,80 @@ +label('导入模板'); + + $this->icon('heroicon-o-arrow-up-tray'); + + $this->color('success'); + + $this->modalHeading('导入 SOP 模板'); + + $this->modalDescription('从 JSON 文件导入 SOP 模板'); + + $this->modalSubmitActionLabel('导入'); + + $this->form([ + Forms\Components\FileUpload::make('file') + ->label('选择文件') + ->acceptedFileTypes(['application/json']) + ->required() + ->maxSize(5120), // 5MB + ]); + + $this->action(function (array $data) { + try { + $filePath = storage_path('app/public/' . $data['file']); + $json = file_get_contents($filePath); + + $service = app(SopTemplateService::class); + $template = $service->importFromJson($json); + + // 删除临时文件 + @unlink($filePath); + + Notification::make() + ->title('导入成功') + ->body("SOP 模板「{$template->name}」已成功导入") + ->success() + ->send(); + + // 重定向到编辑页面 + return redirect()->route('filament.admin.resources.sop-templates.edit', ['record' => $template]); + } catch (ValidationException $e) { + Notification::make() + ->title('导入失败') + ->body($e->getMessage()) + ->danger() + ->send(); + + throw $e; + } catch (\Exception $e) { + Notification::make() + ->title('导入失败') + ->body('文件格式错误或数据无效') + ->danger() + ->send(); + + throw $e; + } + }); + } +}