50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?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 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);
|
|
}
|
|
|
|
} |