whereNull('delete_time') ->column('data_scope'); $scopes = array_values(array_filter(array_map('intval', $scopes), static function (int $v): bool { return $v >= self::SCOPE_ALL && $v <= self::SCOPE_SELF; })); if ($scopes === []) { return self::SCOPE_ALL; } return (int) min($scopes); } /** * 可见 admin id 集合;null = 不过滤(ALL) * * @return array|null */ public static function getVisibleAdminIds(int $adminId, array $adminInfo): ?array { $scope = self::getEffectiveScope($adminInfo); if ($scope === self::SCOPE_ALL) { return null; } if ($scope === self::SCOPE_SELF) { return $adminId > 0 ? [$adminId] : []; } $myDeptIds = AdminDept::where('admin_id', $adminId)->column('dept_id'); $myDeptIds = array_values(array_filter(array_map('intval', $myDeptIds), static function (int $v): bool { return $v > 0; })); if ($myDeptIds === []) { $fallback = (bool) Config::get('project.data_scope.no_dept_fallback_self', true); return $fallback ? [$adminId] : []; } $targetDeptIds = []; if ($scope === self::SCOPE_DEPT) { $targetDeptIds = $myDeptIds; } else { foreach ($myDeptIds as $did) { foreach (DeptLogic::getSelfAndDescendantIds($did) as $id) { $id = (int) $id; if ($id > 0) { $targetDeptIds[$id] = true; } } } $targetDeptIds = array_keys($targetDeptIds); } if ($targetDeptIds === []) { return $adminId > 0 ? [$adminId] : []; } $ids = AdminDept::whereIn('dept_id', $targetDeptIds)->column('admin_id'); $ids = array_values(array_unique(array_filter(array_map('intval', $ids), static function (int $v): bool { return $v > 0; }))); if ($adminId > 0 && !in_array($adminId, $ids, true)) { $ids[] = $adminId; } return $ids; } public static function isEnabled(): bool { return (bool) Config::get('project.data_scope.enabled', true); } public static function isAll(array $adminInfo): bool { return self::getEffectiveScope($adminInfo) === self::SCOPE_ALL; } /** * 文字描述(日志 / 接口返回可选使用) */ public static function scopeLabel(int $scope): string { return [ self::SCOPE_ALL => '全部', self::SCOPE_DEPT_AND_CHILD => '本部门及下级', self::SCOPE_DEPT => '仅本部门', self::SCOPE_SELF => '仅本人', ][$scope] ?? '全部'; } }