| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Livewire;
- use Livewire\Component;
- use Livewire\WithFileUploads;
- use App\Models\Student;
- use App\Models\OCRRecord;
- use App\Services\OCRService;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- class UploadExamPaper extends Component
- {
- use WithFileUploads;
- public $selectedTeacherId = null;
- public $selectedStudentId = null;
- public $image = null;
- public $isUploading = false;
- public $uploadProgress = 0;
- public $uploadMessage = '';
- public $uploadError = '';
- public $uploadSuccess = false;
- public $teachers = [];
- public $students = [];
- protected $rules = [
- 'selectedTeacherId' => 'required|exists:teachers,teacher_id',
- 'selectedStudentId' => 'required|exists:students,student_id',
- 'image' => 'required|image|max:10240', // 10MB
- ];
- protected $messages = [
- 'selectedTeacherId.required' => '请选择老师',
- 'selectedTeacherId.exists' => '所选老师不存在',
- 'selectedStudentId.required' => '请选择学生',
- 'selectedStudentId.exists' => '所选学生不存在',
- 'image.required' => '请选择要上传的卷子照片',
- 'image.image' => '文件必须是图片格式',
- 'image.max' => '图片大小不能超过10MB',
- ];
- public function mount()
- {
- $this->loadTeachers();
- }
- public function loadTeachers()
- {
- $this->teachers = DB::table('teachers')
- ->join('users', 'teachers.user_id', '=', 'users.user_id')
- ->select('teachers.teacher_id', 'users.name')
- ->orderBy('users.name')
- ->get()
- ->map(fn ($teacher) => [
- 'id' => $teacher->teacher_id,
- 'name' => $teacher->name,
- ])
- ->toArray();
- }
- public function updatedSelectedTeacherId($value)
- {
- $this->selectedStudentId = null;
- if ($value) {
- $this->loadStudents($value);
- } else {
- $this->students = [];
- }
- }
- public function loadStudents($teacherId)
- {
- $this->students = DB::table('students')
- ->where('teacher_id', $teacherId)
- ->select('student_id', 'name', 'grade', 'class_name')
- ->orderBy('name')
- ->get()
- ->map(fn ($student) => [
- 'id' => $student->student_id,
- 'name' => $student->name . " ({$student->grade}-{$student->class_name})",
- ])
- ->toArray();
- }
- public function upload()
- {
- $this->validate();
- $this->isUploading = true;
- $this->uploadError = '';
- $this->uploadSuccess = false;
- $this->uploadProgress = 0;
- try {
- $ocrService = app(OCRService::class);
- $this->uploadProgress = 20;
- // 上传并创建OCR记录
- $ocrRecord = $ocrService->uploadExamPaper($this->image, $this->selectedStudentId);
- $this->uploadProgress = 80;
- $this->uploadProgress = 100;
- $this->uploadMessage = '上传成功!卷子照片已提交OCR识别,系统将自动处理。';
- $this->uploadSuccess = true;
- // 重置表单
- $this->reset(['image', 'selectedTeacherId', 'selectedStudentId']);
- $this->students = [];
- } catch (\Exception $e) {
- $this->uploadError = '上传失败:' . $e->getMessage();
- \Log::error('OCR上传失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- }
- $this->isUploading = false;
- }
- public function render()
- {
- return view('livewire.upload-exam-paper');
- }
- }
|