初始化
缺陷:主题配色需要优化整体的同风格
This commit is contained in:
@@ -20,8 +20,8 @@ class RoleService extends BaseService
|
||||
{
|
||||
parent::__construct();
|
||||
$this->model = RoleModel::class;
|
||||
$this->selectField = ['id', 'name', 'value', 'pid', 'desc', 'created_at'];
|
||||
$this->queryField = ['name' => 'like', 'value' => 'like', 'pid' => '='];
|
||||
$this->selectField = ['id', 'name', 'value', 'pid', 'desc', 'status', 'color', 'created_at'];
|
||||
$this->queryField = ['name' => 'like', 'value' => 'like', 'pid' => '=', 'status' => '='];
|
||||
$this->orderBy = ['name' => 'id', 'sort' => 'asc'];
|
||||
}
|
||||
|
||||
@@ -97,13 +97,13 @@ class RoleService extends BaseService
|
||||
'created_at' => time()
|
||||
];
|
||||
}
|
||||
$userRoleBinding = RoleMenuRelationModel::insert($insertData);
|
||||
if (!$userRoleBinding) {
|
||||
// 只取消勾选、没有新增时 $insertData 为空,insert([]) 返回 false 会误判成失败
|
||||
if (!empty($insertData) && !RoleMenuRelationModel::insert($insertData)) {
|
||||
$this->utils->errorThrow('更新失败!');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
RedisService::getInstance()->init(config('nl.redis.menu_key'))->del($roleId);
|
||||
$this->flushRoleCache((int) $roleId);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
$this->utils->errorThrow($e->getMessage());
|
||||
@@ -112,6 +112,50 @@ class RoleService extends BaseService
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口授权树
|
||||
*/
|
||||
public function endpointTree(): array
|
||||
{
|
||||
return PermissionService::getInstance()->endpointTree();
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色已授权的接口 id
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getEndpointIdsByRoleId($roleId): array
|
||||
{
|
||||
if (empty($roleId)) $this->utils->errorThrow('请选择角色');
|
||||
return PermissionService::getInstance()->grantedIds((int) $roleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存角色接口授权
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveRoleEndpoint(int $roleId, array $endpointIds): bool
|
||||
{
|
||||
if ($roleId <= 0) $this->utils->errorThrow('请选择角色');
|
||||
if ($roleId === 1) $this->utils->errorThrow('超级管理员默认拥有全部接口权限,无需授权');
|
||||
return PermissionService::getInstance()->grant($roleId, $endpointIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用 / 停用角色
|
||||
*
|
||||
* 停用后该角色下的账号仍能登录但权限为空,所以要顺手清掉权限与菜单缓存
|
||||
* @throws Exception
|
||||
*/
|
||||
public function status($id, $status): mixed
|
||||
{
|
||||
$id = (int) $id;
|
||||
if ($id === 1) $this->utils->errorThrow('超级管理员角色禁止停用');
|
||||
$result = $this->save($id, ['status' => (int) $status]);
|
||||
$this->flushRoleCache($id);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据详情
|
||||
* @param $id
|
||||
@@ -159,6 +203,19 @@ class RoleService extends BaseService
|
||||
if (array_intersect(array_map('intval', $ids), [1, 2])) {
|
||||
$this->utils->errorThrow('管理员角色禁止删除');
|
||||
}
|
||||
return $this->del($id);
|
||||
$result = $this->del($id);
|
||||
foreach ($ids as $roleId) {
|
||||
$this->flushRoleCache((int) $roleId);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色的菜单与权限都按角色缓存,改动后必须一起清,否则要等 1 小时才生效
|
||||
*/
|
||||
private function flushRoleCache(int $roleId): void
|
||||
{
|
||||
RedisService::getInstance()->init(config('nl.redis.menu_key'))->del($roleId);
|
||||
PermissionService::getInstance()->clear($roleId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user