| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace App\Livewire\UploadExam;
- use Livewire\Component;
- use Livewire\WithFileUploads;
- use Livewire\Attributes\On;
- use Filament\Notifications\Notification;
- use App\Jobs\ProcessOCRRecord;
- use Illuminate\Support\Facades\Storage;
- class UploadForm extends Component
- {
- use WithFileUploads;
- public ?string $teacherId = null;
- public ?string $studentId = null;
- public $uploadedImages = [];
- public bool $isUploading = false;
- public ?int $currentOcrRecordId = null;
- public ?string $ocrStatus = null;
- protected $listeners = [
- 'uploadComplete' => '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');
- }
- }
|