125 lines
4.0 KiB
PHP
125 lines
4.0 KiB
PHP
<?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 {
|
|
$yesterday = date('Y-m-d', strtotime('-1 day'));
|
|
$monthStart = date('Y-m-01');
|
|
$monthEnd = date('Y-m-t 23:59:59');
|
|
|
|
// 总发布量
|
|
$totalArticles = Articles::where('delete_time', null)
|
|
->where('status', 2)
|
|
->count();
|
|
|
|
// 昨日新增发布
|
|
$yesterdayArticles = 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();
|
|
|
|
// 总点赞量
|
|
$totalLikes = Articles::where('delete_time', null)
|
|
->where('status', 2)
|
|
->sum('likes');
|
|
|
|
// 总访问量
|
|
$totalViews = Articles::where('delete_time', null)
|
|
->where('status', 2)
|
|
->sum('views');
|
|
|
|
// 热门内容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 = [
|
|
'total_articles' => $totalArticles,
|
|
'yesterday_articles' => $yesterdayArticles,
|
|
'month_articles' => $monthNewArticles,
|
|
'total_likes' => (int)$totalLikes,
|
|
'total_views' => (int)$totalViews,
|
|
'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' => []
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
}
|