feat: AI 任务同步执行、评论垃圾管理、会员导航分组、多 AI 配置
Build and Deploy / verify (push) Successful in 10s
Build and Deploy / deploy (push) Successful in 20s

- 评论 AI 审核/文章润色改为同步执行(站点无 queue worker,任务曾永久卡队列,
  导致垃圾评论滞留待审核、润色毫无效果);审核在响应结束后经 terminating 触发
- 评论表:修复 approve 闭包里误嵌入 recordUrl 的坏链;新增「非垃圾」恢复发布、
  批量「标为垃圾」、工具栏「清空垃圾评论」;各操作补充通知反馈
- 会员套餐/订阅/订单移入新导航组「会员」
- LlmClient 支持多服务商 profile;AI 设置页可添加多个 AI 并为审核/润色/封面
  分别指定服务商(不指定用默认配置);封面生成同步支持 profile
- AI 设置页:润色改同步并即时展示结果,按钮加 loading;删除无 workerman 的
  WebSocket 进度区块
This commit is contained in:
2026-10-15 00:06:55 +00:00
parent ddbf259e93
commit 2e052498a5
14 changed files with 221 additions and 83 deletions
+2 -1
View File
@@ -8,7 +8,8 @@ namespace App\Blog\Jobs;
use App\Blog\Services\LlmClient;
/**
* 由 Workerman 消费者执行的 AI 任务统一接口。
* AI 任务统一接口。站点无常驻 queue worker,任务在响应结束后同步执行
* (comment.created 钩子经 app()->terminating 触发,后台操作直接 dispatchSync)。
*/
interface AiJob
{
+19 -5
View File
@@ -18,17 +18,29 @@ class CoverGenerator
public function isConfigured(): bool
{
return $this->llm->isConfigured();
return $this->llm()->isConfigured();
}
public function model(?string $override = null): string
{
return $override ?: (string) blog_setting('llm_image_model', 'image-01');
// 优先级:命令行参数 > llm_image_model 设置 > 图片 profile 的模型 > image-01
$profileModel = blog_setting('llm_profile_image') ? $this->llm()->model() : null;
return $override
?: (string) (blog_setting('llm_image_model') ?: $profileModel ?: 'image-01');
}
/**
* 封面生成使用的 LLM 配置(可在 AI 设置里指定独立的服务商)。
*/
private function llm(): LlmClient
{
return $this->llm->forProfile((string) blog_setting('llm_profile_image', ''));
}
public function generateFor(Post $post, ?string $model = null, ?string $style = null): void
{
if (! $this->llm->isConfigured()) {
if (! $this->llm()->isConfigured()) {
throw new \RuntimeException('LLM 未配置:请先在后台「AI 设置」配置 Base URL / API Key');
}
@@ -102,9 +114,11 @@ class CoverGenerator
private function requestImage(string $prompt, ?string $model = null): \Illuminate\Http\Client\Response
{
$llm = $this->llm();
return Http::timeout(120)
->withToken($this->llm->apiKey())
->post($this->llm->baseUrl().'/image_generation', [
->withToken($llm->apiKey())
->post($llm->baseUrl().'/image_generation', [
'model' => $this->model($model),
'prompt' => $prompt,
'aspect_ratio' => '16:9',
+44 -3
View File
@@ -10,22 +10,26 @@ use Illuminate\Support\Facades\Log;
/**
* OpenAI 兼容 LLM 客户端(支持 OpenAI / DeepSeek / 通义 / 自建代理)。
* 支持多服务商:后台「AI 设置」可配置 llm_profiles,用 forProfile('名称') 切换。
*/
class LlmClient
{
/** @var array{base_url?: string, api_key?: string, model?: string} */
private array $overrides = [];
public function baseUrl(): string
{
return rtrim((string) blog_setting('llm_base_url', config('services.llm.base_url', env('LLM_BASE_URL'))), '/');
return rtrim((string) ($this->overrides['base_url'] ?? blog_setting('llm_base_url', config('services.llm.base_url', env('LLM_BASE_URL')))), '/');
}
public function apiKey(): string
{
return (string) blog_setting('llm_api_key', config('services.llm.api_key', env('LLM_API_KEY')));
return (string) ($this->overrides['api_key'] ?? blog_setting('llm_api_key', config('services.llm.api_key', env('LLM_API_KEY'))));
}
public function model(): string
{
return (string) blog_setting('llm_model', config('services.llm.model', env('LLM_MODEL', 'gpt-4o-mini')));
return (string) ($this->overrides['model'] ?? blog_setting('llm_model', config('services.llm.model', env('LLM_MODEL', 'gpt-4o-mini'))));
}
public function isConfigured(): bool
@@ -33,6 +37,43 @@ class LlmClient
return $this->baseUrl() !== '' && $this->apiKey() !== '';
}
/**
* 已配置的额外 AI 服务商列表(后台 AI 设置维护)。
*
* @return list<array{name?: string, base_url?: string, api_key?: string, model?: string}>
*/
public static function profiles(): array
{
$profiles = json_decode((string) blog_setting('llm_profiles', '[]'), true);
return is_array($profiles) ? $profiles : [];
}
/**
* 切换到指定名称的服务商配置;名称为空或不存在时返回默认配置。
*/
public function forProfile(?string $name): static
{
if (! $name) {
return $this;
}
foreach (self::profiles() as $profile) {
if (($profile['name'] ?? '') === $name) {
$clone = clone $this;
$clone->overrides = array_filter([
'base_url' => $profile['base_url'] ?? null,
'api_key' => $profile['api_key'] ?? null,
'model' => $profile['model'] ?? null,
], fn ($value) => filled($value));
return $clone;
}
}
return $this;
}
/**
* 简单的聊天补全。
*
@@ -7,12 +7,15 @@ namespace App\Filament\Resources\Comments\Tables;
use App\Models\Comment;
use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Notifications\Notification;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Support\Collection;
class CommentsTable
{
@@ -74,21 +77,68 @@ class CommentsTable
->color('success')
->visible(fn (Comment $record) => $record->status !== 'published')
->action(function (Comment $record): void {
$record->update(['status' => 'published'])
->recordUrl(null)
->recordAction(null);
$record->update(['status' => 'published']);
$record->post?->increment('comment_count');
Notification::make()->title('评论已通过')->success()->send();
}),
Action::make('spam')
->label('垃圾')
->icon('heroicon-o-x-mark')
->color('danger')
->visible(fn (Comment $record) => $record->status !== 'spam')
->action(fn (Comment $record) => $record->update(['status' => 'spam'])),
->action(function (Comment $record): void {
if ($record->status === 'published') {
$record->post?->decrement('comment_count');
}
$record->update(['status' => 'spam']);
Notification::make()->title('已标记为垃圾评论')->success()->send();
}),
Action::make('notSpam')
->label('非垃圾')
->icon('heroicon-o-check-badge')
->color('success')
->requiresConfirmation()
->modalHeading('标记为非垃圾')
->modalDescription('该评论将恢复为已发布状态。')
->visible(fn (Comment $record) => $record->status === 'spam')
->action(function (Comment $record): void {
$record->update(['status' => 'published']);
$record->post?->increment('comment_count');
Notification::make()->title('已恢复发布')->success()->send();
}),
EditAction::make(),
])
->recordUrl(null)
->recordAction(null)
->toolbarActions([
Action::make('purgeSpam')
->label('清空垃圾评论')
->icon('heroicon-o-trash')
->color('danger')
->requiresConfirmation()
->modalHeading('清空垃圾评论')
->modalDescription(fn (): string => '将永久删除全部 '.Comment::query()->where('status', 'spam')->count().' 条垃圾评论,不可恢复。')
->visible(fn (): bool => Comment::query()->where('status', 'spam')->exists())
->action(function (): void {
$count = Comment::query()->where('status', 'spam')->count();
Comment::query()->where('status', 'spam')->delete();
Notification::make()->title("已删除 {$count} 条垃圾评论")->success()->send();
}),
BulkActionGroup::make([
BulkAction::make('markSpam')
->label('标为垃圾')
->icon('heroicon-o-x-mark')
->color('danger')
->requiresConfirmation()
->action(function (Collection $records): void {
$records->each(function (Comment $record): void {
if ($record->status === 'published') {
$record->post?->decrement('comment_count');
}
$record->update(['status' => 'spam']);
});
Notification::make()->title('已标记 '.$records->count().' 条为垃圾评论')->success()->send();
}),
DeleteBulkAction::make(),
]),
])
@@ -83,6 +83,7 @@ class AdminPanelProvider extends PanelProvider
)
->navigationGroups([
NavigationGroup::make('内容'),
NavigationGroup::make('会员'),
NavigationGroup::make('管理'),
NavigationGroup::make('插件'),
])