| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\QuestionServiceApi;
- use App\Services\KnowledgeGraphService;
- use App\Services\QuestionBankService;
- use App\Livewire\Traits\WithMathRender;
- use BackedEnum;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use UnitEnum;
- use Livewire\Attributes\Computed;
- class QuestionManagement extends Page
- {
- use WithMathRender;
- protected static ?string $title = '题库管理';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-document-text';
- protected static ?string $navigationLabel = '题库管理';
- protected static string|UnitEnum|null $navigationGroup = '题库系统';
- protected static ?int $navigationSort = 2;
- protected string $view = 'filament.pages.question-management';
- public ?string $search = null;
- public ?string $selectedKpCode = null;
- public ?string $selectedDifficulty = null;
- public int $currentPage = 1;
- public int $perPage = 25;
- public ?string $generateKpCode = null;
- public array $selectedSkills = [];
- public int $questionCount = 100;
- public ?string $promptTemplate = null;
- public bool $showGenerateModal = false;
- public bool $showPromptModal = false;
- public ?string $currentTaskId = null;
- public int $currentTaskProgress = 0;
- public ?string $currentTaskMessage = null;
- public bool $isGenerating = false;
- #[Computed(cache: false)]
- public function questions(): array
- {
- $service = app(QuestionServiceApi::class);
- $filters = array_filter([
- 'kp_code' => $this->selectedKpCode,
- 'difficulty' => $this->selectedDifficulty,
- 'search' => $this->search,
- ], fn ($value) => filled($value));
- $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
- return $response['data'] ?? [];
- }
- #[Computed(cache: false)]
- public function meta(): array
- {
- $service = app(QuestionServiceApi::class);
- $filters = array_filter([
- 'kp_code' => $this->selectedKpCode,
- 'difficulty' => $this->selectedDifficulty,
- 'search' => $this->search,
- ], fn ($value) => filled($value));
- $response = $service->listQuestions($this->currentPage, $this->perPage, $filters);
- return $response['meta'] ?? ['page' => 1, 'per_page' => 25, 'total' => 0, 'total_pages' => 0];
- }
- #[Computed(cache: false)]
- public function statistics(): array
- {
- return app(QuestionServiceApi::class)->getStatistics();
- }
- #[Computed(cache: false)]
- public function knowledgePointOptions(): array
- {
- return app(QuestionServiceApi::class)->getKnowledgePointOptions();
- }
- #[Computed(cache: false)]
- public function skillsOptions(): array
- {
- if (!$this->generateKpCode) {
- return [];
- }
- $service = app(KnowledgeGraphService::class);
- return $service->getSkillsByKnowledgePoint($this->generateKpCode);
- }
- public function openGenerateModal(): void
- {
- $this->showGenerateModal = true;
- }
- public function closeGenerateModal(): void
- {
- $this->showGenerateModal = false;
- $this->reset(['generateKpCode', 'selectedSkills', 'questionCount']);
- $this->isGenerating = false;
- $this->currentTaskId = null;
- $this->currentTaskProgress = 0;
- $this->currentTaskMessage = null;
- }
- public function updatedGenerateKpCode(): void
- {
- // 选择新知识点时重置技能选择
- $this->selectedSkills = [];
- }
- public function toggleAllSkills(): void
- {
- $skills = $this->skillsOptions;
- if (count($this->selectedSkills) === count($skills)) {
- $this->selectedSkills = [];
- } else {
- $this->selectedSkills = array_column($skills, 'code');
- }
- }
- public function executeGenerate(): void
- {
- // 防止重复提交
- if ($this->isGenerating) {
- return;
- }
- // ✅ 立即关闭弹窗,无论验证结果如何
- $this->showGenerateModal = false;
- // 验证参数
- if (!$this->generateKpCode) {
- Notification::make()->title('请选择知识点')->danger()->send();
- return;
- }
- if (empty($this->selectedSkills)) {
- Notification::make()->title('请选择至少一个技能')->danger()->send();
- return;
- }
- // 设置异步生成状态
- $this->isGenerating = true;
- $this->currentTaskId = null;
- try {
- $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,
- 'prompt_template' => $this->promptTemplate ?? null
- ], $callbackUrl);
- if ($result['success'] ?? false) {
- $this->currentTaskId = $result['task_id'] ?? null;
- \Log::info("[QuestionGen] 任务已创建: {$this->currentTaskId},启动前端监控");
- Notification::make()
- ->title('正在生成题目')
- ->body("任务 ID: {$this->currentTaskId}\nAI正在后台生成,预计需要30-60秒...")
- ->info()
- ->persistent()
- ->send();
- // 开始异步轮询检查任务状态
- $this->dispatch('start-async-task-monitoring');
- } 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();
- }
- }
- // ✅ 已移除轮询机制 - 现在使用 Laravel 广播事件
- // 事件监听在前端 JavaScript 中直接处理,无需后端轮询
- public function forceCloseStatusBar(string $taskId): void
- {
- \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}");
- $this->isGenerating = false;
- $this->currentTaskId = null;
- Notification::make()
- ->title('⚠️ 任务超时')
- ->body('生成任务可能已完成,请刷新页面查看')
- ->warning()
- ->persistent()
- ->send();
- }
- public function deleteQuestion(string $questionCode): void
- {
- try {
- $service = app(QuestionBankService::class);
- $result = $service->deleteQuestion($questionCode);
- if ($result) {
- Notification::make()->title('删除成功')->body("题目 {$questionCode} 已删除")->success()->send();
- $this->dispatch('refresh-page');
- } else {
- Notification::make()->title('删除失败')->body("题目 {$questionCode} 不存在或已被删除")->warning()->send();
- }
- } catch (\Exception $e) {
- Notification::make()->title('删除异常')->body($e->getMessage())->danger()->send();
- }
- }
- public function updatedSearch(): void
- {
- $this->currentPage = 1;
- }
- public function updatedSelectedKpCode(): void
- {
- $this->currentPage = 1;
- }
- public function updatedSelectedDifficulty(): void
- {
- $this->currentPage = 1;
- }
- public function updatedPerPage(): void
- {
- $this->currentPage = 1;
- }
- public function gotoPage(int $page): void
- {
- $this->currentPage = $page;
- }
- public function previousPage(): void
- {
- if ($this->currentPage > 1) {
- $this->currentPage--;
- }
- }
- public function nextPage(): void
- {
- if ($this->currentPage < ($this->meta['total_pages'] ?? 1)) {
- $this->currentPage++;
- }
- }
- public function getPages(): array
- {
- $totalPages = $this->meta['total_pages'] ?? 1;
- $currentPage = $this->currentPage;
- $pages = [];
- $start = max(1, $currentPage - 2);
- $end = min($totalPages, $currentPage + 2);
- for ($i = $start; $i <= $end; $i++) {
- $pages[] = $i;
- }
- return $pages;
- }
- }
|