// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\articles\controller\backend; use addon\articles\library\ArticleSchema; use addon\articles\model\Article as ArticleModel; use addon\articles\model\ArticleCategory; use addon\articles\model\ArticleComment; use ywxapp\controller\BackendBase; /** * 文章CMS 后台仪表盘(统计概览) */ class Dashboard extends BackendBase { protected $noNeedVerify = []; protected function initialize() { parent::initialize(); ArticleSchema::ensure(); } public function index() { $today = strtotime('today'); $stats = [ 'total' => ArticleModel::count(), 'published' => ArticleModel::where('status', 1)->count(), 'draft' => ArticleModel::where('status', 0)->count(), 'pending' => ArticleModel::scope('pending')->count(), 'today' => ArticleModel::where('create_at', '>=', $today)->count(), 'views' => (int) ArticleModel::sum('view_count'), 'comments' => ArticleComment::count(), 'pending_comments' => ArticleComment::where('status', 0)->count(), 'trashed' => ArticleModel::onlyTrashed()->count(), ]; // 分类分布(Top 10) $dist = []; foreach (ArticleCategory::where('delete_at', 0)->order('sort', 'desc')->select() as $c) { $dist[] = [ 'id' => $c->id, 'name' => $c->name, 'count' => ArticleModel::where('cid', $c->id)->count(), ]; } usort($dist, function ($a, $b) { return $b['count'] <=> $a['count']; }); $dist = array_slice($dist, 0, 10); // 热门文章 Top 10 $hot = ArticleModel::where('status', 1) ->field('id,title,view_count,comment_count') ->order('view_count', 'desc') ->limit(10) ->select(); return $this->view->fetch('dashboard/index', [ 'stats' => $stats, 'dist' => $dist, 'hot' => $hot, ]); } }