UploadExamPaper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithFileUploads;
  5. use App\Models\Student;
  6. use App\Models\OCRRecord;
  7. use App\Services\OCRService;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Str;
  11. class UploadExamPaper extends Component
  12. {
  13. use WithFileUploads;
  14. public $selectedTeacherId = null;
  15. public $selectedStudentId = null;
  16. public $image = null;
  17. public $isUploading = false;
  18. public $uploadProgress = 0;
  19. public $uploadMessage = '';
  20. public $uploadError = '';
  21. public $uploadSuccess = false;
  22. public $teachers = [];
  23. public $students = [];
  24. protected $rules = [
  25. 'selectedTeacherId' => 'required|exists:teachers,teacher_id',
  26. 'selectedStudentId' => 'required|exists:students,student_id',
  27. 'image' => 'required|image|max:10240', // 10MB
  28. ];
  29. protected $messages = [
  30. 'selectedTeacherId.required' => '请选择老师',
  31. 'selectedTeacherId.exists' => '所选老师不存在',
  32. 'selectedStudentId.required' => '请选择学生',
  33. 'selectedStudentId.exists' => '所选学生不存在',
  34. 'image.required' => '请选择要上传的卷子照片',
  35. 'image.image' => '文件必须是图片格式',
  36. 'image.max' => '图片大小不能超过10MB',
  37. ];
  38. public function mount()
  39. {
  40. $this->loadTeachers();
  41. }
  42. public function loadTeachers()
  43. {
  44. // Load teachers using the Teacher model (assumes a Teacher model exists with a relation to User)
  45. $this->teachers = \App\Models\Teacher::with('user')
  46. ->orderBy('users.name')
  47. ->get()
  48. ->map(function ($teacher) {
  49. return [
  50. 'id' => $teacher->id,
  51. 'teacher_id' => $teacher->id,
  52. 'name' => $teacher->user->name,
  53. ];
  54. })
  55. ->toArray();
  56. }
  57. public function updatedSelectedTeacherId($value)
  58. {
  59. $this->selectedStudentId = null;
  60. if ($value) {
  61. $this->loadStudents($value);
  62. } else {
  63. $this->students = [];
  64. }
  65. }
  66. public function loadStudents($teacherId)
  67. {
  68. $this->students = DB::table('students')
  69. ->where('teacher_id', $teacherId)
  70. ->select('student_id', 'name', 'grade', 'class_name')
  71. ->orderBy('name')
  72. ->get()
  73. ->map(fn ($student) => [
  74. 'id' => $student->student_id,
  75. 'name' => $student->name . " ({$student->grade}-{$student->class_name})",
  76. ])
  77. ->toArray();
  78. }
  79. public function upload()
  80. {
  81. $this->validate();
  82. $this->isUploading = true;
  83. $this->uploadError = '';
  84. $this->uploadSuccess = false;
  85. $this->uploadProgress = 0;
  86. try {
  87. $ocrService = app(OCRService::class);
  88. $this->uploadProgress = 20;
  89. // 上传并创建OCR记录
  90. $ocrRecord = $ocrService->uploadExamPaper($this->image, $this->selectedStudentId);
  91. $this->uploadProgress = 80;
  92. $this->uploadProgress = 100;
  93. $this->uploadMessage = '上传成功!卷子照片已提交OCR识别,系统将自动处理。';
  94. $this->uploadSuccess = true;
  95. // 重置表单
  96. $this->reset(['image', 'selectedTeacherId', 'selectedStudentId']);
  97. $this->students = [];
  98. } catch (\Exception $e) {
  99. $this->uploadError = '上传失败:' . $e->getMessage();
  100. \Log::error('OCR上传失败', [
  101. 'error' => $e->getMessage(),
  102. 'trace' => $e->getTraceAsString(),
  103. ]);
  104. }
  105. $this->isUploading = false;
  106. }
  107. public function render()
  108. {
  109. return view('livewire.upload-exam-paper');
  110. }
  111. }