更新welcome
This commit is contained in:
@@ -68,77 +68,150 @@ class IndexController extends Base{
|
||||
}
|
||||
# 欢迎页面
|
||||
public function welcome(){
|
||||
// 获取今日统计数据
|
||||
$today = date('Y-m-d');
|
||||
$todayStats = DailyStats::where('date', $today)
|
||||
->find();
|
||||
try {
|
||||
// 获取最近7天的日期
|
||||
$dates = [];
|
||||
for ($i = 6; $i >= 0; $i--) {
|
||||
$dates[] = date('Y-m-d', strtotime("-$i day"));
|
||||
}
|
||||
|
||||
// 获取最近7天的访问趋势
|
||||
$last7Days = DailyStats::where('date', '>=', date('Y-m-d', strtotime('-7 days')))
|
||||
->where('date', '<=', $today)
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
// 初始化数据数组
|
||||
$visitData = [];
|
||||
$userData = [];
|
||||
$resourceData = [];
|
||||
$articleData = [];
|
||||
|
||||
// 获取用户增长趋势
|
||||
$userGrowth = DailyStats::where('date', '>=', date('Y-m-d', strtotime('-30 days')))
|
||||
->where('date', '<=', $today)
|
||||
->field('date, new_users, total_users')
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
// 直接查询每天的数据
|
||||
foreach ($dates as $date) {
|
||||
$dayStats = Db::name('daily_stats')
|
||||
->where('date', $date)
|
||||
->find();
|
||||
|
||||
// 获取资源下载统计
|
||||
$resourceStats = DailyStats::where('date', '>=', date('Y-m-d', strtotime('-7 days')))
|
||||
->where('date', '<=', $today)
|
||||
->field('date, daily_resources, resource_downloads')
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
// 访问数据
|
||||
$visitData[] = [
|
||||
'date' => $date,
|
||||
'visits' => $dayStats ? intval($dayStats['daily_visits']) : 0,
|
||||
'uv' => $dayStats ? intval($dayStats['unique_visitors']) : 0
|
||||
];
|
||||
|
||||
// 获取文章访问统计
|
||||
$articleStats = DailyStats::where('date', '>=', date('Y-m-d', strtotime('-7 days')))
|
||||
->where('date', '<=', $today)
|
||||
->field('date, daily_articles, article_views')
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
// 用户数据
|
||||
$userData[] = [
|
||||
'date' => $date,
|
||||
'total' => $dayStats ? intval($dayStats['total_users']) : 0,
|
||||
'new' => $dayStats ? intval($dayStats['new_users']) : 0
|
||||
];
|
||||
|
||||
// 获取最近的操作日志
|
||||
$recentActivities = LogsOperation::field('operation_time, module, operation')
|
||||
->order('operation_time desc')
|
||||
->limit(10)
|
||||
->select()
|
||||
->each(function($item) {
|
||||
// 格式化时间
|
||||
$item['time'] = date('Y年m月d日 H:i:s', strtotime($item['operation_time']));
|
||||
// 格式化操作内容
|
||||
$item['content'] = date('Y年m月d日 H:i:s', strtotime($item['operation_time'])) . '在【' . $item['module'] . '】模块进行操作:' . $item['operation'];
|
||||
return $item;
|
||||
});
|
||||
|
||||
// 资源数据
|
||||
$resourceData[] = [
|
||||
'date' => $date,
|
||||
'total' => $dayStats ? intval($dayStats['total_resources']) : 0,
|
||||
'new' => $dayStats ? intval($dayStats['daily_resources']) : 0,
|
||||
'downloads' => $dayStats ? intval($dayStats['resource_downloads']) : 0
|
||||
];
|
||||
|
||||
// 准备图表数据
|
||||
$chartData = [
|
||||
'visitTrend' => $this->formatVisitTrendData($last7Days),
|
||||
'userGrowth' => $this->formatUserGrowthData($userGrowth),
|
||||
'resourceStats' => $this->formatResourceStatsData($resourceStats),
|
||||
'articleStats' => $this->formatArticleStatsData($articleStats)
|
||||
];
|
||||
// 文章数据
|
||||
$articleData[] = [
|
||||
'date' => $date,
|
||||
'total' => $dayStats ? intval($dayStats['total_articles']) : 0,
|
||||
'new' => $dayStats ? intval($dayStats['daily_articles']) : 0,
|
||||
'views' => $dayStats ? intval($dayStats['article_views']) : 0
|
||||
];
|
||||
}
|
||||
|
||||
// 准备统计数据
|
||||
$stats = [
|
||||
'total_users' => $todayStats['total_users'] ?? 0,
|
||||
'daily_visits' => $todayStats['daily_visits'] ?? 0,
|
||||
'total_articles' => $todayStats['total_articles'] ?? 0,
|
||||
'total_resources' => $todayStats['total_resources'] ?? 0,
|
||||
];
|
||||
// 获取今日统计数据
|
||||
$today = date('Y-m-d');
|
||||
$todayStats = Db::name('daily_stats')
|
||||
->where('date', $today)
|
||||
->find();
|
||||
|
||||
return View::fetch('', [
|
||||
'stats' => $stats,
|
||||
'chartData' => $chartData,
|
||||
'recentActivities' => $recentActivities
|
||||
]);
|
||||
// 获取最近的操作日志
|
||||
$recentActivities = Db::name('logs_operation')
|
||||
->field('operation_time, module, operation')
|
||||
->order('operation_time DESC')
|
||||
->limit(5)
|
||||
->select()
|
||||
->each(function($item) {
|
||||
$item['content'] = date('Y年m月d日 H:i:s', strtotime($item['operation_time'])) . ' 在 ' .
|
||||
($item['module'] ?: '未知模块') . ' ' .
|
||||
($item['operation'] ?: '未知操作');
|
||||
$item['icon'] = $this->getActivityIcon($item['module'] ?: '其他');
|
||||
return $item;
|
||||
});
|
||||
|
||||
// 处理图表数据
|
||||
$chartData = [
|
||||
'visitTrend' => [
|
||||
'dates' => array_map(function($item) { return date('m-d', strtotime($item['date'])); }, $visitData),
|
||||
'visits' => array_column($visitData, 'visits'),
|
||||
'uvs' => array_column($visitData, 'uv')
|
||||
],
|
||||
'userGrowth' => [
|
||||
'dates' => array_map(function($item) { return date('m-d', strtotime($item['date'])); }, $userData),
|
||||
'newUsers' => array_column($userData, 'new'),
|
||||
'totalUsers' => array_column($userData, 'total')
|
||||
],
|
||||
'resourceStats' => [
|
||||
'dates' => array_map(function($item) { return date('m-d', strtotime($item['date'])); }, $resourceData),
|
||||
'newResources' => array_column($resourceData, 'new'),
|
||||
'totalResources' => array_column($resourceData, 'total'),
|
||||
'downloads' => array_column($resourceData, 'downloads')
|
||||
],
|
||||
'articleStats' => [
|
||||
'dates' => array_map(function($item) { return date('m-d', strtotime($item['date'])); }, $articleData),
|
||||
'newArticles' => array_column($articleData, 'new'),
|
||||
'totalArticles' => array_column($articleData, 'total'),
|
||||
'views' => array_column($articleData, 'views')
|
||||
]
|
||||
];
|
||||
|
||||
// 传递给视图
|
||||
View::assign([
|
||||
'todayStats' => $todayStats ?: [
|
||||
'total_users' => 0,
|
||||
'new_users' => 0,
|
||||
'total_visits' => 0,
|
||||
'daily_visits' => 0,
|
||||
'unique_visitors' => 0,
|
||||
'total_articles' => 0,
|
||||
'daily_articles' => 0,
|
||||
'article_views' => 0,
|
||||
'total_resources' => 0,
|
||||
'daily_resources' => 0,
|
||||
'resource_downloads' => 0
|
||||
],
|
||||
'recentActivities' => $recentActivities,
|
||||
'chartData' => $chartData
|
||||
]);
|
||||
|
||||
return View::fetch();
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误日志
|
||||
\think\facade\Log::error('获取统计数据失败:' . $e->getMessage());
|
||||
// 返回空数据
|
||||
View::assign([
|
||||
'todayStats' => [
|
||||
'total_users' => 0,
|
||||
'new_users' => 0,
|
||||
'total_visits' => 0,
|
||||
'daily_visits' => 0,
|
||||
'unique_visitors' => 0,
|
||||
'total_articles' => 0,
|
||||
'daily_articles' => 0,
|
||||
'article_views' => 0,
|
||||
'total_resources' => 0,
|
||||
'daily_resources' => 0,
|
||||
'resource_downloads' => 0
|
||||
],
|
||||
'recentActivities' => [],
|
||||
'chartData' => [
|
||||
'visitTrend' => ['dates' => [], 'visits' => [], 'uvs' => []],
|
||||
'userGrowth' => ['dates' => [], 'newUsers' => [], 'totalUsers' => []],
|
||||
'resourceStats' => ['dates' => [], 'newResources' => [], 'totalResources' => [], 'downloads' => []],
|
||||
'articleStats' => ['dates' => [], 'newArticles' => [], 'totalArticles' => [], 'views' => []]
|
||||
]
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -211,22 +211,22 @@
|
||||
<div class="stats-container">
|
||||
<div class="stat-card">
|
||||
<div class="stat-title">用户总数</div>
|
||||
<div class="stat-value">{$stats.total_users|number_format}</div>
|
||||
<div class="stat-value">{$todayStats.total_users|number_format}</div>
|
||||
<div class="stat-icon">👥</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-title">今日访问</div>
|
||||
<div class="stat-value">{$stats.daily_visits|number_format}</div>
|
||||
<div class="stat-value">{$todayStats.daily_visits|number_format}</div>
|
||||
<div class="stat-icon">📊</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-title">文章总数</div>
|
||||
<div class="stat-value">{$stats.total_articles|number_format}</div>
|
||||
<div class="stat-value">{$todayStats.total_articles|number_format}</div>
|
||||
<div class="stat-icon">📝</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-title">资源总数</div>
|
||||
<div class="stat-value">{$stats.total_resources|number_format}</div>
|
||||
<div class="stat-value">{$todayStats.total_resources|number_format}</div>
|
||||
<div class="stat-icon">📦</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -504,7 +504,6 @@ function initVisitTrend() {
|
||||
// 用户增长图表
|
||||
function initUserGrowth() {
|
||||
var chart = echarts.init(document.getElementById('userGrowth'));
|
||||
window.userChart = chart;
|
||||
var option = {
|
||||
tooltip: {
|
||||
trigger: 'axis',
|
||||
@@ -527,7 +526,7 @@ function initUserGrowth() {
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
boundaryGap: false,
|
||||
data: []
|
||||
data: {$chartData.userGrowth.dates|json_encode}
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value'
|
||||
@@ -536,7 +535,7 @@ function initUserGrowth() {
|
||||
{
|
||||
name: '新增用户',
|
||||
type: 'bar',
|
||||
data: [],
|
||||
data: {$chartData.userGrowth.newUsers|json_encode},
|
||||
itemStyle: {
|
||||
color: '#3881fd'
|
||||
}
|
||||
@@ -545,7 +544,7 @@ function initUserGrowth() {
|
||||
name: '总用户数',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
data: [],
|
||||
data: {$chartData.userGrowth.totalUsers|json_encode},
|
||||
itemStyle: {
|
||||
color: '#10b981'
|
||||
},
|
||||
@@ -561,7 +560,6 @@ function initUserGrowth() {
|
||||
// 资源统计图表
|
||||
function initResourceStats() {
|
||||
var chart = echarts.init(document.getElementById('resourceStats'));
|
||||
window.resourceChart = chart;
|
||||
var option = {
|
||||
tooltip: {
|
||||
trigger: 'axis',
|
||||
@@ -573,7 +571,7 @@ function initResourceStats() {
|
||||
}
|
||||
},
|
||||
legend: {
|
||||
data: ['新增资源', '总资源数']
|
||||
data: ['新增资源', '总资源数', '下载量']
|
||||
},
|
||||
grid: {
|
||||
left: '3%',
|
||||
@@ -584,7 +582,7 @@ function initResourceStats() {
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
boundaryGap: false,
|
||||
data: []
|
||||
data: {$chartData.resourceStats.dates|json_encode}
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value'
|
||||
@@ -593,7 +591,7 @@ function initResourceStats() {
|
||||
{
|
||||
name: '新增资源',
|
||||
type: 'bar',
|
||||
data: [],
|
||||
data: {$chartData.resourceStats.newResources|json_encode},
|
||||
itemStyle: {
|
||||
color: '#3881fd'
|
||||
}
|
||||
@@ -602,13 +600,25 @@ function initResourceStats() {
|
||||
name: '总资源数',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
data: [],
|
||||
data: {$chartData.resourceStats.totalResources|json_encode},
|
||||
itemStyle: {
|
||||
color: '#10b981'
|
||||
},
|
||||
lineStyle: {
|
||||
width: 3
|
||||
}
|
||||
},
|
||||
{
|
||||
name: '下载量',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
data: {$chartData.resourceStats.downloads|json_encode},
|
||||
itemStyle: {
|
||||
color: '#f59e0b'
|
||||
},
|
||||
lineStyle: {
|
||||
width: 3
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
@@ -618,7 +628,6 @@ function initResourceStats() {
|
||||
// 文章统计图表
|
||||
function initArticleStats() {
|
||||
var chart = echarts.init(document.getElementById('articleStats'));
|
||||
window.articleChart = chart;
|
||||
var option = {
|
||||
tooltip: {
|
||||
trigger: 'axis',
|
||||
@@ -630,7 +639,7 @@ function initArticleStats() {
|
||||
}
|
||||
},
|
||||
legend: {
|
||||
data: ['新增文章', '总文章数']
|
||||
data: ['新增文章', '总文章数', '浏览量']
|
||||
},
|
||||
grid: {
|
||||
left: '3%',
|
||||
@@ -641,7 +650,7 @@ function initArticleStats() {
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
boundaryGap: false,
|
||||
data: []
|
||||
data: {$chartData.articleStats.dates|json_encode}
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value'
|
||||
@@ -650,7 +659,7 @@ function initArticleStats() {
|
||||
{
|
||||
name: '新增文章',
|
||||
type: 'bar',
|
||||
data: [],
|
||||
data: {$chartData.articleStats.newArticles|json_encode},
|
||||
itemStyle: {
|
||||
color: '#3881fd'
|
||||
}
|
||||
@@ -659,13 +668,25 @@ function initArticleStats() {
|
||||
name: '总文章数',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
data: [],
|
||||
data: {$chartData.articleStats.totalArticles|json_encode},
|
||||
itemStyle: {
|
||||
color: '#10b981'
|
||||
},
|
||||
lineStyle: {
|
||||
width: 3
|
||||
}
|
||||
},
|
||||
{
|
||||
name: '浏览量',
|
||||
type: 'line',
|
||||
smooth: true,
|
||||
data: {$chartData.articleStats.views|json_encode},
|
||||
itemStyle: {
|
||||
color: '#f59e0b'
|
||||
},
|
||||
lineStyle: {
|
||||
width: 3
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
@@ -674,18 +695,32 @@ function initArticleStats() {
|
||||
|
||||
// 初始化所有图表
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
initVisitTrend();
|
||||
initUserGrowth();
|
||||
initResourceStats();
|
||||
initArticleStats();
|
||||
|
||||
// 监听窗口大小变化,重绘图表
|
||||
window.addEventListener('resize', function() {
|
||||
var charts = document.querySelectorAll('.chart-container');
|
||||
charts.forEach(function(chart) {
|
||||
echarts.getInstanceByDom(chart)?.resize();
|
||||
// 确保ECharts已加载
|
||||
if (typeof echarts === 'undefined') {
|
||||
console.error('ECharts未加载');
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化图表
|
||||
try {
|
||||
initVisitTrend();
|
||||
initUserGrowth();
|
||||
initResourceStats();
|
||||
initArticleStats();
|
||||
|
||||
// 监听窗口大小变化,重绘图表
|
||||
window.addEventListener('resize', function() {
|
||||
var charts = document.querySelectorAll('.chart-container');
|
||||
charts.forEach(function(chart) {
|
||||
var instance = echarts.getInstanceByDom(chart);
|
||||
if (instance) {
|
||||
instance.resize();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('初始化图表失败:', error);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user