395 lines
9.3 KiB
PHP
395 lines
9.3 KiB
PHP
<?php
|
||
|
||
namespace plugin\admin\app\common;
|
||
|
||
use app\utils\Logger;
|
||
|
||
class AccessDataUtil
|
||
{
|
||
|
||
|
||
public static function admin_id(): ?int
|
||
{
|
||
return runCatching(fn() => admin()['id'], "无法获取到管理员登录信息")->getOrNull();
|
||
}
|
||
|
||
public static function admin_username(): ?string
|
||
{
|
||
return runCatching(fn() => admin()['username'], "无法获取到管理员登录信息")->getOrNull();
|
||
}
|
||
|
||
public static function admin_nickname(): ?string
|
||
{
|
||
return runCatching(fn() => admin()['nickname'], "无法获取到管理员登录信息")->getOrNull();
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取&设置权限树
|
||
*/
|
||
public static function data_permission_tree(?array $data = null): DataPermissionInterface
|
||
{
|
||
// 获取 session
|
||
try {
|
||
$data_permission_tree = session()("data_permission_tree");
|
||
if (!empty($data_permission_tree)) return new DataPermission($data_permission_tree);
|
||
} catch (\Exception $e) {
|
||
Logger::error("获取 session data_permission_tree key 失败",$e);
|
||
}
|
||
// 如果不存在就去数据库查表
|
||
|
||
return [];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 数据权限基础接口
|
||
* 定义所有权限类必须实现的核心方法
|
||
*/
|
||
interface DataPermissionInterface
|
||
{
|
||
/**
|
||
* 获取权限唯一标识ID
|
||
*/
|
||
public function id(): int;
|
||
|
||
/**
|
||
* 获取权限名称
|
||
*/
|
||
public function name(): string;
|
||
|
||
/**
|
||
* 转为array
|
||
* @return array
|
||
*/
|
||
public function toArray(): array;
|
||
}
|
||
|
||
/**
|
||
* 科室权限类(你需要的 DepartmentPxx)
|
||
* 最小权限单元:管理科室下的具体权限项(如查看、编辑、删除等)
|
||
*/
|
||
class DepartmentPermission implements DataPermissionInterface
|
||
{
|
||
// 科室ID
|
||
private int $deptId;
|
||
// 科室名称
|
||
private string $deptName;
|
||
|
||
public function __construct(int $deptId, string $deptName)
|
||
{
|
||
$this->deptId = $deptId;
|
||
$this->deptName = $deptName;
|
||
}
|
||
|
||
/**
|
||
* 实现接口:获取科室ID
|
||
*/
|
||
public function id(): int
|
||
{
|
||
return $this->deptId;
|
||
}
|
||
|
||
/**
|
||
* 实现接口:获取科室名称
|
||
*/
|
||
public function name(): string
|
||
{
|
||
return $this->deptName;
|
||
}
|
||
|
||
public function toArray(): array
|
||
{
|
||
$array = [];
|
||
$array['id'] = $this->deptId;
|
||
$array['name'] = $this->deptName;
|
||
return $array;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 医院权限类
|
||
* 内部管理:多个科室权限对象(DepartmentPermission)
|
||
*/
|
||
class HospitalPermission implements DataPermissionInterface
|
||
{
|
||
// 医院ID
|
||
private int $hospitalId;
|
||
// 医院名称
|
||
private string $hospitalName;
|
||
// 科室权限集合:科室ID => DepartmentPermission对象
|
||
private array $deptPermissions = [];
|
||
|
||
public function __construct(int $hospitalId, string $hospitalName)
|
||
{
|
||
$this->hospitalId = $hospitalId;
|
||
$this->hospitalName = $hospitalName;
|
||
}
|
||
|
||
/**
|
||
* 实现接口:获取医院ID
|
||
*/
|
||
public function id(): int
|
||
{
|
||
return $this->hospitalId;
|
||
}
|
||
|
||
/**
|
||
* 实现接口:获取医院名称
|
||
*/
|
||
public function name(): string
|
||
{
|
||
return $this->hospitalName;
|
||
}
|
||
|
||
/**
|
||
* 添加单个科室权限
|
||
*/
|
||
public function addDept(DepartmentPermission $deptPermission): void
|
||
{
|
||
$this->deptPermissions[$deptPermission->id()] = $deptPermission;
|
||
}
|
||
|
||
/**
|
||
* 批量添加科室权限
|
||
*/
|
||
public function addDepts(array $deptPermissions): void
|
||
{
|
||
foreach ($deptPermissions as $dept) {
|
||
if ($dept instanceof DepartmentPermission) {
|
||
$this->addDept($dept);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取单个科室权限
|
||
*/
|
||
public function getDept(int $deptId): ?DepartmentPermission
|
||
{
|
||
return $this->deptPermissions[$deptId] ?? null;
|
||
}
|
||
|
||
/**
|
||
* 判断是否包含某个科室
|
||
*/
|
||
public function hasDept(int $deptId): bool
|
||
{
|
||
return isset($this->deptPermissions[$deptId]);
|
||
}
|
||
|
||
/**
|
||
* 删除指定科室权限
|
||
*/
|
||
public function removeDept(int $deptId): void
|
||
{
|
||
unset($this->deptPermissions[$deptId]);
|
||
}
|
||
|
||
/**
|
||
* 获取所有科室权限
|
||
*/
|
||
public function getAllDepts(): array
|
||
{
|
||
return $this->deptPermissions;
|
||
}
|
||
|
||
/**
|
||
* 清空所有科室权限
|
||
*/
|
||
public function clearDepts(): void
|
||
{
|
||
$this->deptPermissions = [];
|
||
}
|
||
|
||
public function toArray(): array
|
||
{
|
||
$array = [];
|
||
$depts = [];
|
||
$array['id'] = $this->id();
|
||
$array['name'] = $this->name();
|
||
foreach ($this->deptPermissions as $deptPermission) {
|
||
$depts[] = $deptPermission->toArray();
|
||
}
|
||
$array['depts'] = $depts;
|
||
return $array;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 顶级数据权限类
|
||
*/
|
||
class DataPermission implements DataPermissionInterface
|
||
{
|
||
/**
|
||
* 从传入数组中获取的 ID 与名称
|
||
*/
|
||
private int $id;
|
||
private string $name;
|
||
|
||
/**
|
||
* 多医院集合:hospitalId => HospitalPermission
|
||
*/
|
||
private array $hospitalPermissions = [];
|
||
|
||
/**
|
||
* 构造方法:接收 toArray() 输出的权限数组,自动还原为多医院对象结构
|
||
* @param array $data_permission_tree 传入 DataPermission::toArray() 输出的数组
|
||
*/
|
||
public function __construct(array $data_permission_tree)
|
||
{
|
||
$this->clear();
|
||
|
||
// 读取传入的 id 和 name,不硬编码
|
||
$this->id = isset($data_permission_tree['id']) ? (int)$data_permission_tree['id'] : 0;
|
||
$this->name = isset($data_permission_tree['name']) ? (string)$data_permission_tree['name'] : '';
|
||
|
||
// 获取所有医院数组
|
||
$hospitalList = $data_permission_tree['hospitals'] ?? [];
|
||
if (!is_array($hospitalList) || empty($hospitalList)) {
|
||
return;
|
||
}
|
||
|
||
// 循环还原所有医院 + 下属科室
|
||
foreach ($hospitalList as $hospitalData) {
|
||
if (
|
||
!is_array($hospitalData)
|
||
|| empty($hospitalData['id'])
|
||
|| empty($hospitalData['name'])
|
||
) {
|
||
continue;
|
||
}
|
||
|
||
// 创建医院权限
|
||
$hospitalId = (int)$hospitalData['id'];
|
||
$hospitalName = (string)$hospitalData['name'];
|
||
$hospitalPermission = new HospitalPermission($hospitalId, $hospitalName);
|
||
|
||
// 添加下属科室
|
||
$deptList = $hospitalData['depts'] ?? [];
|
||
foreach ($deptList as $deptData) {
|
||
if (empty($deptData['id']) || empty($deptData['name'])) {
|
||
continue;
|
||
}
|
||
$deptPermission = new DepartmentPermission(
|
||
(int)$deptData['id'],
|
||
(string)$deptData['name']
|
||
);
|
||
$hospitalPermission->addDept($deptPermission);
|
||
}
|
||
|
||
// 存入多医院集合
|
||
$this->hospitalPermissions[$hospitalId] = $hospitalPermission;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 实现接口:获取顶级权限ID(来自传入数组)
|
||
*/
|
||
public function id(): int
|
||
{
|
||
return $this->id;
|
||
}
|
||
|
||
/**
|
||
* 实现接口:获取顶级权限名称(来自传入数组)
|
||
*/
|
||
public function name(): string
|
||
{
|
||
return $this->name;
|
||
}
|
||
|
||
/**
|
||
* 添加单个医院权限
|
||
*/
|
||
public function addHospital(HospitalPermission $hospitalPermission): void
|
||
{
|
||
$this->hospitalPermissions[$hospitalPermission->id()] = $hospitalPermission;
|
||
}
|
||
|
||
/**
|
||
* 批量添加医院
|
||
*/
|
||
public function addHospitals(array $hospitalPermissions): void
|
||
{
|
||
foreach ($hospitalPermissions as $hospital) {
|
||
if ($hospital instanceof HospitalPermission) {
|
||
$this->addHospital($hospital);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取单个医院
|
||
*/
|
||
public function getHospital(int $hospitalId): ?HospitalPermission
|
||
{
|
||
return $this->hospitalPermissions[$hospitalId] ?? null;
|
||
}
|
||
|
||
/**
|
||
* 判断是否包含某个医院
|
||
*/
|
||
public function hasHospital(int $hospitalId): bool
|
||
{
|
||
return isset($this->hospitalPermissions[$hospitalId]);
|
||
}
|
||
|
||
/**
|
||
* 删除指定医院
|
||
*/
|
||
public function removeHospital(int $hospitalId): void
|
||
{
|
||
unset($this->hospitalPermissions[$hospitalId]);
|
||
}
|
||
|
||
/**
|
||
* 获取所有医院
|
||
*/
|
||
public function getAllHospitals(): array
|
||
{
|
||
return $this->hospitalPermissions;
|
||
}
|
||
|
||
/**
|
||
* 获取所有医院ID
|
||
*/
|
||
public function getAllHospitalIds(): array
|
||
{
|
||
return array_keys($this->hospitalPermissions);
|
||
}
|
||
|
||
/**
|
||
* 判断是否有医院权限
|
||
*/
|
||
public function has(): bool
|
||
{
|
||
return !empty($this->hospitalPermissions);
|
||
}
|
||
|
||
/**
|
||
* 清空所有医院权限
|
||
*/
|
||
public function clear(): void
|
||
{
|
||
$this->hospitalPermissions = [];
|
||
}
|
||
|
||
/**
|
||
* 转为array(完美支持构造方法还原)
|
||
*/
|
||
public function toArray(): array
|
||
{
|
||
$array = [
|
||
'id' => $this->id(),
|
||
'name' => $this->name(),
|
||
'hospitals' => []
|
||
];
|
||
|
||
foreach ($this->hospitalPermissions as $hospital) {
|
||
$array['hospitals'][] = $hospital->toArray();
|
||
}
|
||
|
||
return $array;
|
||
}
|
||
} |