From 9baadccfbbcb4de61fd8d5ee2a3202d6cd303d9b Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Wed, 21 Oct 2020 08:12:07 +0800 Subject: [PATCH] =?UTF-8?q?add:=E6=96=B0=E5=A2=9ECollection=20toTree?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/controller/Department.php | 2 +- catch/permissions/controller/Permission.php | 4 ++-- catch/permissions/controller/Role.php | 2 +- catch/permissions/model/Department.php | 6 ++--- catch/permissions/model/Roles.php | 2 +- extend/catcher/CatchModelCollection.php | 12 ++++++++++ extend/catcher/traits/db/BaseOptionsTrait.php | 23 +++++++++++++++++++ 7 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 extend/catcher/CatchModelCollection.php diff --git a/catch/permissions/controller/Department.php b/catch/permissions/controller/Department.php index 67db5bb..04c7a31 100644 --- a/catch/permissions/controller/Department.php +++ b/catch/permissions/controller/Department.php @@ -27,7 +27,7 @@ class Department extends CatchController */ public function index(): \think\response\Json { - return CatchResponse::success(Tree::done($this->department->getList())); + return CatchResponse::success($this->department->getList()); } /** diff --git a/catch/permissions/controller/Permission.php b/catch/permissions/controller/Permission.php index b249efd..8c2845b 100644 --- a/catch/permissions/controller/Permission.php +++ b/catch/permissions/controller/Permission.php @@ -47,9 +47,9 @@ class Permission extends CatchController // 子节点的 key $children = $request->param('actionList') ?? 'children'; // 返回树结构 - return CatchResponse::success(Tree::done($menuList->each(function (&$item) use ($buttonList, $children){ + return CatchResponse::success($menuList->each(function (&$item) use ($buttonList, $children){ $item[$children] = $buttonList[$item['id']] ?? []; - })->toArray())); + })->toTree()); } /** diff --git a/catch/permissions/controller/Role.php b/catch/permissions/controller/Role.php index 1e52c28..6f74676 100644 --- a/catch/permissions/controller/Role.php +++ b/catch/permissions/controller/Role.php @@ -27,7 +27,7 @@ class Role extends CatchController */ public function index() { - return CatchResponse::success(Tree::done($this->role->getList())); + return CatchResponse::success($this->role->getList()); } /** diff --git a/catch/permissions/model/Department.php b/catch/permissions/model/Department.php index 7c84adc..c2a87cb 100644 --- a/catch/permissions/model/Department.php +++ b/catch/permissions/model/Department.php @@ -3,6 +3,7 @@ namespace catchAdmin\permissions\model; use catchAdmin\permissions\model\search\DepartmentSearch; use catcher\base\CatchModel; +use think\db\exception\DbException; class Department extends CatchModel { @@ -29,14 +30,13 @@ class Department extends CatchModel * 列表数据 * * @time 2020年01月09日 - * @param $params * @return array - * @throws \think\db\exception\DbException + * @throws DbException */ public function getList(): array { return $this->catchSearch() ->catchOrder() - ->select()->toArray(); + ->select()->toTree(); } } diff --git a/catch/permissions/model/Roles.php b/catch/permissions/model/Roles.php index 85350d3..21fb2b3 100644 --- a/catch/permissions/model/Roles.php +++ b/catch/permissions/model/Roles.php @@ -37,7 +37,7 @@ class Roles extends CatchModel return $this->catchSearch() ->order('id', 'desc') ->select() - ->toArray(); + ->toTree(); } /** diff --git a/extend/catcher/CatchModelCollection.php b/extend/catcher/CatchModelCollection.php new file mode 100644 index 0000000..05751de --- /dev/null +++ b/extend/catcher/CatchModelCollection.php @@ -0,0 +1,12 @@ +items, $pid, $pidField, $children); + } +} \ No newline at end of file diff --git a/extend/catcher/traits/db/BaseOptionsTrait.php b/extend/catcher/traits/db/BaseOptionsTrait.php index e2e4183..633d891 100644 --- a/extend/catcher/traits/db/BaseOptionsTrait.php +++ b/extend/catcher/traits/db/BaseOptionsTrait.php @@ -2,7 +2,9 @@ namespace catcher\traits\db; +use catcher\CatchModelCollection; use catcher\Utils; +use think\Collection; trait BaseOptionsTrait { @@ -186,4 +188,25 @@ trait BaseOptionsTrait return $model->save(); } + + /** + * rewrite collection + * + * @time 2020年10月20日 + * @param array|iterable $collection + * @param string|null $resultSetType + * @return CatchModelCollection|mixed + */ + public function toCollection(iterable $collection = [], string $resultSetType = null): Collection + { + $resultSetType = $resultSetType ?: $this->resultSetType; + + if ($resultSetType && false !== strpos($resultSetType, '\\')) { + $collection = new $resultSetType($collection); + } else { + $collection = new CatchModelCollection($collection); + } + + return $collection; + } }