Files
dsserver/plugin/admin/app/model/Base.php
T
zimoyin 673c83109f init
2026-04-03 15:34:04 +08:00

89 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace plugin\admin\app\model;
use app\utils\Logger;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Builder;
use plugin\admin\app\common\DataPermissionService;
use support\Db;
use support\Model;
/**
* @method static \Illuminate\Database\Eloquent\Builder|static withDataPermission()
*/
class Base extends Model
{
/**
* @var string
*/
protected $connection = 'plugin.admin.mysql';
/**
* --------------------------
* 【核心配置】权限规则配置
* 新增规则只需在这里加一项,无需改下面的逻辑
* --------------------------
*/
protected function getPermissionRules(): array
{
return [
// 规则1:医院权限
'hospital' => [
'table' => 'opm_mw_hospital', // 表名
'admin_attr' => 'hospitals', // 用户属性里的键($admin['hospitals']
'permission_field'=> 'id', // 表中用于权限过滤的字段
'related_field' => null, // 关联上级权限的字段(如科室关联医院的organ_id)
'related_rule' => null, // 关联的上级规则key(对应上面的'hospital'
],
// 规则2:科室权限
'department' => [
'table' => 'opm_mw_department',
'admin_attr' => 'departments',
'permission_field'=> 'id',
'related_field' => 'organ_id', // 科室通过organ_id关联医院
'related_rule' => 'hospital', // 关联上级规则:医院
],
// 规则3:数据权限
// 这个需要绑定 医院的.id
// 这个需要绑定 科室的.id
'data' => [
'table' => 'opm_mw_info_data',
'admin_attr' => 'data',
'permission_field'=> 'id',
'related_field' => null,
'related_rule' => null,
],
];
}
/**
* 格式化日期
*/
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d H:i:s');
}
public function scopeWithDataPermission(Builder $query): Builder
{
$admin = runCatching(fn() => admin(), "无法获取登录状态")->getOrDefault([]);
// 超管判断(可选,也可以在规则里配置*)
$isSuper = true;
foreach (['hospitals', 'departments'] as $attr) {
if (($admin[$attr] ?? '') !== '*') {
$isSuper = false;
break;
}
}
if ($isSuper) return $query;
// 使用服务类应用权限
$service = new DataPermissionService($admin);
return $service->apply($query);
}
}