v1.0.10
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* 角色列表接口 GET/POST /api/system/role_list.php
|
||||
* 参数:page / limit / keyword / is_active
|
||||
*/
|
||||
require_once __DIR__ . '/../common/db.php';
|
||||
require_once __DIR__ . '/../common/response.php';
|
||||
require_once __DIR__ . '/../common/auth.php';
|
||||
|
||||
checkPermission('system');
|
||||
|
||||
[$page, $limit] = pageParams();
|
||||
$keyword = trim($_REQUEST['keyword'] ?? '');
|
||||
$active = isset($_REQUEST['is_active']) && $_REQUEST['is_active'] !== '' ? (int)$_REQUEST['is_active'] : null;
|
||||
|
||||
$where = ['1=1'];
|
||||
$params = [];
|
||||
if ($keyword !== '') {
|
||||
$where[] = 'role_name LIKE ?';
|
||||
$params[] = "%$keyword%";
|
||||
}
|
||||
if ($active !== null) { $where[] = 'is_active = ?'; $params[] = $active; }
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
$pdo = DB::getInstance()->getPdo();
|
||||
|
||||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM system_roles WHERE $whereSql");
|
||||
$stmt->execute($params);
|
||||
$total = (int)$stmt->fetchColumn();
|
||||
|
||||
$offset = ($page - 1) * $limit;
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT id, role_name, permissions, is_active, created_at,
|
||||
(SELECT COUNT(*) FROM system_users u WHERE u.role_id = system_roles.id) AS user_count
|
||||
FROM system_roles
|
||||
WHERE $whereSql
|
||||
ORDER BY id ASC
|
||||
LIMIT $limit OFFSET $offset"
|
||||
);
|
||||
$stmt->execute($params);
|
||||
$list = $stmt->fetchAll();
|
||||
foreach ($list as &$r) {
|
||||
$r['permissions'] = json_decode($r['permissions'], true) ?: [];
|
||||
}
|
||||
unset($r);
|
||||
|
||||
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);
|
||||
Reference in New Issue
Block a user