'handleUploadComplete', ]; public function mount($teacherId = null, $studentId = null) { $this->teacherId = $teacherId; $this->studentId = $studentId; } public function handleSubmit() { // 验证图片 if (empty($this->uploadedImages)) { Notification::make() ->title('请上传试卷图片') ->danger() ->send(); return; } $this->isUploading = true; try { // 保存图片 $savedImages = []; foreach ($this->uploadedImages as $image) { $path = $image->store('exam-papers', 'public'); $savedImages[] = [ 'path' => $path, 'original_name' => $image->getClientOriginalName(), 'size' => $image->getSize(), ]; } // 创建 OCR 记录(使用正确的字段名) $ocrRecord = \App\Models\OCRRecord::create([ 'user_id' => $this->studentId, 'paper_title' => '待OCR识别', 'paper_type' => null, // OCR识别 'file_path' => $savedImages[0]['path'], // 只存储第一张图片的路径 'image_count' => count($savedImages), 'status' => 'processing', ]); $this->currentOcrRecordId = $ocrRecord->id; $this->ocrStatus = 'processing'; // 派发 OCR 处理任务 ProcessOCRRecord::dispatch($ocrRecord->id); $this->dispatch('uploadComplete', [ 'ocrRecordId' => $ocrRecord->id, 'success' => true, 'message' => '图片上传成功,正在进行 OCR 识别...', ]); } catch (\Exception $e) { Notification::make() ->title('上传失败') ->body($e->getMessage()) ->danger() ->send(); $this->dispatch('uploadComplete', [ 'success' => false, 'message' => $e->getMessage(), ]); } finally { $this->isUploading = false; } } public function resetForm() { $this->uploadedImages = []; $this->currentOcrRecordId = null; $this->ocrStatus = null; } public function removeImage($index) { unset($this->uploadedImages[$index]); $this->uploadedImages = array_values($this->uploadedImages); } public function handleUploadComplete($data) { if ($data['success'] ?? false) { // 清空表单 $this->resetForm(); } } public function checkOcrStatus() { if (!$this->currentOcrRecordId) { return; } $ocrRecord = \App\Models\OCRRecord::find($this->currentOcrRecordId); if ($ocrRecord) { $this->ocrStatus = $ocrRecord->status; if ($ocrRecord->status === 'completed') { // OCR完成,跳转到详情页 $url = '/admin/exam-analysis?recordId=' . $ocrRecord->id . '&studentId=' . $this->studentId; $this->redirect($url); } elseif ($ocrRecord->status === 'failed') { Notification::make() ->title('OCR识别失败') ->body($ocrRecord->error_message ?? '未知错误') ->danger() ->send(); $this->currentOcrRecordId = null; } } } public function render() { return view('livewire.upload-exam.upload-form'); } }