cfn/app/Http/Controllers/Article/ListController.php
2026-01-04 20:59:04 +08:00

150 lines
4.1 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Article;
use Illuminate\Http\Request;
use App\Models\ArticleList;
use App\Models\Tags;
use App\Services\Time;
use voku\helper\AntiXSS;
use Illuminate\Routing\Controller;
class ListController extends Controller
{
public function __construct()
{
$sSearch = request('search');
if ($sSearch) {
$this->middleware('throttle:3,1')->only('mix');
} else {
// $this->middleware('throttle:15,1');
}
}
public function client($sType = false)
{
$r = $this->view('client', $sType);
return $r;
}
public function fn()
{
$r = $this->view('freenode');
$iPageNo = (int) request('pageNo', 1);
if ($iPageNo == 1) {
file_put_contents('index.html', $r);
}
return $r;
}
public function mix()
{
$r = $this->view('mix');
return $r;
}
public function view($sType = false, $sTypeSub = false)
{
$oAntiXss = new AntiXSS();
$sType = $oAntiXss->xss_clean($sType);
$sTypeSub = $oAntiXss->xss_clean($sTypeSub);
$iPageNo = $oAntiXss->xss_clean(request('pageNo'));
$bIsAjax = $oAntiXss->xss_clean(request('isAjax'));
$iTagId = $oAntiXss->xss_clean(request('tag'));
$sSearch = $oAntiXss->xss_clean(request('search'));
$iPageNo = $iPageNo ? $iPageNo : 1;
$bIsAjax = $bIsAjax ? $bIsAjax : false;
$iTagId = $iTagId ? $iTagId : 0;
$iLimit = 10;
$iStart = $iLimit * $iPageNo - $iLimit;
$oQuery = ArticleList::query();
$sMenu = "main";
if (!$sSearch && !$iTagId) {
if ($sType == 'client') {
$sMenu = "client";
if ($sTypeSub) {
$oQuery->where("type", $sTypeSub);
} else {
$oQuery->whereIn("type", ['ios','mac','win','linux','android']);
}
} else if ($sType == "freenode") {
$sMenu = "freenode";
$oQuery->where("type", "=", "node");
}
}
if ($iTagId) {
$oQuery->where('tags', 'like', '%['.$iTagId.']%');
}
if ($sSearch) {
$oQuery->where('title', 'like', '%'.$sSearch.'%');
}
// dd($oQuery->toSql(), $oQuery->getBindings());
$aArticleList = $oQuery->orderBy('code', 'desc')->skip($iStart)->take($iLimit)->get()->toArray();
foreach ($aArticleList as &$row) {
$sTimeLost = Time::Lost(time(), strtotime($row['updated_at']));
$row['time_lost'] = $sTimeLost;
$row['updated_at'] = date('n月j日', strtotime($row['updated_at']));
if ($row['type'] == 'node') {
$row['typeStr'] = '免费节点';
$row['pathBase'] = '/fn/';
$row['title'] = '「'.$row['date_str'].'」'.$row['title'];
} else if (in_array($row['type'], ['ios','mac','win','linux','android'])) {
$row['typeStr'] = $row['type'].'客户端';
$row['pathBase'] = '/client/';
}
}
$aTagMap = Tags::all()->toArray();
if ($bIsAjax) {
echo json_encode($aArticleList);exit;
} else {
// 第二页之后是ajax
if ($iPageNo > 1) {
$sViewPath = "article/listAjax";
} else {
$sViewPath = 'article/list';
}
$sHtml = view($sViewPath, [
'sAppName' => env('APP_NAME'),
'aList' => $aArticleList,
'aTag' => @$aTagMap[$iTagId-1],
'search' => $sSearch,
'sMenu' => $sMenu,
'sType' => $sType,
'sTypeSub' => $sTypeSub,
// 'sSiteCode' => env('site_code'),
])->render();
return $sHtml;
}
}
}