tp/app/admin/controller/AnalyticsController.php
2026-01-26 17:52:06 +08:00

181 lines
6.5 KiB
PHP
Raw 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
declare(strict_types=1);
namespace app\admin\controller;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\response\Json;
use think\db\exception\DbException;
use app\model\Articles;
use app\model\ArticlesCategory;
use app\model\AdminUser;
class AnalyticsController extends BaseController
{
/**
* 内容统计
* 包含总发布量、本月新增、总点赞量、总访问量均与昨日对比、热门内容top5
* @return Json
*/
public function getContentStats()
{
try {
$today = date('Y-m-d');
$yesterday = date('Y-m-d', strtotime('-1 day'));
$monthStart = date('Y-m-01');
$yesterdayMonthStart = date('Y-m-01', strtotime('-1 day'));
$monthEnd = date('Y-m-t 23:59:59');
// 总发布量
$totalArticles = Articles::where('delete_time', null)
->where('status', 2)
->count();
// 昨日总发布量
$yesterdayTotalArticles = Articles::where('delete_time', null)
->where('status', 2)
->where('publish_date', '>=', $yesterday . ' 00:00:00')
->where('publish_date', '<=', $yesterday . ' 23:59:59')
->count();
// 本月新增
$monthNewArticles = Articles::where('delete_time', null)
->where('status', 2)
->where('publish_date', '>=', $monthStart . ' 00:00:00')
->where('publish_date', '<=', $monthEnd)
->count();
// 上月同期新增(用于对比)
$lastMonthStart = date('Y-m-01', strtotime('-1 month'));
$lastMonthEnd = date('Y-m-t 23:59:59', strtotime('-1 month'));
$lastMonthNewArticles = Articles::where('delete_time', null)
->where('status', 2)
->where('publish_date', '>=', $lastMonthStart . ' 00:00:00')
->where('publish_date', '<=', $lastMonthEnd)
->count();
// 总点赞量
$totalLikes = Articles::where('delete_time', null)
->where('status', 2)
->sum('likes');
// 昨日点赞量
$yesterdayLikes = Articles::where('delete_time', null)
->where('status', 2)
->where('update_time', '>=', $yesterday . ' 00:00:00')
->where('update_time', '<=', $yesterday . ' 23:59:59')
->sum('likes');
// 总访问量
$totalViews = Articles::where('delete_time', null)
->where('status', 2)
->sum('views');
// 昨日访问量
$yesterdayViews = Articles::where('delete_time', null)
->where('status', 2)
->where('update_time', '>=', $yesterday . ' 00:00:00')
->where('update_time', '<=', $yesterday . ' 23:59:59')
->sum('views');
// 计算增长率
$articleGrowth = $yesterdayTotalArticles > 0
? round(($totalArticles - $yesterdayTotalArticles) / $yesterdayTotalArticles * 100, 2)
: 0;
$monthGrowth = $lastMonthNewArticles > 0
? round(($monthNewArticles - $lastMonthNewArticles) / $lastMonthNewArticles * 100, 2)
: 0;
$likesGrowth = $yesterdayLikes > 0
? round(($totalLikes - $yesterdayLikes) / $yesterdayLikes * 100, 2)
: 0;
$viewsGrowth = $yesterdayViews > 0
? round(($totalViews - $yesterdayViews) / $yesterdayViews * 100, 2)
: 0;
// 热门内容TOP5
$hotArticles = Articles::where('delete_time', null)
->where('status', 2)
->order('views', 'desc')
->limit(5)
->withAttr('cate', function ($value) {
return ArticlesCategory::where('id', $value)->value('name') ?? '未分类';
})
->field('id,title,cate,views,likes,publish_date,status')
->select();
$stats = [
'overview' => [
'total_articles' => [
'value' => $totalArticles,
'yesterday' => $yesterdayTotalArticles,
'growth' => $articleGrowth
],
'month_new' => [
'value' => $monthNewArticles,
'last_month' => $lastMonthNewArticles,
'growth' => $monthGrowth
],
'total_likes' => [
'value' => (int)$totalLikes,
'yesterday' => (int)$yesterdayLikes,
'growth' => $likesGrowth
],
'total_views' => [
'value' => (int)$totalViews,
'yesterday' => (int)$yesterdayViews,
'growth' => $viewsGrowth
],
],
'hot_articles' => $hotArticles->toArray()
];
$this->logSuccess('内容统计', '获取内容统计', ['data' => $stats]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $stats
]);
} catch (DbException $e) {
$this->logFail('内容统计', '获取内容统计', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 用户统计
* @return Json
*/
public function getUserStats()
{
try {
$stats = [
'total_users' => AdminUser::where('delete_time', null)->count(),
];
// 记录操作日志
$this->logSuccess('用户统计', '获取用户统计', ['data' => $stats]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $stats
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('用户统计', '获取用户统计', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
}