Files
superlink/api/system/user_list.php
T

49 lines
1.6 KiB
PHP

<?php
/**
* 用户列表接口 GET/POST /api/system/user_list.php
* 参数:page / limit / keyword / role_id / is_active
*/
require_once __DIR__ . '/../common/Api.php';
$pdo = Api::boot(['module' => 'system']);
[$page, $limit] = pageParams();
$keyword = trim($_REQUEST['keyword'] ?? '');
$roleId = (int)($_REQUEST['role_id'] ?? 0);
$active = isset($_REQUEST['is_active']) && $_REQUEST['is_active'] !== '' ? (int)$_REQUEST['is_active'] : null;
$where = ['1=1'];
$params = [];
if ($keyword !== '') {
$where[] = '(u.username LIKE ? OR u.real_name LIKE ?)';
$like = "%$keyword%";
array_push($params, $like, $like);
}
if ($roleId > 0) { $where[] = 'u.role_id = ?'; $params[] = $roleId; }
if ($active !== null) { $where[] = 'u.is_active = ?'; $params[] = $active; }
$whereSql = implode(' AND ', $where);
$pdo = DB::getInstance()->getPdo();
$stmt = $pdo->prepare("SELECT COUNT(*) FROM system_users u WHERE $whereSql");
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
"SELECT u.id, u.username, u.real_name, u.role_id, r.role_name, u.is_active,
u.last_login_time, u.last_login_ip, u.created_at
FROM system_users u
LEFT JOIN system_roles r ON r.id = u.role_id
WHERE $whereSql
ORDER BY u.id ASC
LIMIT $limit OFFSET $offset"
);
$stmt->execute($params);
$list = $stmt->fetchAll();
// 角色下拉
$roles = $pdo->query("SELECT id, role_name FROM system_roles WHERE is_active = 1 ORDER BY id")->fetchAll();
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit, 'roles' => $roles]);