257 lines
7.2 KiB
PHP
257 lines
7.2 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace app\admin\controller\OperationLog;
|
||
|
||
use app\admin\BaseController;
|
||
use think\facade\Request;
|
||
use think\response\Json;
|
||
use app\model\OperationLog;
|
||
use app\model\AdminUser;
|
||
|
||
class OperationLogController extends BaseController
|
||
{
|
||
/**
|
||
* 获取操作日志列表
|
||
* @return Json
|
||
*/
|
||
public function getOperationLogs()
|
||
{
|
||
try {
|
||
$page = Request::param('page/d', 1);
|
||
$pageSize = Request::param('pageSize/d', 20);
|
||
$keyword = Request::param('keyword/s', '');
|
||
$module = Request::param('module/s', '');
|
||
$action = Request::param('action/s', '');
|
||
$status = Request::param('status/s', '');
|
||
$startTime = Request::param('startTime/s', '');
|
||
$endTime = Request::param('endTime/s', '');
|
||
|
||
$query = OperationLog::where('delete_time', null);
|
||
|
||
// 关键词搜索(用户姓名、URL)
|
||
if ($keyword) {
|
||
$query->where(function ($q) use ($keyword) {
|
||
$q
|
||
->whereOr('user_name', 'like', "%{$keyword}%")
|
||
->whereOr('url', 'like', "%{$keyword}%");
|
||
});
|
||
}
|
||
|
||
// 模块筛选
|
||
if ($module) {
|
||
$query->where('module', $module);
|
||
}
|
||
|
||
// 操作动作筛选
|
||
if ($action) {
|
||
$query->where('action', $action);
|
||
}
|
||
|
||
// 状态筛选
|
||
if ($status !== '') {
|
||
$query->where('status', $status);
|
||
}
|
||
|
||
// 时间范围筛选
|
||
if ($startTime) {
|
||
$query->where('create_time', '>=', $startTime);
|
||
}
|
||
if ($endTime) {
|
||
$query->where('create_time', '<=', $endTime);
|
||
}
|
||
|
||
// 获取总数
|
||
$total = $query->count();
|
||
|
||
// 分页查询
|
||
$list = $query->order('id', 'desc')
|
||
->page($page, $pageSize)
|
||
->select()
|
||
->toArray();
|
||
|
||
// 获取所有唯一的 user_id
|
||
$userIds = array_unique(array_column($list, 'user_id'));
|
||
$userIds = array_filter($userIds); // 过滤掉0和null
|
||
|
||
// 批量查询用户信息
|
||
$users = [];
|
||
if (!empty($userIds)) {
|
||
$userList = AdminUser::whereIn('id', $userIds)
|
||
->where('delete_time', null)
|
||
->field('id, name')
|
||
->select()
|
||
->toArray();
|
||
|
||
// 转换为以 id 为键的数组,方便查找
|
||
foreach ($userList as $user) {
|
||
$users[$user['id']] = [
|
||
'name' => $user['name']
|
||
];
|
||
}
|
||
}
|
||
|
||
// 将用户信息合并到日志列表中
|
||
foreach ($list as &$item) {
|
||
if (isset($users[$item['user_id']])) {
|
||
$item['user_name'] = $users[$item['user_id']]['name'];
|
||
} else {
|
||
$item['user_name'] = $item['user_name'] ?? '';
|
||
}
|
||
}
|
||
unset($item); // 释放引用
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '获取成功',
|
||
'data' => [
|
||
'list' => $list,
|
||
'total' => $total,
|
||
'page' => $page,
|
||
'pageSize' => $pageSize
|
||
]
|
||
]);
|
||
} catch (\Exception $e) {
|
||
return json([
|
||
'code' => 500,
|
||
'msg' => '获取操作日志失败: ' . $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取操作日志详情
|
||
* @param int $id
|
||
* @return Json
|
||
*/
|
||
public function getOperationLogDetail(int $id)
|
||
{
|
||
try {
|
||
$log = OperationLog::where('id', $id)
|
||
->where('delete_time', null)
|
||
->find();
|
||
|
||
if (!$log) {
|
||
return json([
|
||
'code' => 404,
|
||
'msg' => '操作日志不存在'
|
||
]);
|
||
}
|
||
|
||
$logData = $log->toArray();
|
||
$this->logSuccess('操作日志', '查看操作日志详情', $logData);
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '获取成功',
|
||
'data' => $logData
|
||
]);
|
||
} catch (\Exception $e) {
|
||
$this->logFail('操作日志', '查看操作日志详情', $e->getMessage());
|
||
return json([
|
||
'code' => 500,
|
||
'msg' => '获取操作日志详情失败: ' . $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除操作日志
|
||
* @param int $id
|
||
* @return Json
|
||
*/
|
||
public function deleteOperationLog(int $id)
|
||
{
|
||
try {
|
||
$log = OperationLog::where('id', $id)
|
||
->where('delete_time', null)
|
||
->find();
|
||
|
||
if (!$log) {
|
||
return json([
|
||
'code' => 404,
|
||
'msg' => '操作日志不存在'
|
||
]);
|
||
}
|
||
|
||
// 软删除
|
||
$log->delete();
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '删除成功'
|
||
]);
|
||
} catch (\Exception $e) {
|
||
return json([
|
||
'code' => 500,
|
||
'msg' => '删除失败: ' . $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量删除操作日志
|
||
* @return Json
|
||
*/
|
||
public function batchDeleteOperationLogs()
|
||
{
|
||
try {
|
||
$ids = Request::param('ids/a', []);
|
||
|
||
if (empty($ids)) {
|
||
return json([
|
||
'code' => 400,
|
||
'msg' => '请选择要删除的操作日志'
|
||
]);
|
||
}
|
||
|
||
OperationLog::whereIn('id', $ids)
|
||
->where('delete_time', null)
|
||
->delete();
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '批量删除成功'
|
||
]);
|
||
} catch (\Exception $e) {
|
||
return json([
|
||
'code' => 500,
|
||
'msg' => '批量删除失败: ' . $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取操作统计信息(模块、动作等)
|
||
* @return Json
|
||
*/
|
||
public function getOperationStatistics()
|
||
{
|
||
try {
|
||
// 获取模块列表
|
||
$modules = OperationLog::where('delete_time', null)
|
||
->group('module')
|
||
->column('module');
|
||
|
||
// 获取动作列表
|
||
$actions = OperationLog::where('delete_time', null)
|
||
->group('action')
|
||
->column('action');
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '获取成功',
|
||
'data' => [
|
||
'modules' => $modules,
|
||
'actions' => $actions
|
||
]
|
||
]);
|
||
} catch (\Exception $e) {
|
||
return json([
|
||
'code' => 500,
|
||
'msg' => '获取统计信息失败: ' . $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
}
|