c86f08c325
Co-Authored-By: Claude Code <noreply@anthropic.com>
34 lines
1.2 KiB
PHP
34 lines
1.2 KiB
PHP
<?php
|
||
/**
|
||
* 新增角色接口(含权限分配) POST /api/system/role_add.php
|
||
* 入参:role_name / permissions(JSON数组,菜单标识)/ is_active
|
||
*/
|
||
require_once __DIR__ . '/../common/Api.php';
|
||
$pdo = Api::boot(['module' => 'system']);
|
||
|
||
|
||
$roleName = trim($_POST['role_name'] ?? '');
|
||
$perms = json_decode($_POST['permissions'] ?? '[]', true);
|
||
$isActive = isset($_POST['is_active']) ? ((int)$_POST['is_active'] ? 1 : 0) : 1;
|
||
|
||
if ($roleName === '') {
|
||
Response::error('角色名称不能为空', 400);
|
||
}
|
||
if (!is_array($perms)) {
|
||
Response::error('权限格式错误', 400);
|
||
}
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
$chk = $pdo->prepare("SELECT COUNT(*) FROM system_roles WHERE role_name = ?");
|
||
$chk->execute([$roleName]);
|
||
if ((int)$chk->fetchColumn() > 0) {
|
||
Response::error('角色名称已存在');
|
||
}
|
||
|
||
$stmt = $pdo->prepare("INSERT INTO system_roles (role_name, permissions, is_active) VALUES (?, ?, ?)");
|
||
$stmt->execute([$roleName, json_encode(array_values($perms), JSON_UNESCAPED_UNICODE), $isActive]);
|
||
$newId = (int)$pdo->lastInsertId();
|
||
|
||
logCurrent('add', 'system', 'system_roles', $newId, ['role_name' => $roleName, 'permissions' => $perms]);
|
||
Response::success(['id' => $newId], '新增成功');
|