Files
ai-web/app/Http/Controllers/Site/GuideController.php
jiangdong.cheng a633234239 优化界面
2026-02-12 10:31:53 +08:00

87 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Site;
use App\Http\Controllers\Controller;
use App\Models\AiModel;
use App\Models\Guide;
use App\Models\Tool;
use App\Support\MarkdownRenderer;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
class GuideController extends Controller
{
public function __construct(
private readonly MarkdownRenderer $markdownRenderer,
) {
}
public function index(Request $request): View
{
$builder = Guide::query()->published()->with('category');
if ($request->filled('q')) {
$builder->whereFullText(['title', 'excerpt', 'body'], (string) $request->string('q'));
}
if ($request->filled('difficulty')) {
$builder->where('difficulty', (string) $request->string('difficulty'));
}
$difficultyCounts = Guide::query()
->published()
->selectRaw('difficulty, COUNT(*) as aggregate')
->groupBy('difficulty')
->pluck('aggregate', 'difficulty');
$difficultyOptions = [
['value' => 'beginner', 'label' => '入门', 'count' => (int) ($difficultyCounts['beginner'] ?? 0)],
['value' => 'intermediate', 'label' => '进阶', 'count' => (int) ($difficultyCounts['intermediate'] ?? 0)],
['value' => 'advanced', 'label' => '高级', 'count' => (int) ($difficultyCounts['advanced'] ?? 0)],
];
$guideStats = [
'total' => Guide::query()->published()->count(),
'beginner' => Guide::query()->published()->where('difficulty', 'beginner')->count(),
'advanced' => Guide::query()->published()->where('difficulty', 'advanced')->count(),
'updated_7d' => Guide::query()->published()->where('updated_at', '>=', now()->subDays(7))->count(),
];
return view('public.guides.index', [
'items' => $builder->latest('published_at')->paginate(15)->withQueryString(),
'difficultyOptions' => $difficultyOptions,
'guideStats' => $guideStats,
'filters' => $request->only(['q', 'difficulty']),
'sidebarTools' => Tool::published()->latest('published_at')->limit(6)->get(),
'sidebarModels' => AiModel::published()->orderByDesc('total_score')->limit(6)->get(),
]);
}
public function byTopic(string $slug, Request $request): View
{
$request->merge(['difficulty' => $slug]);
return $this->index($request);
}
public function show(string $slug): View
{
/** @var Guide $guide */
$guide = Guide::query()
->published()
->with('category')
->where('slug', $slug)
->firstOrFail();
return view('public.guides.show', [
'item' => $guide,
'bodyHtml' => $this->markdownRenderer->render($guide->body),
'sidebarTools' => Tool::published()->latest('published_at')->limit(6)->get(),
'sidebarModels' => AiModel::published()->orderByDesc('total_score')->limit(6)->get(),
]);
}
}