| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?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 ?string $selectedPaperId = null;
- public $uploadedImages = [];
- public bool $isUploading = false;
- public function mount($teacherId = null, $studentId = null, $selectedPaperId = null)
- {
- $this->teacherId = $teacherId;
- $this->studentId = $studentId;
- $this->selectedPaperId = $selectedPaperId;
- }
- 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(),
- ];
- }
- // 获取试卷名称
- $paperTitle = '待OCR识别';
- if ($this->selectedPaperId) {
- $paper = \App\Models\Paper::where('paper_id', $this->selectedPaperId)->first();
- if ($paper) {
- $paperTitle = $paper->paper_name;
- }
- }
- // 创建 OCR 记录
- $ocrRecord = \App\Models\OCRRecord::create([
- 'user_id' => $this->studentId,
- 'student_id' => $this->studentId, // 同时设置 student_id
- 'paper_title' => $paperTitle,
- 'paper_type' => null,
- 'file_path' => $savedImages[0]['path'],
- 'image_count' => count($savedImages),
- 'status' => 'processing',
- 'analysis_id' => $this->selectedPaperId, // 存储关联的试卷ID
- ]);
- // 派发 OCR 处理任务
- ProcessOCRRecord::dispatch($ocrRecord->id);
- Notification::make()
- ->title('上传成功')
- ->body('图片已上传,正在进行 OCR 识别...')
- ->success()
- ->send();
- // 判断是否为系统生成的试卷
- if ($this->selectedPaperId && str_starts_with($this->selectedPaperId, 'paper_')) {
- // 系统生成的试卷,跳转到专门的分析页面
- $this->redirect('/admin/ocr-paper-analysis/' . $ocrRecord->id);
- } else {
- // 上传的试卷,跳转到原来的 OCR 详情页
- $this->redirect('/admin/ocr-record-view/' . $ocrRecord->id);
- }
- } catch (\Exception $e) {
- Notification::make()
- ->title('上传失败')
- ->body($e->getMessage())
- ->danger()
- ->send();
- } 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 render()
- {
- return view('livewire.upload-exam.upload-form');
- }
- }
|