'view', 'patients' => 'view', 'staff' => 'view', 'teamoverview' => 'view', 'staffoverview' => 'view', 'staffcases' => 'view', 'staffissues' => 'view', 'staffdailycontacts' => 'view', 'alerts' => 'view', 'wecomdirectory' => 'view', 'wecomchats' => 'view', 'wecommessages' => 'view', 'context' => 'view', 'job' => 'view', 'jobs' => 'view', 'actions' => 'view', 'knowledge' => 'view', 'analyze' => 'analyze', 'analyzestaff' => 'analyze', 'draftreply' => 'draftReply', 'record' => 'record', 'adoptaction' => 'adoptAction', 'completeaction' => 'completeAction', 'reviewquality' => 'reviewQuality', 'reviewalert' => 'reviewQuality', 'saveknowledge' => 'manageKnowledge', 'publishknowledge' => 'manageKnowledge', ]; if (!isset($actions[strtolower($action)])) { throw new DomainException('不支持的医助服务操作'); } return $actions[strtolower($action)]; } public static function actor(int $adminId, string $permission = 'view'): array { $row = Db::name('admin')->where('id', $adminId)->whereNull('delete_time')->where('disable', 0)->lock(true)->find(); if ($adminId <= 0 || !$row) { throw new DomainException('账号已停用或无权访问医助服务'); } $roles = array_map('intval', Db::name('admin_role')->where('admin_id', $adminId)->lock(true)->column('role_id')); // 已删除角色的遗留菜单关系不能继续授权。 $roles = $roles ? array_map('intval', Db::name('system_role')->whereIn('id', $roles) ->whereNull('delete_time')->lock(true)->column('id')) : []; sort($roles); $menus = $roles ? Db::name('system_role_menu')->whereIn('role_id', $roles)->lock(true)->column('menu_id') : []; $perms = $menus ? Db::name('system_menu')->whereIn('id', array_unique($menus)) ->where('is_disable', 0)->lock(true)->column('perms') : []; $perms = array_map('strtolower', $perms); $permissions = array_values(array_filter(self::PERMISSIONS, static fn (string $p): bool => (int) $row['root'] === 1 || in_array(strtolower('tcm.assistantService/' . $p), $perms, true))); $actor = [ 'id' => $adminId, 'admin_id' => $adminId, 'root' => (int) $row['root'], 'name' => (string) ($row['name'] ?? ''), 'role_id' => $roles, 'dept_id' => array_map('intval', Db::name('admin_dept')->where('admin_id', $adminId)->lock(true)->column('dept_id')), 'permissions' => $permissions, ]; if (!self::hasPermission($actor, 'view') || !self::hasPermission($actor, $permission)) { throw new DomainException('无权执行此医助服务操作'); } return $actor; } public static function hasPermission(array $actor, string $permission): bool { return in_array($permission, self::PERMISSIONS, true) && in_array($permission, $actor['permissions'] ?? [], true); } /** 员工域与有效医助角色取交集;不借用目标账号的菜单权限。 */ public static function assistantRows(int $adminId, ?array $actor = null): array { $actor = $actor ?? self::actor($adminId); $visible = self::visibleStaffIds($adminId, $actor); $roles = Db::name('system_role')->whereNull('delete_time')->lock(true)->select()->toArray(); $assistantRoles = []; $managementRoles = [3, 7, 8]; foreach ($roles as $role) { if ((int) $role['id'] === 2 || trim((string) $role['name']) === '医助') { $assistantRoles[] = (int) $role['id']; } } if (!$assistantRoles) { return []; } $ids = self::ids(Db::name('admin_role')->whereIn('role_id', $assistantRoles)->lock(true)->column('admin_id')); $managerIds = self::ids(Db::name('admin_role')->whereIn('role_id', array_intersect($managementRoles, array_column($roles, 'id')) ?: [0]) ->lock(true)->column('admin_id')); $ids = array_values(array_diff($ids, $managerIds)); if ($visible !== null) { $ids = array_values(array_intersect($ids, $visible)); } if (!$ids) { return []; } $rows = Db::name('admin')->whereIn('id', $ids)->where('root', 0)->where('disable', 0)->whereNull('delete_time') ->field('id,name')->order('id', 'asc')->lock(true)->select()->toArray(); return array_map(static fn (array $row): array => ['id' => (int) $row['id'], 'name' => (string) $row['name'], 'is_self' => (int) $row['id'] === $adminId], $rows); } public static function assistant(int $assistantId, int $adminId): array { foreach (self::assistantRows($adminId) as $row) { if ($assistantId > 0 && $row['id'] === $assistantId) { return $row; } } throw new DomainException('医助不存在、已停用或不在当前员工范围'); } public static function diagnosis(int $diagnosisId, int $adminId, string $permission = 'view', bool $lock = false): array { // 锁等待后再读取身份,不能用等待前的 root / 菜单快照授权。 $row = Db::name('tcm_diagnosis')->where('id', $diagnosisId)->whereNull('delete_time') ->where('status', 1)->lock($lock)->find(); $actor = self::actor($adminId, $permission); if ($diagnosisId <= 0 || !$row) { throw new DomainException('患者不存在或已不在当前可见范围'); } $query = self::patientQuery($adminId, $actor)->where('d.id', $diagnosisId); $authorized = $query->lock(true)->find(); if (!$authorized) { throw new DomainException('患者不存在或已不在当前可见范围'); } return $authorized; } public static function patientQuery(int $adminId, array $actor) { $query = Db::name('tcm_diagnosis')->alias('d')->whereNull('d.delete_time')->where('d.status', 1); self::applyScope($query, $adminId, $actor); return $query; } public static function applyScope($query, int $adminId, array $actor): void { self::assertRoleSnapshot($adminId, $actor); if (array_intersect($actor['role_id'], [3, 7, 8])) { self::visibleStaffIds($adminId, $actor); } $scopeRoles = $actor['role_id']; // 本模块也承认现存的命名医助角色,使用既有医助患者规则,不更改全站角色定义。 if ($scopeRoles && !in_array(2, $scopeRoles, true) && Db::name('system_role')->whereIn('id', $scopeRoles)->whereNull('delete_time')->where('name', '医助')->lock(true)->find()) { $scopeRoles[] = 2; } MyPatientLogic::applyScope($query, $adminId, $actor, $scopeRoles); } /** 一线员工固定本人;主管按 DataScope,不能因为普通医生的 ALL 配置扩散到其他员工。 */ public static function visibleStaffIds(int $adminId, array $actor): ?array { if ((int) $actor['root'] === 1) { return null; } self::assertRoleSnapshot($adminId, $actor); if (!array_intersect($actor['role_id'], [3, 7, 8])) { return [$adminId]; } $scope = DataScopeService::getEffectiveScope($actor); $ids = DataScopeService::getVisibleAdminIds($adminId, $actor); if ($ids === null) { return null; } $ids = self::ids($ids); if ($scope === DataScopeService::SCOPE_SELF) { return $ids; } $departments = self::ids(Db::name('admin_dept')->where('admin_id', $adminId)->lock(true)->column('dept_id')); $snapshotDepartments = self::ids(Db::name('admin_dept')->where('admin_id', $adminId)->column('dept_id')); if ($departments !== $snapshotDepartments) { self::scopeChanged(); } if (!$departments) { return $ids; } $target = $departments; if ($scope === DataScopeService::SCOPE_DEPT_AND_CHILD) { // 旧 MyPatient/DataScope 内部普通读可能使用已存在的 RR 快照。 // 只在它与锁定的当前关系一致时复用其结论,不复制另一套范围规则。 $fresh = Db::name('dept')->whereNull('delete_time')->field('id,pid')->order('id', 'asc')->lock(true)->select()->toArray(); $snapshot = Db::name('dept')->whereNull('delete_time')->field('id,pid')->order('id', 'asc')->select()->toArray(); if ($fresh != $snapshot) { self::scopeChanged(); } $target = []; foreach ($departments as $dept) { $target = array_merge($target, DeptLogic::getSelfAndDescendantIds($dept)); } } $freshIds = self::ids(array_merge([$adminId], Db::name('admin_dept')->whereIn('dept_id', self::ids($target))->lock(true)->column('admin_id'))); if ($freshIds !== $ids) { self::scopeChanged(); } return $ids; } /** 当前事务已有旧快照时宁可要求重试,绝不借旧角色扩大患者域。 */ private static function assertRoleSnapshot(int $adminId, array $actor): void { if ((int) $actor['root'] === 1) { return; } $roles = self::ids($actor['role_id']); $snapshotRoleIds = Db::name('admin_role')->where('admin_id', $adminId)->column('role_id'); $snapshotRoles = $snapshotRoleIds ? self::ids(Db::name('system_role')->whereIn('id', $snapshotRoleIds) ->whereNull('delete_time')->column('id')) : []; if ($snapshotRoles !== $roles) { self::scopeChanged(); } if (!$roles) { return; } $fresh = Db::name('system_role')->whereIn('id', $roles)->whereNull('delete_time') ->field('id,name,data_scope')->order('id', 'asc')->lock(true)->select()->toArray(); $snapshot = Db::name('system_role')->whereIn('id', $roles)->whereNull('delete_time') ->field('id,name,data_scope')->order('id', 'asc')->select()->toArray(); if ($fresh != $snapshot) { self::scopeChanged(); } } private static function ids(array $ids): array { $ids = array_values(array_unique(array_filter(array_map('intval', $ids), static fn (int $id): bool => $id > 0))); sort($ids); return $ids; } private static function scopeChanged(): void { throw new DomainException('数据权限已变化,请刷新后重试'); } }