更新
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\lists\Traits;
|
||||
|
||||
use app\common\service\DataScope\DataScopeService;
|
||||
use think\db\Query;
|
||||
|
||||
/**
|
||||
* 列表按「数据范围」过滤。
|
||||
*
|
||||
* 使用前置条件:宿主类须通过 BaseAdminDataLists 获得 $this->adminId 与 $this->adminInfo。
|
||||
*
|
||||
* 三种用法:
|
||||
* - applyDataScopeByOwner($q, 'creator_id') 直接按某列 IN
|
||||
* - applyDataScopeByOwnerColumns($q, ['creator_id', 'doctor_id']) 多列 OR
|
||||
* - applyDataScopeByExists($q, $sqlTemplate, 'owner_expr') 通过 exists 子查询(跨表)
|
||||
*/
|
||||
trait HasDataScopeFilter
|
||||
{
|
||||
/**
|
||||
* 单列过滤。null 表示豁免(ALL)。
|
||||
*/
|
||||
protected function applyDataScopeByOwner($query, string $ownerField): bool
|
||||
{
|
||||
if (!$this->dataScopeShouldApply()) {
|
||||
return false;
|
||||
}
|
||||
$ids = $this->getDataScopeVisibleAdminIds();
|
||||
if ($ids === null) {
|
||||
return false;
|
||||
}
|
||||
if ($ids === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return true;
|
||||
}
|
||||
$query->whereIn($ownerField, $ids);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多列 OR 过滤(任意属主列命中即可)。
|
||||
*
|
||||
* @param string[] $ownerFields
|
||||
*/
|
||||
protected function applyDataScopeByOwnerColumns($query, array $ownerFields): bool
|
||||
{
|
||||
if (!$this->dataScopeShouldApply()) {
|
||||
return false;
|
||||
}
|
||||
$ids = $this->getDataScopeVisibleAdminIds();
|
||||
if ($ids === null) {
|
||||
return false;
|
||||
}
|
||||
if ($ids === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return true;
|
||||
}
|
||||
$query->where(function ($q) use ($ownerFields, $ids) {
|
||||
$first = true;
|
||||
foreach ($ownerFields as $f) {
|
||||
if ($first) {
|
||||
$q->whereIn($f, $ids);
|
||||
$first = false;
|
||||
} else {
|
||||
$q->whereOr(function ($qq) use ($f, $ids) {
|
||||
$qq->whereIn($f, $ids);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* exists 子查询方式。
|
||||
* $subSqlTemplate 内部可使用占位符 `__OWNER_IDS__`,将被替换成逗号分隔的整数列表。
|
||||
*/
|
||||
protected function applyDataScopeByExistsSql($query, string $subSqlTemplate): bool
|
||||
{
|
||||
if (!$this->dataScopeShouldApply()) {
|
||||
return false;
|
||||
}
|
||||
$ids = $this->getDataScopeVisibleAdminIds();
|
||||
if ($ids === null) {
|
||||
return false;
|
||||
}
|
||||
if ($ids === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return true;
|
||||
}
|
||||
$inList = implode(',', $ids);
|
||||
$sql = str_replace('__OWNER_IDS__', $inList, $subSqlTemplate);
|
||||
$query->whereExists($sql);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 可见 admin id;null = 全部。
|
||||
*
|
||||
* @return array<int>|null
|
||||
*/
|
||||
protected function getDataScopeVisibleAdminIds(): ?array
|
||||
{
|
||||
$adminId = property_exists($this, 'adminId') ? (int) $this->adminId : 0;
|
||||
$adminInfo = property_exists($this, 'adminInfo') && is_array($this->adminInfo) ? $this->adminInfo : [];
|
||||
|
||||
return DataScopeService::getVisibleAdminIds($adminId, $adminInfo);
|
||||
}
|
||||
|
||||
protected function dataScopeShouldApply(): bool
|
||||
{
|
||||
return DataScopeService::isEnabled();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user