refactor: 修复知识库和操作指引

This commit is contained in:
2026-03-13 14:32:37 +08:00
parent bbe8e60646
commit 58f42de9df
88 changed files with 3387 additions and 2472 deletions

View File

@@ -1,50 +0,0 @@
<?php
namespace App\Filament\Actions;
use App\Models\SopTemplate;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
class ArchiveSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'archive';
}
protected function setUp(): void
{
parent::setUp();
$this->label('归档模板');
$this->icon('heroicon-o-archive-box');
$this->color('warning');
$this->requiresConfirmation();
$this->modalHeading('归档 SOP 模板');
$this->modalDescription('归档后,模板将不再对用户显示。确定要归档吗?');
$this->modalSubmitActionLabel('确认归档');
$this->visible(function ($record) {
return $record instanceof SopTemplate && $record->status === 'published';
});
$this->action(function (SopTemplate $record) {
$record->update([
'status' => 'archived',
]);
Notification::make()
->title('归档成功')
->body('SOP 模板已成功归档')
->success()
->send();
});
}
}

View File

@@ -1,51 +0,0 @@
<?php
namespace App\Filament\Actions;
use App\Models\SopTemplate;
use App\Services\SopTemplateService;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Response;
class ExportSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'export';
}
protected function setUp(): void
{
parent::setUp();
$this->label('导出模板');
$this->icon('heroicon-o-arrow-down-tray');
$this->color('info');
$this->action(function (SopTemplate $record) {
$service = app(SopTemplateService::class);
$json = $service->exportToJson($record);
$fileName = sprintf(
'sop_template_%s_%s.json',
$record->id,
now()->format('YmdHis')
);
Notification::make()
->title('导出成功')
->body('SOP 模板已成功导出')
->success()
->send();
return Response::streamDownload(function () use ($json) {
echo $json;
}, $fileName, [
'Content-Type' => 'application/json',
]);
});
}
}

View File

@@ -1,80 +0,0 @@
<?php
namespace App\Filament\Actions;
use App\Services\SopTemplateService;
use Filament\Actions\Action;
use Filament\Forms;
use Filament\Notifications\Notification;
use Illuminate\Validation\ValidationException;
class ImportSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'import';
}
protected function setUp(): void
{
parent::setUp();
$this->label('导入模板');
$this->icon('heroicon-o-arrow-up-tray');
$this->color('success');
$this->modalHeading('导入 SOP 模板');
$this->modalDescription('从 JSON 文件导入 SOP 模板');
$this->modalSubmitActionLabel('导入');
$this->form([
Forms\Components\FileUpload::make('file')
->label('选择文件')
->acceptedFileTypes(['application/json'])
->required()
->maxSize(5120), // 5MB
]);
$this->action(function (array $data) {
try {
$filePath = storage_path('app/public/' . $data['file']);
$json = file_get_contents($filePath);
$service = app(SopTemplateService::class);
$template = $service->importFromJson($json);
// 删除临时文件
@unlink($filePath);
Notification::make()
->title('导入成功')
->body("SOP 模板「{$template->name}」已成功导入")
->success()
->send();
// 重定向到编辑页面
return redirect()->route('filament.admin.resources.sop-templates.edit', ['record' => $template]);
} catch (ValidationException $e) {
Notification::make()
->title('导入失败')
->body($e->getMessage())
->danger()
->send();
throw $e;
} catch (\Exception $e) {
Notification::make()
->title('导入失败')
->body('文件格式错误或数据无效')
->danger()
->send();
throw $e;
}
});
}
}

View File

@@ -1,90 +0,0 @@
<?php
namespace App\Filament\Actions;
use App\Models\SopTemplate;
use Filament\Actions\Action;
use Filament\Infolists;
use Filament\Infolists\Infolist;
class PreviewSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'preview';
}
protected function setUp(): void
{
parent::setUp();
$this->label('预览模板');
$this->icon('heroicon-o-eye');
$this->color('info');
$this->modalHeading('SOP 模板预览');
$this->modalWidth('7xl');
$this->modalSubmitAction(false);
$this->modalCancelActionLabel('关闭');
$this->infolist(function (SopTemplate $record): Infolist {
return Infolist::make()
->record($record)
->schema([
Infolists\Components\Section::make('模板信息')
->schema([
Infolists\Components\TextEntry::make('name')
->label('模板名称')
->size(Infolists\Components\TextEntry\TextEntrySize::Large)
->weight('bold'),
Infolists\Components\TextEntry::make('description')
->label('模板描述')
->columnSpanFull(),
Infolists\Components\TextEntry::make('category')
->label('分类')
->badge(),
Infolists\Components\TextEntry::make('version')
->label('版本'),
])
->columns(2),
Infolists\Components\Section::make('操作步骤')
->schema([
Infolists\Components\RepeatableEntry::make('steps')
->label('')
->schema([
Infolists\Components\TextEntry::make('step_number')
->label('步骤')
->badge()
->color('primary'),
Infolists\Components\TextEntry::make('title')
->label('标题')
->weight('bold'),
Infolists\Components\TextEntry::make('content')
->label('内容')
->html()
->columnSpanFull(),
Infolists\Components\TextEntry::make('is_required')
->label('必需')
->badge()
->formatStateUsing(fn (bool $state): string => $state ? '是' : '否')
->color(fn (bool $state): string => $state ? 'success' : 'gray'),
])
->columns(3)
->contained(false),
]),
]);
});
}
}

View File

@@ -1,85 +0,0 @@
<?php
namespace App\Filament\Actions;
use App\Models\SopTemplate;
use App\Models\SopTemplateVersion;
use Filament\Actions\Action;
use Filament\Forms;
use Filament\Notifications\Notification;
class PublishSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'publish';
}
protected function setUp(): void
{
parent::setUp();
$this->label('发布模板');
$this->icon('heroicon-o-check-circle');
$this->color('success');
$this->requiresConfirmation();
$this->modalHeading('发布 SOP 模板');
$this->modalDescription('发布后,模板将对所有用户可见。确定要发布吗?');
$this->modalSubmitActionLabel('确认发布');
$this->form([
Forms\Components\Textarea::make('change_log')
->label('变更说明')
->placeholder('请描述本次发布的主要变更内容...')
->rows(3),
]);
$this->visible(function ($record) {
return $record instanceof SopTemplate && $record->status === 'draft';
});
$this->action(function (SopTemplate $record, array $data) {
// 验证模板是否有步骤
if ($record->steps()->count() === 0) {
Notification::make()
->title('发布失败')
->body('模板至少需要包含一个步骤才能发布')
->danger()
->send();
return;
}
// 创建版本快照
SopTemplateVersion::create([
'sop_template_id' => $record->id,
'version' => $record->version,
'change_log' => $data['change_log'] ?? '首次发布',
'content_snapshot' => [
'template' => $record->toArray(),
'steps' => $record->steps->toArray(),
],
'created_by' => auth()->id(),
'created_at' => now(),
]);
// 更新模板状态
$record->update([
'status' => 'published',
'published_at' => now(),
]);
Notification::make()
->title('发布成功')
->body('SOP 模板已成功发布')
->success()
->send();
});
}
}