| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\KnowledgeGraphService;
- use Filament\Actions\Action;
- use Filament\Forms\Components\FileUpload;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Components\Textarea;
- use Filament\Forms\Components\TextInput;
- use Filament\Notifications\Notification;
- use Filament\Pages\Page;
- use Filament\Support\Enums\ActionSize;
- use Illuminate\Support\Facades\Storage;
- class KnowledgeGraphManagement extends Page
- {
- protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-rectangle-stack';
- protected static string|\UnitEnum|null $navigationGroup = '知识图谱系统';
- protected static ?int $navigationSort = 1;
- protected static ?string $navigationLabel = '知识图谱管理';
- protected static ?string $title = '知识图谱管理';
- protected string $view = 'filament.pages.knowledge-graph-management';
- public array $knowledgePoints = [];
- public function mount(KnowledgeGraphService $service): void
- {
- $this->knowledgePoints = $service->listKnowledgePoints(1, 1000);
- }
- public function edit(string $code): void
- {
- $this->mountAction('edit', ['code' => $code]);
- }
- public function delete(string $code): void
- {
- $this->mountAction('delete', ['code' => $code]);
- }
- protected function getHeaderActions(): array
- {
- return [
- Action::make('create')
- ->label('新增知识点')
- ->form([
- TextInput::make('cn_name')->label('中文名称')->required(),
- TextInput::make('en_name')->label('英文名称'),
- TextInput::make('kp_code')->label('知识点编码')->placeholder('自动生成或手动输入'),
- Select::make('phase')->label('学段')->options([
- '小学' => '小学', '初中' => '初中', '高中' => '高中'
- ])->required(),
- Select::make('grade')->label('年级')->options([
- 7 => '七年级', 8 => '八年级', 9 => '九年级'
- ]),
- TextInput::make('category')->label('分类')->default('数学'),
- TextInput::make('importance')->label('重要性')->numeric()->default(5),
- Textarea::make('description')->label('描述'),
- ])
- ->action(function (array $data, KnowledgeGraphService $service) {
- if ($service->createKnowledgePoint($data)) {
- Notification::make()->title('创建成功')->success()->send();
- $this->mount($service);
- } else {
- Notification::make()->title('创建失败')->danger()->send();
- }
- }),
- Action::make('import')
- ->label('导入图谱数据')
- ->color('gray')
- ->form([
- FileUpload::make('tree_file')
- ->label('Tree JSON (知识点结构)')
- ->required()
- ->storeFiles(false),
- FileUpload::make('edges_file')
- ->label('Edges JSON (依赖关系)')
- ->required()
- ->storeFiles(false),
- ])
- ->action(function (array $data, KnowledgeGraphService $service) {
- try {
- $treePath = $data['tree_file'];
- $edgesPath = $data['edges_file'];
-
- if (is_array($treePath)) $treePath = reset($treePath);
- if (is_array($edgesPath)) $edgesPath = reset($edgesPath);
- $treeContent = json_decode(Storage::disk('public')->get($treePath), true);
- $edgesContent = json_decode(Storage::disk('public')->get($edgesPath), true);
- if (!$treeContent || !$edgesContent) {
- Notification::make()->title('文件解析失败')->danger()->send();
- return;
- }
- if ($service->importGraph($treeContent, $edgesContent)) {
- Notification::make()->title('导入成功')->success()->send();
- $this->mount($service);
- } else {
- Notification::make()->title('导入失败')->danger()->send();
- }
- } catch (\Exception $e) {
- Notification::make()->title('导入异常')->body($e->getMessage())->danger()->send();
- }
- })
- ];
- }
- public function editAction(): Action
- {
- return Action::make('edit')
- ->label('编辑')
- ->modalHeading('编辑知识点')
- ->form([
- TextInput::make('cn_name')->label('中文名称')->required(),
- TextInput::make('en_name')->label('英文名称'),
- Select::make('phase')->label('学段')->options([
- '小学' => '小学', '初中' => '初中', '高中' => '高中'
- ])->required(),
- Select::make('grade')->label('年级')->options([
- 7 => '七年级', 8 => '八年级', 9 => '九年级'
- ]),
- TextInput::make('category')->label('分类'),
- TextInput::make('importance')->label('重要性')->numeric(),
- Textarea::make('description')->label('描述'),
- ])
- ->fillForm(function (array $arguments, KnowledgeGraphService $service) {
- $code = $arguments['code'];
- $data = $service->getKnowledgePoint($code);
- return $data ?? [];
- })
- ->action(function (array $data, array $arguments, KnowledgeGraphService $service) {
- if ($service->updateKnowledgePoint($arguments['code'], $data)) {
- Notification::make()->title('更新成功')->success()->send();
- $this->mount($service);
- } else {
- Notification::make()->title('更新失败')->danger()->send();
- }
- });
- }
- public function deleteAction(): Action
- {
- return Action::make('delete')
- ->label('删除')
- ->requiresConfirmation()
- ->action(function (array $arguments, KnowledgeGraphService $service) {
- if ($service->deleteKnowledgePoint($arguments['code'])) {
- Notification::make()->title('删除成功')->success()->send();
- $this->mount($service);
- } else {
- Notification::make()->title('删除失败')->danger()->send();
- }
- });
- }
- }
|