From effde58a5392bf64307490243849562e3e62b3be Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Wed, 1 Apr 2026 14:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DCURD=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E5=85=B3=E8=81=94=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/library/traits/Backend.php | 35 +++++++++++++++++++++++++++- app/common/controller/Backend.php | 5 ++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/admin/library/traits/Backend.php b/app/admin/library/traits/Backend.php index 5f2fb28..5eae2a3 100644 --- a/app/admin/library/traits/Backend.php +++ b/app/admin/library/traits/Backend.php @@ -301,7 +301,40 @@ trait Backend /** * 加载为 select(远程下拉选择框)数据,子类可覆盖 */ - protected function _select(): void + protected function _select(): Response { + if (empty($this->model)) { + return $this->success('', [ + 'list' => [], + 'total' => 0, + ]); + } + + $pk = $this->model->getPk(); + + // 远程下拉只要求包含主键与可显示字段;这里尽量返回主键 + quickSearch 字段,避免全量字段带来性能问题 + $fields = [$pk]; + $quickSearchArr = is_array($this->quickSearchField) ? $this->quickSearchField : explode(',', (string) $this->quickSearchField); + foreach ($quickSearchArr as $f) { + $f = trim((string) $f); + if ($f === '') continue; + $f = str_contains($f, '.') ? substr($f, strrpos($f, '.') + 1) : $f; + if ($f !== '' && !in_array($f, $fields, true)) { + $fields[] = $f; + } + } + + list($where, $alias, $limit, $order) = $this->queryBuilder(); + $res = $this->model + ->field($fields) + ->alias($alias) + ->where($where) + ->order($order) + ->paginate($limit); + + return $this->success('', [ + 'list' => $res->items(), + 'total' => $res->total(), + ]); } } diff --git a/app/common/controller/Backend.php b/app/common/controller/Backend.php index 6e7c365..e2d8a4d 100644 --- a/app/common/controller/Backend.php +++ b/app/common/controller/Backend.php @@ -214,9 +214,8 @@ class Backend extends Api public function select(WebmanRequest $request): Response { $response = $this->initializeBackend($request); - if ($response !== null) return $response; - $this->_select(); - return $this->success(); + if ($response instanceof Response) return $response; + return $this->_select(); } /**