refactor(dao): remove BaseAuth search dependency

Made-with: Cursor
This commit is contained in:
apple
2026-04-29 17:09:08 +08:00
parent bbeb8bc6b6
commit 083c51ed7e
3 changed files with 86 additions and 2 deletions

View File

@@ -0,0 +1,59 @@
<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace app\services\dao;
use think\helper\Str;
/**
* 本项目自有搜索条件构建器,不依赖 CRMEB 商业授权基础类。
*/
class SearchConditionBuilder
{
/**
* 根据模型搜索器和表字段拆分 withSearch 字段与普通 where 字段。
*
* @param array $whereKeys 请求条件 key 列表
* @param string|object $model 模型类名或模型实例
* @return array{0: array, 1: array}
* @throws \ReflectionException
*/
public function build(array $whereKeys, $model): array
{
$modelClass = is_object($model) ? $model::class : $model;
$reflection = new \ReflectionClass($modelClass);
$fields = $this->getTableFields($model);
$with = [];
$whereKey = [];
foreach ($whereKeys as $key) {
$key = (string)$key;
if ($key === 'timeKey') {
continue;
}
if ($reflection->hasMethod('search' . Str::studly($key) . 'Attr')) {
$with[] = $key;
continue;
}
if (!$fields || in_array($key, $fields, true)) {
$whereKey[] = $key;
}
}
return [$with, $whereKey];
}
protected function getTableFields($model): array
{
try {
$model = is_object($model) ? $model : new $model();
return $model->getTableFields();
} catch (\Throwable $e) {
return [];
}
}
}

View File

@@ -5,8 +5,8 @@
namespace crmeb\traits;
use app\services\dao\SearchConditionBuilder;
use app\dao\BaseDao;
use crmeb\basic\BaseAuth;
use crmeb\basic\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
@@ -28,7 +28,7 @@ trait SearchDaoTrait
*/
public function searchWhere(array $where, bool $authWhere = true)
{
[$with, $whereKey] = app()->make(BaseAuth::class)->________(array_keys($where), $this->setModel());
[$with, $whereKey] = app()->make(SearchConditionBuilder::class)->build(array_keys($where), $this->setModel());
$whereData = [];
foreach ($whereKey as $key) {
if (isset($where[$key]) && 'timeKey' !== $key) {