This commit is contained in:
Your Name
2026-05-05 14:51:35 +08:00
parent 40eec808ef
commit 3ef10846b4
10 changed files with 492 additions and 57 deletions
@@ -18,6 +18,7 @@ use app\common\logic\BaseLogic;
use app\common\model\auth\Admin;
use app\common\model\auth\AdminDept;
use app\common\model\dept\Dept;
use app\common\service\DataScope\DataScopeService;
/**
@@ -284,6 +285,60 @@ class DeptLogic extends BaseLogic
return self::getTree($data, $pid);
}
/**
* 部门树(与 getAllData 同结构),按当前管理员角色数据权限收窄可选节点;
* 保留必选祖先节点以便树形展示(与业绩看板 deptOptions 的 allowed 集合一致)。
*
* @param array<string, mixed> $adminInfo BaseAdminController::$adminInfo 形态(须含 root、role_id 等)
*
* @return array<int, array<string, mixed>>
*/
public static function getAllDataScoped(int $adminId, array $adminInfo): array
{
$full = self::getAllData();
if ($full === []) {
return [];
}
if ($adminId <= 0 || !DataScopeService::isEnabled()) {
return $full;
}
$allowed = DataScopeService::getAllowedDeptIdSet($adminId, $adminInfo);
if ($allowed === null) {
return $full;
}
if ($allowed === []) {
return [];
}
return self::filterDeptTreeByAllowedIds($full, $allowed);
}
/**
* @param array<int, array<string, mixed>> $nodes
* @param array<int, true> $allowedIdMap
*
* @return array<int, array<string, mixed>>
*/
private static function filterDeptTreeByAllowedIds(array $nodes, array $allowedIdMap): array
{
$out = [];
foreach ($nodes as $node) {
$id = (int) ($node['id'] ?? 0);
$rawChildren = $node['children'] ?? [];
$children = \is_array($rawChildren) && $rawChildren !== []
? self::filterDeptTreeByAllowedIds($rawChildren, $allowedIdMap)
: [];
$inAllowed = isset($allowedIdMap[$id]);
if ($inAllowed || $children !== []) {
$row = $node;
$row['children'] = $children;
$out[] = $row;
}
}
return $out;
}
/**
* 指定部门及其全部下级部门 id(含自身)。筛选时选父级可匹配子级下成员(admin_dept.dept_id)。
*