34 lines
1.3 KiB
PHP
34 lines
1.3 KiB
PHP
<?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');
|
|
}
|
|
}
|