UploadExamPaper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $this->teachers = DB::table('teachers')
  45. ->join('users', 'teachers.user_id', '=', 'users.user_id')
  46. ->select('teachers.teacher_id', 'users.name')
  47. ->orderBy('users.name')
  48. ->get()
  49. ->map(fn ($teacher) => [
  50. 'id' => $teacher->teacher_id,
  51. 'name' => $teacher->name,
  52. ])
  53. ->toArray();
  54. }
  55. public function updatedSelectedTeacherId($value)
  56. {
  57. $this->selectedStudentId = null;
  58. if ($value) {
  59. $this->loadStudents($value);
  60. } else {
  61. $this->students = [];
  62. }
  63. }
  64. public function loadStudents($teacherId)
  65. {
  66. $this->students = DB::table('students')
  67. ->where('teacher_id', $teacherId)
  68. ->select('student_id', 'name', 'grade', 'class_name')
  69. ->orderBy('name')
  70. ->get()
  71. ->map(fn ($student) => [
  72. 'id' => $student->student_id,
  73. 'name' => $student->name . " ({$student->grade}-{$student->class_name})",
  74. ])
  75. ->toArray();
  76. }
  77. public function upload()
  78. {
  79. $this->validate();
  80. $this->isUploading = true;
  81. $this->uploadError = '';
  82. $this->uploadSuccess = false;
  83. $this->uploadProgress = 0;
  84. try {
  85. $ocrService = app(OCRService::class);
  86. $this->uploadProgress = 20;
  87. // 上传并创建OCR记录
  88. $ocrRecord = $ocrService->uploadExamPaper($this->image, $this->selectedStudentId);
  89. $this->uploadProgress = 80;
  90. $this->uploadProgress = 100;
  91. $this->uploadMessage = '上传成功!卷子照片已提交OCR识别,系统将自动处理。';
  92. $this->uploadSuccess = true;
  93. // 重置表单
  94. $this->reset(['image', 'selectedTeacherId', 'selectedStudentId']);
  95. $this->students = [];
  96. } catch (\Exception $e) {
  97. $this->uploadError = '上传失败:' . $e->getMessage();
  98. \Log::error('OCR上传失败', [
  99. 'error' => $e->getMessage(),
  100. 'trace' => $e->getTraceAsString(),
  101. ]);
  102. }
  103. $this->isUploading = false;
  104. }
  105. public function render()
  106. {
  107. return view('livewire.upload-exam-paper');
  108. }
  109. }