This commit is contained in:
walkor 2022-12-21 15:46:47 +08:00
parent 073ab4afcc
commit 5ec88341e3
2 changed files with 15 additions and 6 deletions

View File

@ -13,7 +13,7 @@ class Auth
* @param array $admin_ids * @param array $admin_ids
* @return array * @return array
*/ */
public static function getAdminIds(array $admin_ids = []): array public static function getDescendantRoleIds(array $admin_ids = []): array
{ {
if (!$admin_ids) { if (!$admin_ids) {
$admin = admin(); $admin = admin();
@ -31,9 +31,18 @@ class Auth
$roles = Role::get(); $roles = Role::get();
$tree = new Tree($roles); $tree = new Tree($roles);
$descendants = $tree->getDescendants($role_ids, true); $descendants = $tree->getDescendant($role_ids, true);
$role_ids = array_column($descendants, 'id'); return array_column($descendants, 'id');
return AdminRole::whereIn('role_id', $role_ids)->pluck('admin_id')->toArray(); }
/**
* 获取管理员及子管理员id数组
* @param array $admin_ids
* @return array
*/
public static function getDescendantAdminIds(array $admin_ids = []): array
{
return AdminRole::whereIn('role_id', static::getDescendantRoleIds())->pluck('admin_id')->toArray();
} }
/** /**

View File

@ -43,7 +43,7 @@ class Tree
* @param bool $with_self * @param bool $with_self
* @return array * @return array
*/ */
public function getDescendants(array $include, bool $with_self = false): array public function getDescendant(array $include, bool $with_self = false): array
{ {
$items = []; $items = [];
foreach ($include as $id) { foreach ($include as $id) {
@ -58,7 +58,7 @@ class Tree
foreach ($this->hashTree[$id]['children'] ?? [] as $item) { foreach ($this->hashTree[$id]['children'] ?? [] as $item) {
unset($item['children']); unset($item['children']);
$items[$item['id']] = $item; $items[$item['id']] = $item;
foreach ($this->getDescendants([$item['id']]) as $it) { foreach ($this->getDescendant([$item['id']]) as $it) {
$items[$it['id']] = $it; $items[$it['id']] = $it;
} }
} }