UploadForm.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Livewire\UploadExam;
  3. use Livewire\Component;
  4. use Livewire\WithFileUploads;
  5. use Livewire\Attributes\On;
  6. use Filament\Notifications\Notification;
  7. use App\Jobs\ProcessOCRRecord;
  8. use Illuminate\Support\Facades\Storage;
  9. class UploadForm extends Component
  10. {
  11. use WithFileUploads;
  12. public ?string $teacherId = null;
  13. public ?string $studentId = null;
  14. public ?string $selectedPaperId = null;
  15. public $uploadedImages = [];
  16. public bool $isUploading = false;
  17. public function mount($teacherId = null, $studentId = null, $selectedPaperId = null)
  18. {
  19. $this->teacherId = $teacherId;
  20. $this->studentId = $studentId;
  21. $this->selectedPaperId = $selectedPaperId;
  22. }
  23. public function handleSubmit()
  24. {
  25. // 验证图片
  26. if (empty($this->uploadedImages)) {
  27. Notification::make()
  28. ->title('请上传试卷图片')
  29. ->danger()
  30. ->send();
  31. return;
  32. }
  33. $this->isUploading = true;
  34. try {
  35. // 保存图片
  36. $savedImages = [];
  37. foreach ($this->uploadedImages as $image) {
  38. $path = $image->store('exam-papers', 'public');
  39. $savedImages[] = [
  40. 'path' => $path,
  41. 'original_name' => $image->getClientOriginalName(),
  42. 'size' => $image->getSize(),
  43. ];
  44. }
  45. // 获取试卷名称
  46. $paperTitle = '待OCR识别';
  47. if ($this->selectedPaperId) {
  48. $paper = \App\Models\Paper::where('paper_id', $this->selectedPaperId)->first();
  49. if ($paper) {
  50. $paperTitle = $paper->paper_name;
  51. }
  52. }
  53. // 创建 OCR 记录
  54. $ocrRecord = \App\Models\OCRRecord::create([
  55. 'user_id' => $this->studentId,
  56. 'student_id' => $this->studentId, // 同时设置 student_id
  57. 'paper_title' => $paperTitle,
  58. 'paper_type' => null,
  59. 'file_path' => $savedImages[0]['path'],
  60. 'image_count' => count($savedImages),
  61. 'status' => 'processing',
  62. 'analysis_id' => $this->selectedPaperId, // 存储关联的试卷ID
  63. ]);
  64. // 派发 OCR 处理任务
  65. ProcessOCRRecord::dispatch($ocrRecord->id);
  66. Notification::make()
  67. ->title('上传成功')
  68. ->body('图片已上传,正在进行 OCR 识别...')
  69. ->success()
  70. ->send();
  71. // 判断是否为系统生成的试卷
  72. if ($this->selectedPaperId && str_starts_with($this->selectedPaperId, 'paper_')) {
  73. // 系统生成的试卷,跳转到专门的分析页面
  74. $this->redirect('/admin/ocr-paper-analysis/' . $ocrRecord->id);
  75. } else {
  76. // 上传的试卷,跳转到原来的 OCR 详情页
  77. $this->redirect('/admin/ocr-record-view/' . $ocrRecord->id);
  78. }
  79. } catch (\Exception $e) {
  80. Notification::make()
  81. ->title('上传失败')
  82. ->body($e->getMessage())
  83. ->danger()
  84. ->send();
  85. } finally {
  86. $this->isUploading = false;
  87. }
  88. }
  89. public function resetForm()
  90. {
  91. $this->uploadedImages = [];
  92. $this->currentOcrRecordId = null;
  93. $this->ocrStatus = null;
  94. }
  95. public function removeImage($index)
  96. {
  97. unset($this->uploadedImages[$index]);
  98. $this->uploadedImages = array_values($this->uploadedImages);
  99. }
  100. public function render()
  101. {
  102. return view('livewire.upload-exam.upload-form');
  103. }
  104. }