安全(P0) - 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、 ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签 (ywxapp.framework_package_verify,默认开启) - JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret() 校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET - /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验 - 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流 - TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive - 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php) - 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis - paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG 工程化 / 交付 - 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本 - 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过 - 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障 - .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess 结构 - appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
273 lines
8.8 KiB
PHP
273 lines
8.8 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Author: ywxapp<admin@ywxapp.cn>
|
||
// +----------------------------------------------------------------------
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace addon\blog\controller;
|
||
|
||
use addon\blog\model\BlogArticle as ArticleModel;
|
||
use addon\blog\model\BlogCategory as CategoryModel;
|
||
use addon\blog\model\BlogComment as CommentModel;
|
||
use addon\blog\model\BlogTag as TagModel;
|
||
use think\facade\Cache;
|
||
use think\facade\View;
|
||
use think\facade\Request;
|
||
|
||
|
||
/**
|
||
* Index 类
|
||
*
|
||
* @author ywxapp <admin@ywxapp.cn>
|
||
*/
|
||
class Index extends BlogFrontend
|
||
{
|
||
|
||
public function initialize() {}
|
||
|
||
|
||
/**
|
||
* Summary of needLogin
|
||
* @var array
|
||
*/
|
||
protected $noNeedLogin = ['*'];
|
||
/**
|
||
* 网站首页
|
||
*/
|
||
public function index()
|
||
{
|
||
// 尝试从缓存获取首页数据(v2 键:绕过旧版本可能残留的损坏缓存)
|
||
$cacheKey = 'homepage_data_v2';
|
||
$data = Cache::get($cacheKey);
|
||
// 判断缓存中的文章是否为空(兼容数组与模型集合对象)
|
||
$articlesEmpty = true;
|
||
if (! empty($data) && ! empty($data['articles'])) {
|
||
$cachedArticles = $data['articles'];
|
||
$articlesEmpty = (is_object($cachedArticles) && method_exists($cachedArticles, 'isEmpty'))
|
||
? $cachedArticles->isEmpty()
|
||
: (count($cachedArticles) === 0);
|
||
}
|
||
if (empty($data) || $articlesEmpty) {
|
||
// 获取最新文章
|
||
// 必须同时为「审核通过」,否则待审/驳回文章会被后台批量发布推到前台
|
||
// 列表排除正文(longtext),字段由 listFields() 动态生成(避开 bind + field 的坑)
|
||
$articles = ArticleModel::with(['user', 'category', 'tags'])
|
||
->field(ArticleModel::listFields())
|
||
->where('status', 1)
|
||
->where('audit_status', 2)
|
||
->order('create_at', 'desc')
|
||
->limit(10)
|
||
->select();
|
||
|
||
// 获取热门文章
|
||
$hotArticles = ArticleModel::with(['user', 'category'])
|
||
->where('status', 1)
|
||
->order('view_count', 'desc')
|
||
->limit(5)
|
||
->select();
|
||
|
||
// 获取推荐文章(置顶+高赞)
|
||
$recommendedArticles = ArticleModel::with(['user', 'category'])
|
||
->where('status', 1)
|
||
->where('is_top', 1)
|
||
->order('like_count', 'desc')
|
||
->limit(5)
|
||
->select();
|
||
|
||
// 获取分类(公开站点展示全部分类)
|
||
$categories = CategoryModel::withCount(['articles' =>
|
||
function ($query, &$alias) {
|
||
$query->where('status', 1);
|
||
$alias = 'card_count';
|
||
}])
|
||
->order('sort', 'asc')
|
||
->select();
|
||
|
||
// 获取标签
|
||
$tags = TagModel::withCount('articles')
|
||
->order('create_at', 'desc')
|
||
->limit(20)
|
||
->select();
|
||
|
||
// 获取最新评论
|
||
$latestComments = CommentModel::with(['user', 'article'])
|
||
->where('status', 1)
|
||
->order('create_at', 'desc')
|
||
->limit(5)
|
||
->select();
|
||
$data = [
|
||
'articles' => $articles,
|
||
'hotArticles' => $hotArticles,
|
||
'recommendedArticles' => $recommendedArticles,
|
||
'categories' => $categories,
|
||
'tags' => $tags,
|
||
'latestComments' => $latestComments,
|
||
];
|
||
// 仅在存在文章时缓存,避免空结果被长期缓存挡住新数据
|
||
if (! empty($articles) && ! $articles->isEmpty()) {
|
||
Cache::set($cacheKey, $data, 3600);
|
||
}
|
||
}
|
||
$this->view->assign('articles', $data['articles']);
|
||
$this->view->assign('hotArticles', $data['hotArticles']);
|
||
$this->view->assign('recommendedArticles', $data['recommendedArticles']);
|
||
$this->view->assign('categories', $data['categories']);
|
||
$this->view->assign('tags', $data['tags']);
|
||
$this->view->assign('latestComments', $data['latestComments']);
|
||
return $this->view->fetch('index/index');
|
||
}
|
||
|
||
/**
|
||
* 分类文章列表(无 $id 时展示全部分类)
|
||
*/
|
||
public function category($id = null)
|
||
{
|
||
if ($id === null) {
|
||
$categories = CategoryModel::withCount(['articles' =>
|
||
function ($query, &$alias) {
|
||
$query->where('status', 1);
|
||
$alias = 'card_count';
|
||
}])->order('sort', 'asc')->select();
|
||
return View::fetch('category/index', [
|
||
'category' => null,
|
||
'categories' => $categories,
|
||
]);
|
||
}
|
||
|
||
$category = CategoryModel::find($id);
|
||
if (! $category) {
|
||
// 软 404 会被搜索引擎收录:显式返回 HTTP 404
|
||
return $this->result->setStatusCode(404)->error('分类不存在', 404);
|
||
}
|
||
|
||
$page = Request::param('page', 1);
|
||
$pageSize = 10;
|
||
|
||
$articles = ArticleModel::with(['user', 'tags'])
|
||
->field(ArticleModel::listFields())
|
||
->where('cid', $id)
|
||
->where('status', 1)
|
||
->where('audit_status', 2)
|
||
->order('create_at', 'desc')
|
||
->paginate([
|
||
'list_rows' => $pageSize,
|
||
'page' => $page,
|
||
]);
|
||
|
||
return View::fetch('category/index', [
|
||
'category' => $category,
|
||
'articles' => $articles,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 标签文章列表(无 $id 时展示全部标签)
|
||
*/
|
||
public function tag($id = null)
|
||
{
|
||
if ($id === null) {
|
||
$tags = TagModel::withCount('articles')->order('create_at', 'desc')->select();
|
||
return View::fetch('tag/index', [
|
||
'tag' => null,
|
||
'tags' => $tags,
|
||
]);
|
||
}
|
||
|
||
$tag = TagModel::find($id);
|
||
if (! $tag) {
|
||
// 软 404 会被搜索引擎收录:显式返回 HTTP 404
|
||
return $this->result->setStatusCode(404)->error('标签不存在', 404);
|
||
}
|
||
|
||
$page = Request::param('page', 1);
|
||
$pageSize = 10;
|
||
|
||
$articles = $tag->articles()
|
||
->with(['user', 'category'])
|
||
->where('status', 1)
|
||
->where('audit_status', 2)
|
||
->order('create_at', 'desc')
|
||
->paginate([
|
||
'list_rows' => $pageSize,
|
||
'page' => $page,
|
||
]);
|
||
|
||
return View::fetch('tag/index', [
|
||
'tag' => $tag,
|
||
'articles' => $articles,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 搜索页面
|
||
*/
|
||
public function search()
|
||
{
|
||
$keyword = Request::param('keyword', '');
|
||
if (empty($keyword)) {
|
||
return $this->result->error('请输入搜索关键词');
|
||
}
|
||
|
||
$page = Request::param('page', 1);
|
||
$pageSize = 10;
|
||
|
||
$articles = \addon\blog\service\BlogSearchService::search($keyword, (int) $page, $pageSize);
|
||
|
||
return View::fetch('index/search', [
|
||
'keyword' => $keyword,
|
||
'articles' => $articles,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 关于页面
|
||
*/
|
||
public function about()
|
||
{
|
||
return View::fetch('index/about');
|
||
}
|
||
|
||
/**
|
||
* 联系页面
|
||
*/
|
||
public function contact()
|
||
{
|
||
return View::fetch('index/contact');
|
||
}
|
||
|
||
/**
|
||
* 附近文章(GEO/LBS)
|
||
*/
|
||
public function nearby()
|
||
{
|
||
$lat = (float) Request::param('lat', 0);
|
||
$lng = (float) Request::param('lng', 0);
|
||
$radius = (float) Request::param('radius', 10);
|
||
$list = \addon\blog\service\BlogSearchService::nearby($lat, $lng, $radius, 30);
|
||
$data = [];
|
||
foreach ($list as $a) {
|
||
$data[] = [
|
||
'id' => $a->id,
|
||
'title' => $a->title,
|
||
'distance' => isset($a->distance_km) ? round($a->distance_km, 2) : null,
|
||
'category' => $a->category ? $a->category->name : '',
|
||
'url' => url('blog/article/detail', ['id' => $a->id]),
|
||
];
|
||
}
|
||
if (Request::isAjax()) {
|
||
return json(['code' => 1, 'data' => $data]);
|
||
}
|
||
return View::fetch('index/nearby', [
|
||
'list' => $data,
|
||
'lat' => $lat,
|
||
'lng' => $lng,
|
||
'radius' => $radius,
|
||
]);
|
||
}
|
||
}
|