Files

72 lines
2.9 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
// +----------------------------------------------------------------------
// | 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 ywxapp\controller\FrontendBase;
use addon\blog\model\BlogArticle;
use addon\blog\model\BlogCategory;
use addon\blog\model\BlogTag;
use addon\blog\model\BlogComment;
/**
* 博客前台基类:统一注入侧边栏/统计等共享数据
*/
class BlogFrontend extends FrontendBase
{
// 博客前台为公开页面,不做强制登录拦截
protected $noNeedLogin = ['*'];
protected $noNeedVerify = ['*'];
public function _initialize()
{
parent::_initialize();
$articleModel = new BlogArticle();
$categoryModel = new BlogCategory();
$tagModel = new BlogTag();
$commentModel = new BlogComment();
// 主动解析登录态:verifyAuth 因 noNeedLogin='*' 会跳过 tokenParse
// 必须显式 checkLogin() 从 session(access_token) 还原 isLogin。
$isLogin = $this->auth && $this->auth->isLogin;
$currentUid = 0;
$loginUser = null;
if ($isLogin) {
$u = $this->auth->info;
$loginUser = $u;
$currentUid = is_array($u) ? (int) ($u['uid'] ?? 0) : (int) ($u->uid ?? 0);
}
$this->view->assign([
'total_articles' => $articleModel->where('status', 1)->count(),
'total_categories' => $categoryModel->count(),
'total_views' => (int) $articleModel->where('status', 1)->sum('view_count'),
'hotArticles' => $articleModel->with(['user', 'category'])
->where('status', 1)->order('view_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(),
'nav_active' => '',
'is_login' => $isLogin,
'member' => $loginUser,
'current_uid' => $currentUid,
// 主题色:后台「基本设置」可配置,默认 #2d6cdf
'theme_color' => \think\facade\Config::get('site.theme_color', '#2d6cdf'),
]);
}
}