更新后端
This commit is contained in:
@@ -7,6 +7,7 @@ use app\admin\controller\Base;
|
||||
use think\facade\Db;
|
||||
use think\facade\View;
|
||||
use think\facade\Request;
|
||||
use app\common\service\LogService;
|
||||
|
||||
class Article extends Base
|
||||
{
|
||||
@@ -411,4 +412,20 @@ class Article extends Base
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
|
||||
}
|
||||
|
||||
//统计文章数量
|
||||
public function counts() {
|
||||
$total = Db::table('yz_article')
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3)
|
||||
->count();
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'total' => $total
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -61,10 +61,206 @@ class Index extends Base{
|
||||
}
|
||||
# 欢迎页面
|
||||
public function welcome(){
|
||||
View::assign([
|
||||
'time' => date('Y-m-d',$_SERVER['REQUEST_TIME']),
|
||||
// 获取今日统计数据
|
||||
$today = date('Y-m-d');
|
||||
$todayStats = Db::name('yz_daily_stats')
|
||||
->where('date', $today)
|
||||
->find();
|
||||
|
||||
// 获取最近7天的访问趋势
|
||||
$last7Days = Db::name('yz_daily_stats')
|
||||
->where('date', '>=', date('Y-m-d', strtotime('-7 days')))
|
||||
->where('date', '<=', $today)
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 获取用户增长趋势
|
||||
$userGrowth = Db::name('yz_daily_stats')
|
||||
->where('date', '>=', date('Y-m-d', strtotime('-30 days')))
|
||||
->where('date', '<=', $today)
|
||||
->field('date, new_users, total_users')
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 获取资源下载统计
|
||||
$resourceStats = Db::name('yz_daily_stats')
|
||||
->where('date', '>=', date('Y-m-d', strtotime('-7 days')))
|
||||
->where('date', '<=', $today)
|
||||
->field('date, daily_resources, resource_downloads')
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 获取文章访问统计
|
||||
$articleStats = Db::name('yz_daily_stats')
|
||||
->where('date', '>=', date('Y-m-d', strtotime('-7 days')))
|
||||
->where('date', '<=', $today)
|
||||
->field('date, daily_articles, article_views')
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 获取最近的活动记录
|
||||
$recentActivities = $this->getRecentActivities();
|
||||
|
||||
// 准备图表数据
|
||||
$chartData = [
|
||||
'visitTrend' => $this->formatVisitTrendData($last7Days),
|
||||
'userGrowth' => $this->formatUserGrowthData($userGrowth),
|
||||
'resourceStats' => $this->formatResourceStatsData($resourceStats),
|
||||
'articleStats' => $this->formatArticleStatsData($articleStats)
|
||||
];
|
||||
|
||||
// 准备统计数据
|
||||
$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,
|
||||
];
|
||||
|
||||
return View::fetch('', [
|
||||
'stats' => $stats,
|
||||
'chartData' => $chartData,
|
||||
'recentActivities' => $recentActivities
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近的活动记录
|
||||
*/
|
||||
private function getRecentActivities()
|
||||
{
|
||||
$today = date('Y-m-d');
|
||||
$activities = [];
|
||||
|
||||
// 获取今日新用户
|
||||
$newUsers = Db::name('yz_daily_stats')
|
||||
->where('date', $today)
|
||||
->value('new_users');
|
||||
if ($newUsers > 0) {
|
||||
$activities[] = [
|
||||
'icon' => '👥',
|
||||
'title' => '新增用户 ' . $newUsers . ' 人',
|
||||
'time' => '今日'
|
||||
];
|
||||
}
|
||||
|
||||
// 获取今日文章
|
||||
$newArticles = Db::name('yz_daily_stats')
|
||||
->where('date', $today)
|
||||
->value('daily_articles');
|
||||
if ($newArticles > 0) {
|
||||
$activities[] = [
|
||||
'icon' => '📝',
|
||||
'title' => '发布文章 ' . $newArticles . ' 篇',
|
||||
'time' => '今日'
|
||||
];
|
||||
}
|
||||
|
||||
// 获取今日资源
|
||||
$newResources = Db::name('yz_daily_stats')
|
||||
->where('date', $today)
|
||||
->value('daily_resources');
|
||||
if ($newResources > 0) {
|
||||
$activities[] = [
|
||||
'icon' => '📦',
|
||||
'title' => '上传资源 ' . $newResources . ' 个',
|
||||
'time' => '今日'
|
||||
];
|
||||
}
|
||||
|
||||
return $activities;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化访问趋势数据
|
||||
*/
|
||||
private function formatVisitTrendData($data)
|
||||
{
|
||||
$dates = [];
|
||||
$visits = [];
|
||||
$uvs = [];
|
||||
|
||||
foreach ($data as $item) {
|
||||
$dates[] = date('m-d', strtotime($item['date']));
|
||||
$visits[] = $item['daily_visits'];
|
||||
$uvs[] = $item['unique_visitors'];
|
||||
}
|
||||
|
||||
return [
|
||||
'dates' => $dates,
|
||||
'visits' => $visits,
|
||||
'uvs' => $uvs
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化用户增长数据
|
||||
*/
|
||||
private function formatUserGrowthData($data)
|
||||
{
|
||||
$dates = [];
|
||||
$newUsers = [];
|
||||
$totalUsers = [];
|
||||
|
||||
foreach ($data as $item) {
|
||||
$dates[] = date('m-d', strtotime($item['date']));
|
||||
$newUsers[] = $item['new_users'];
|
||||
$totalUsers[] = $item['total_users'];
|
||||
}
|
||||
|
||||
return [
|
||||
'dates' => $dates,
|
||||
'newUsers' => $newUsers,
|
||||
'totalUsers' => $totalUsers
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化资源统计数据
|
||||
*/
|
||||
private function formatResourceStatsData($data)
|
||||
{
|
||||
$dates = [];
|
||||
$resources = [];
|
||||
$downloads = [];
|
||||
|
||||
foreach ($data as $item) {
|
||||
$dates[] = date('m-d', strtotime($item['date']));
|
||||
$resources[] = $item['daily_resources'];
|
||||
$downloads[] = $item['resource_downloads'];
|
||||
}
|
||||
|
||||
return [
|
||||
'dates' => $dates,
|
||||
'resources' => $resources,
|
||||
'downloads' => $downloads
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文章统计数据
|
||||
*/
|
||||
private function formatArticleStatsData($data)
|
||||
{
|
||||
$dates = [];
|
||||
$articles = [];
|
||||
$views = [];
|
||||
|
||||
foreach ($data as $item) {
|
||||
$dates[] = date('m-d', strtotime($item['date']));
|
||||
$articles[] = $item['daily_articles'];
|
||||
$views[] = $item['article_views'];
|
||||
}
|
||||
|
||||
return [
|
||||
'dates' => $dates,
|
||||
'articles' => $articles,
|
||||
'views' => $views
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,4 +439,5 @@ class Index extends Base{
|
||||
return json(['code'=>1, 'msg'=>$e->getMessage()])->send();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\admin\controller\Base;
|
||||
use think\facade\Db;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
|
||||
class Log extends Base
|
||||
{
|
||||
/**
|
||||
* 登录日志列表
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$page = input('post.page', 1);
|
||||
$limit = input('post.limit', 10);
|
||||
$username = input('post.username');
|
||||
$ip = input('post.ip');
|
||||
$status = input('post.status');
|
||||
$startTime = input('post.start_time');
|
||||
$endTime = input('post.end_time');
|
||||
|
||||
$query = Db::name('yz_logs_login');
|
||||
|
||||
// 搜索条件
|
||||
if ($username) {
|
||||
$query = $query->where('username', 'like', "%{$username}%");
|
||||
}
|
||||
if ($ip) {
|
||||
$query = $query->where('ip_address', 'like', "%{$ip}%");
|
||||
}
|
||||
if ($status !== '') {
|
||||
$query = $query->where('login_status', $status);
|
||||
}
|
||||
if ($startTime) {
|
||||
$query = $query->where('login_time', '>=', $startTime);
|
||||
}
|
||||
if ($endTime) {
|
||||
$query = $query->where('login_time', '<=', $endTime);
|
||||
}
|
||||
|
||||
$count = $query->count();
|
||||
$list = $query->order('id desc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'count' => $count,
|
||||
'data' => $list
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作日志列表
|
||||
*/
|
||||
public function operation()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$page = input('post.page', 1);
|
||||
$limit = input('post.limit', 10);
|
||||
$username = input('post.username');
|
||||
$module = input('post.module');
|
||||
$operation = input('post.operation');
|
||||
$status = input('post.status');
|
||||
$startTime = input('post.start_time');
|
||||
$endTime = input('post.end_time');
|
||||
|
||||
$query = Db::name('yz_logs_operation');
|
||||
|
||||
// 搜索条件
|
||||
if ($username) {
|
||||
$query = $query->where('username', 'like', "%{$username}%");
|
||||
}
|
||||
if ($module) {
|
||||
$query = $query->where('module', 'like', "%{$module}%");
|
||||
}
|
||||
if ($operation) {
|
||||
$query = $query->where('operation', 'like', "%{$operation}%");
|
||||
}
|
||||
if ($status !== '') {
|
||||
$query = $query->where('status', $status);
|
||||
}
|
||||
if ($startTime) {
|
||||
$query = $query->where('operation_time', '>=', $startTime);
|
||||
}
|
||||
if ($endTime) {
|
||||
$query = $query->where('operation_time', '<=', $endTime);
|
||||
}
|
||||
|
||||
$count = $query->count();
|
||||
$list = $query->order('id desc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'count' => $count,
|
||||
'data' => $list
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录操作日志
|
||||
*/
|
||||
protected function recordOperation($operation, $status = 1, $error_message = '')
|
||||
{
|
||||
$data = [
|
||||
'username' => session('admin_username'),
|
||||
'module' => '日志管理',
|
||||
'operation' => $operation,
|
||||
'request_method' => Request::method(),
|
||||
'request_url' => Request::url(true),
|
||||
'request_params' => json_encode(Request::param(), JSON_UNESCAPED_UNICODE),
|
||||
'ip_address' => Request::ip(),
|
||||
'status' => $status,
|
||||
'error_message' => $error_message,
|
||||
'operation_time' => date('Y-m-d H:i:s'),
|
||||
'execution_time' => 0
|
||||
];
|
||||
|
||||
Db::name('yz_logs_operation')->insert($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除登录日志
|
||||
*/
|
||||
public function deleteLogin()
|
||||
{
|
||||
$id = input('post.id');
|
||||
try {
|
||||
if (Db::name('yz_logs_login')->delete($id)) {
|
||||
$this->recordOperation('删除登录日志');
|
||||
return json(['code' => 0, 'msg' => '删除成功']);
|
||||
}
|
||||
$this->recordOperation('删除登录日志', 0, '删除失败');
|
||||
return json(['code' => 1, 'msg' => '删除失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('删除登录日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除操作日志
|
||||
*/
|
||||
public function deleteOperation()
|
||||
{
|
||||
$id = input('post.id');
|
||||
try {
|
||||
if (Db::name('yz_logs_operation')->delete($id)) {
|
||||
$this->recordOperation('删除操作日志');
|
||||
return json(['code' => 0, 'msg' => '删除成功']);
|
||||
}
|
||||
$this->recordOperation('删除操作日志', 0, '删除失败');
|
||||
return json(['code' => 1, 'msg' => '删除失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('删除操作日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空登录日志
|
||||
*/
|
||||
public function clearLogin()
|
||||
{
|
||||
try {
|
||||
if (Db::name('yz_logs_login')->where('1=1')->delete()) {
|
||||
$this->recordOperation('清空登录日志');
|
||||
return json(['code' => 0, 'msg' => '清空成功']);
|
||||
}
|
||||
$this->recordOperation('清空登录日志', 0, '清空失败');
|
||||
return json(['code' => 1, 'msg' => '清空失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('清空登录日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '清空失败:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空操作日志
|
||||
*/
|
||||
public function clearOperation()
|
||||
{
|
||||
try {
|
||||
if (Db::name('yz_logs_operation')->where('1=1')->delete()) {
|
||||
$this->recordOperation('清空操作日志');
|
||||
return json(['code' => 0, 'msg' => '清空成功']);
|
||||
}
|
||||
$this->recordOperation('清空操作日志', 0, '清空失败');
|
||||
return json(['code' => 1, 'msg' => '清空失败']);
|
||||
} catch (\Exception $e) {
|
||||
$this->recordOperation('清空操作日志', 0, $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '清空失败:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -445,4 +445,20 @@ class Resources extends Base
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
//统计资源数量
|
||||
public function counts() {
|
||||
$total = Db::table('yz_resources')
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3)
|
||||
->count();
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'total' => $total
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user