init
This commit is contained in:
66
app/Http/Controllers/Site/NewsController.php
Normal file
66
app/Http/Controllers/Site/NewsController.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Site;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\AiModel;
|
||||
use App\Models\Article;
|
||||
use App\Models\Category;
|
||||
use App\Models\Guide;
|
||||
use App\Support\MarkdownRenderer;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class NewsController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MarkdownRenderer $markdownRenderer,
|
||||
) {
|
||||
}
|
||||
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$builder = Article::query()->published()->with('category');
|
||||
|
||||
if ($request->filled('q')) {
|
||||
$builder->whereFullText(['title', 'excerpt', 'body'], (string) $request->string('q'));
|
||||
}
|
||||
|
||||
if ($request->filled('category')) {
|
||||
$builder->whereHas('category', function ($query) use ($request): void {
|
||||
$query->where('slug', (string) $request->string('category'));
|
||||
});
|
||||
}
|
||||
|
||||
return view('public.news.index', [
|
||||
'items' => $builder->latest('published_at')->paginate(15)->withQueryString(),
|
||||
'categories' => Category::query()->where('type', 'news')->where('is_active', true)->orderBy('name')->get(),
|
||||
'filters' => $request->only(['q', 'category']),
|
||||
'sidebarModels' => AiModel::published()->orderByDesc('total_score')->limit(6)->get(),
|
||||
'sidebarGuides' => Guide::published()->latest('published_at')->limit(6)->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(string $slug): View
|
||||
{
|
||||
/** @var Article $article */
|
||||
$article = Article::query()
|
||||
->published()
|
||||
->with(['category', 'source'])
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
|
||||
return view('public.news.show', [
|
||||
'item' => $article,
|
||||
'bodyHtml' => $this->markdownRenderer->render($article->body),
|
||||
'showRiskNotice' => $this->containsRiskKeyword($article->title.' '.$article->body),
|
||||
]);
|
||||
}
|
||||
|
||||
private function containsRiskKeyword(string $text): bool
|
||||
{
|
||||
return str_contains($text, '医疗') || str_contains($text, '法律') || str_contains($text, '投资');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user