QuestionPreviewController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\ExamPdfExportService;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Storage;
  6. class QuestionPreviewController extends Controller
  7. {
  8. /**
  9. * 显示预览工具页面
  10. */
  11. public function index()
  12. {
  13. return view('tools.question-preview');
  14. }
  15. /**
  16. * 生成 PDF 预览
  17. */
  18. public function generatePdf(Request $request)
  19. {
  20. $request->validate([
  21. 'stem' => 'required|string',
  22. 'options' => 'nullable|array',
  23. 'answer' => 'nullable|string',
  24. 'solution' => 'nullable|string',
  25. ]);
  26. try {
  27. $service = app(ExamPdfExportService::class);
  28. // 过滤空选项
  29. $options = array_filter($request->input('options', []), fn($v) => !empty($v));
  30. $pdfPath = $service->generatePreviewPdf(
  31. stem: $request->input('stem'),
  32. options: $options,
  33. answer: $request->input('answer'),
  34. solution: $request->input('solution')
  35. );
  36. // 返回可访问的 URL
  37. $url = Storage::disk('public')->url($pdfPath);
  38. return response()->json([
  39. 'success' => true,
  40. 'url' => $url,
  41. ]);
  42. } catch (\Exception $e) {
  43. return response()->json([
  44. 'success' => false,
  45. 'error' => $e->getMessage(),
  46. ], 500);
  47. }
  48. }
  49. }