| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\QuestionServiceApi;
- use BackedEnum;
- use Filament\Actions;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use UnitEnum;
- use Livewire\Attributes\Computed;
- use Livewire\Attributes\On;
- class PromptManagement extends Page
- {
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-chat-bubble-left-right';
- protected static string|UnitEnum|null $navigationGroup = '管理';
- protected static ?string $navigationLabel = '提示词管理';
- protected static ?int $navigationSort = 12;
- protected ?string $heading = '提示词管理';
- protected string $view = 'filament.pages.prompt-management';
- public ?string $selectedType = null;
- public ?string $search = null;
- public int $currentPage = 1;
- public int $perPage = 10;
- public array $selectedPrompts = [];
- /**
- * 获取提示词列表
- */
- public function getPrompts(): array
- {
- $service = app(QuestionServiceApi::class);
- try {
- // 获取所有提示词
- $prompts = $service->listPrompts();
- // 筛选
- if ($this->selectedType) {
- $prompts = array_filter($prompts, fn($prompt) =>
- $prompt['template_type'] === $this->selectedType
- );
- }
- if ($this->search) {
- $searchTerm = strtolower($this->search);
- $prompts = array_filter($prompts, fn($prompt) =>
- str_contains(strtolower($prompt['template_name']), $searchTerm) ||
- str_contains(strtolower($prompt['description']), $searchTerm)
- );
- }
- // 分页
- $total = count($prompts);
- $offset = ($this->currentPage - 1) * $this->perPage;
- $paginated = array_slice($prompts, $offset, $this->perPage);
- return [
- 'data' => $paginated,
- 'meta' => [
- 'page' => $this->currentPage,
- 'per_page' => $this->perPage,
- 'total' => $total,
- 'total_pages' => (int) ceil($total / $this->perPage),
- ]
- ];
- } catch (\Exception $e) {
- \Log::error('Failed to fetch prompts: ' . $e->getMessage());
- return ['data' => [], 'meta' => ['total' => 0]];
- }
- }
- /**
- * 获取提示词类型统计
- */
- public function getTypeStats(): array
- {
- $service = app(QuestionServiceApi::class);
- try {
- $prompts = $service->listPrompts();
- $stats = [];
- foreach ($prompts as $prompt) {
- $type = $prompt['template_type'];
- if (!isset($stats[$type])) {
- $stats[$type] = 0;
- }
- $stats[$type]++;
- }
- return $stats;
- } catch (\Exception $e) {
- \Log::error('Failed to fetch prompt stats: ' . $e->getMessage());
- return [];
- }
- }
- /**
- * 获取所有类型选项
- */
- public function getTypeOptions(): array
- {
- return [
- '题目生成' => '题目生成',
- '掌握度评估' => '掌握度评估',
- '技能熟练度' => '技能熟练度',
- '质量审核' => '质量审核',
- ];
- }
- /**
- * 筛选更新
- */
- public function updatedSelectedType(): void
- {
- $this->currentPage = 1;
- }
- public function updatedSearch(): 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
- {
- $totalPages = $this->prompts['meta']['total_pages'] ?? 1;
- if ($this->currentPage < $totalPages) {
- $this->currentPage++;
- }
- }
- /**
- * 创建新提示词
- */
- #[On('create-prompt')]
- public function createPrompt(): void
- {
- Notification::make()
- ->title('创建提示词')
- ->body('打开创建表单')
- ->info()
- ->send();
- }
- /**
- * 编辑提示词
- */
- #[On('edit-prompt')]
- public function editPrompt(array $prompt): void
- {
- Notification::make()
- ->title('编辑提示词:' . $prompt['template_name'])
- ->body('打开编辑表单')
- ->info()
- ->send();
- }
- /**
- * 删除提示词
- */
- #[On('delete-prompt')]
- public function deletePrompt(string $promptName): void
- {
- Notification::make()
- ->title('删除提示词:' . $promptName)
- ->body('此操作不可恢复')
- ->danger()
- ->send();
- }
- /**
- * 启用/禁用提示词
- */
- #[On('toggle-prompt')]
- public function togglePrompt(string $promptName, bool $isActive): void
- {
- $status = $isActive ? '禁用' : '启用';
- Notification::make()
- ->title($status . '提示词:' . $promptName)
- ->success()
- ->send();
- }
- /**
- * 复制提示词
- */
- #[On('duplicate-prompt')]
- public function duplicatePrompt(array $prompt): void
- {
- Notification::make()
- ->title('复制提示词:' . $prompt['template_name'])
- ->success()
- ->send();
- }
- /**
- * 刷新数据
- */
- #[On('refresh-prompts')]
- public function refreshPrompts(): void
- {
- Notification::make()
- ->title('数据已刷新')
- ->success()
- ->send();
- }
- /**
- * 头部操作
- */
- protected function getHeaderActions(): array
- {
- return [
- Actions\Action::make('create')
- ->label('新建提示词')
- ->icon('heroicon-m-plus')
- ->color('success')
- ->action('createPrompt'),
- Actions\Action::make('refresh')
- ->label('刷新')
- ->icon('heroicon-m-arrow-path')
- ->color('warning')
- ->action('refreshPrompts'),
- ];
- }
- }
|