fn_b/app/Http/Controllers/SitemapController.php
2026-09-12 16:32:37 +08:00

34 lines
1.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use App\Models\Articles;
use Illuminate\Http\Response;
class SitemapController extends Controller
{
public function index(): Response
{
// 连表查询分类:按 aOpt['noindexday'] 动态过滤过期文章
$cArticles = Articles::where('iStatus', 1)
->whereHas('oCategory', function ($q) {
$q->where(function ($query) {
// 1. 没有配置 noindexday,或者 noindexday = 0 的文章,一直保留
$query->whereNull('aOpt')
->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(aOpt, '$.noindexday')) IS NULL")
->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(aOpt, '$.noindexday')) = '0'")
// 2. 配置了天数的,只取 N 天内创建的新文章
->orWhereRaw("articles.created_at >= NOW() - INTERVAL CAST(JSON_UNQUOTE(JSON_EXTRACT(aOpt, '$.noindexday')) AS UNSIGNED) DAY");
});
})
->orderBy('created_at', 'desc')
->get();
$sXmlContent = view('sitemap', [
'cArticles' => $cArticles
])->render();
return response($sXmlContent, 200)->header('Content-Type', 'text/xml');
}
}