showWebPreview = true; $this->dispatch('render-math'); } /** * 触发 PDF 预览 */ public function previewPdf(): void { $this->pdfError = null; $this->pdfUrl = null; try { // 构建题目数据 $questionData = $this->buildQuestionData(); // 调用 PDF 生成服务 $pdfService = app(ExamPdfExportService::class); $result = $pdfService->generatePreviewPdf($questionData); if ($result && isset($result['url'])) { $this->pdfUrl = $result['url']; $this->showPdfPreview = true; } else { $this->pdfError = 'PDF 生成失败,请检查输入内容'; } } catch (\Exception $e) { Log::error('QuestionPreviewTool: PDF生成失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); $this->pdfError = 'PDF 生成出错: ' . $e->getMessage(); } } /** * 同时预览网页和 PDF */ public function previewBoth(): void { $this->previewWeb(); $this->previewPdf(); } /** * 清空表单 */ public function clearForm(): void { $this->stem = ''; $this->optionA = ''; $this->optionB = ''; $this->optionC = ''; $this->optionD = ''; $this->answer = ''; $this->solution = ''; $this->showWebPreview = false; $this->showPdfPreview = false; $this->pdfUrl = null; $this->pdfError = null; } /** * 构建题目数据结构 */ private function buildQuestionData(): array { $hasOptions = !empty($this->optionA) || !empty($this->optionB) || !empty($this->optionC) || !empty($this->optionD); $options = null; $questionType = 'fill'; // 默认填空题 if ($hasOptions) { $questionType = 'choice'; $options = []; if (!empty($this->optionA)) $options['A'] = $this->optionA; if (!empty($this->optionB)) $options['B'] = $this->optionB; if (!empty($this->optionC)) $options['C'] = $this->optionC; if (!empty($this->optionD)) $options['D'] = $this->optionD; } return [ 'stem' => $this->stem, 'options' => $options, 'answer' => $this->answer, 'solution' => $this->solution, 'question_type' => $questionType, ]; } /** * 获取用于网页预览的数据 */ public function getPreviewDataProperty(): array { return $this->buildQuestionData(); } public function render() { return view('livewire.question-preview-tool') ->layout('layouts.preview-tool'); } }