getKnowledgePointOptions(); } #[Computed(cache: false)] public function skillsOptions(): array { if (!$this->generateKpCode) { return []; } $service = app(KnowledgeGraphService::class); return $service->getSkillsByKnowledgePoint($this->generateKpCode); } #[Computed(cache: false)] public function questionTypeOptions(): array { return [ 'CHOICE' => '单选题', 'MULTIPLE_CHOICE' => '多选题', 'FILL_IN_THE_BLANK' => '填空题', 'CALCULATION' => '计算题', 'WORD_PROBLEM' => '应用题', 'PROOF' => '证明题', ]; } #[Computed(cache: false)] public function promptOptions(): array { $service = app(\App\Services\QuestionServiceApi::class); $prompts = $service->listPrompts(type: 'question_generation', active: 'yes'); // 只展示激活的题目生成模板 $options = []; foreach ($prompts as $prompt) { $label = $prompt['template_name']; if (!empty($prompt['description'])) { $label .= ' - ' . (is_string($prompt['description']) ? $prompt['description'] : json_encode($prompt['description'])); } $options[$prompt['template_name']] = $label; } // 自动选择第一个激活模板 if (!$this->promptTemplate && !empty($options)) { $this->promptTemplate = array_key_first($options); } return $options; } public function updatedGenerateKpCode(): void { // 选择新知识点时清空技能选择 $this->selectedSkills = []; } public function toggleAllSkills(): void { $skillsOptions = $this->skillsOptions; $skillsCount = count($skillsOptions); \Log::info('[ToggleAllSkills] Called', [ 'skillsCount' => $skillsCount, 'selectedSkillsCount' => count($this->selectedSkills), 'skillsOptions' => array_column($skillsOptions, 'code'), 'selectedSkills' => $this->selectedSkills ]); if ($skillsCount === 0) { \Log::info('[ToggleAllSkills] No skills available, returning'); return; } // 获取所有技能的编码列表 $allSkillCodes = array_values(array_unique(array_column($skillsOptions, 'code'))); // 检查当前选中的技能是否等于全部技能 if (count($this->selectedSkills) === $skillsCount && count(array_intersect($this->selectedSkills, $allSkillCodes)) === $skillsCount) { // 如果已全选,则清空 $this->selectedSkills = []; \Log::info('[ToggleAllSkills] Deselecting all skills'); } else { // 否则全选 $this->selectedSkills = $allSkillCodes; \Log::info('[ToggleAllSkills] Selecting all skills', ['newSelection' => $this->selectedSkills]); } } public function executeGenerate() { if ($this->isGenerating) { return; } if (!$this->generateKpCode) { Notification::make()->title('请选择知识点')->danger()->send(); return; } $skillsOptions = $this->skillsOptions; $skillsCount = count($skillsOptions); if ($skillsCount > 0 && empty($this->selectedSkills)) { Notification::make()->title('请选择至少一个技能')->danger()->send(); return; } $this->isGenerating = true; $this->currentTaskId = null; try { // 增加PHP脚本执行时间到120秒,给足够时间启动异步任务 set_time_limit(120); $service = app(QuestionBankService::class); $callbackUrl = route('api.questions.callback'); \Log::info("[QuestionGen] 开始生成,callback URL: " . $callbackUrl); $result = $service->generateIntelligentQuestions([ 'kp_code' => $this->generateKpCode, 'skills' => $this->selectedSkills, 'count' => $this->questionCount, 'difficulty' => $this->generateDifficulty, 'type' => $this->generateType, 'prompt_template' => $this->promptTemplate ?? null ], $callbackUrl); if ($result['success'] ?? false) { $this->currentTaskId = $result['task_id'] ?? null; \Log::info("[QuestionGen] ✅ 任务已创建: {$this->currentTaskId},准备跳转到题库管理"); \Log::info("[QuestionGen] 完整结果: " . json_encode($result)); // 准备跳转URL(不传递task_id参数) $redirectUrl = "/admin/question-management"; $taskId = $this->currentTaskId ?? 'unknown'; \Log::info("[QuestionGen] ✅ 准备跳转到: {$redirectUrl}"); \Log::info("[QuestionGen] ✅ 准备传递的taskId: {$taskId}"); // 使用简单可靠的Livewire dispatch事件 // 注意:不要设置 isGenerating = false,让状态栏继续显示 \Log::info("[QuestionGen] ✅ 即将分发跳转事件", [ 'url' => $redirectUrl, 'taskId' => $taskId ]); // 使用最简单的事件名称和参数 $this->dispatch('redirect-now', url: $redirectUrl, taskId: $taskId); \Log::info("[QuestionGen] ✅ 事件已分发"); // 使用Livewire的JavaScript方法执行直接跳转 $this->js("console.log('[QuestionGen] 直接JS跳转启动'); alert('✅ 任务已启动\\n任务 ID: {$taskId}\\n正在跳转到题库管理页面...'); setTimeout(function() { console.log('[QuestionGen] 直接跳转执行:', '{$redirectUrl}'); window.location.href = '{$redirectUrl}'; }, 1000);"); // 明确返回,避免执行后面的代码 \Log::info("[QuestionGen] ✅ 即将返回"); return; } else { $this->isGenerating = false; Notification::make() ->title('创建任务失败') ->body($result['message'] ?? '未知错误') ->danger() ->send(); } } catch (\Exception $e) { $this->isGenerating = false; \Log::error("[QuestionGen] 生成异常: " . $e->getMessage()); Notification::make() ->title('生成异常') ->body($e->getMessage()) ->danger() ->send(); } } public function forceCloseStatusBar(string $taskId): void { \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}"); $this->isGenerating = false; $this->currentTaskId = null; Notification::make() ->title('⚠️ 任务超时') ->body('生成任务可能已完成,请刷新页面查看') ->warning() ->persistent() ->send(); } }