Files
superlink/api/system/log_list.php
T

57 lines
2.1 KiB
PHP

<?php
/**
* 操作日志查询接口 GET/POST /api/system/log_list.php
* 仅超级管理员可见,默认查询近1个月
* 参数:page / limit / keyword / action / module / username / start_date / end_date
*/
require_once __DIR__ . '/../common/Api.php';
$pdo = Api::boot(['super' => true]);
[$page, $limit] = pageParams();
$keyword = trim($_REQUEST['keyword'] ?? '');
$action = trim($_REQUEST['action'] ?? '');
$module = trim($_REQUEST['module'] ?? '');
$username = trim($_REQUEST['username'] ?? '');
$startDate = trim($_REQUEST['start_date'] ?? '');
$endDate = trim($_REQUEST['end_date'] ?? '');
$where = ['created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)'];
$params = [];
if ($keyword !== '') {
$where[] = '(username LIKE ? OR content LIKE ? OR module LIKE ?)';
$like = "%$keyword%";
array_push($params, $like, $like, $like);
}
if ($action !== '') { $where[] = 'action = ?'; $params[] = $action; }
if ($module !== '') { $where[] = 'module = ?'; $params[] = $module; }
if ($username !== '') { $where[] = 'username = ?'; $params[] = $username; }
if ($startDate !== '') { $where[] = 'DATE(created_at) >= ?'; $params[] = $startDate; }
if ($endDate !== '') { $where[] = 'DATE(created_at) <= ?'; $params[] = $endDate; }
$whereSql = implode(' AND ', $where);
$pdo = DB::getInstance()->getPdo();
$stmt = $pdo->prepare("SELECT COUNT(*) FROM system_logs WHERE $whereSql");
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
"SELECT id, user_id, username, action, module, target_table, target_id, content, ip, created_at
FROM system_logs
WHERE $whereSql
ORDER BY id DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute($params);
$list = $stmt->fetchAll();
// 筛选下拉字典
$dict = [
'actions' => $pdo->query("SELECT DISTINCT action FROM system_logs ORDER BY action")->fetchAll(PDO::FETCH_COLUMN),
'modules' => $pdo->query("SELECT DISTINCT module FROM system_logs ORDER BY module")->fetchAll(PDO::FETCH_COLUMN),
];
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit, 'dict' => $dict]);