add:新增Collection toTree方法

This commit is contained in:
JaguarJack 2020-10-21 08:12:07 +08:00
parent 538006c3c6
commit 9baadccfbb
7 changed files with 43 additions and 8 deletions

View File

@ -27,7 +27,7 @@ class Department extends CatchController
*/ */
public function index(): \think\response\Json public function index(): \think\response\Json
{ {
return CatchResponse::success(Tree::done($this->department->getList())); return CatchResponse::success($this->department->getList());
} }
/** /**

View File

@ -47,9 +47,9 @@ class Permission extends CatchController
// 子节点的 key // 子节点的 key
$children = $request->param('actionList') ?? 'children'; $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']] ?? []; $item[$children] = $buttonList[$item['id']] ?? [];
})->toArray())); })->toTree());
} }
/** /**

View File

@ -27,7 +27,7 @@ class Role extends CatchController
*/ */
public function index() public function index()
{ {
return CatchResponse::success(Tree::done($this->role->getList())); return CatchResponse::success($this->role->getList());
} }
/** /**

View File

@ -3,6 +3,7 @@ namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\DepartmentSearch; use catchAdmin\permissions\model\search\DepartmentSearch;
use catcher\base\CatchModel; use catcher\base\CatchModel;
use think\db\exception\DbException;
class Department extends CatchModel class Department extends CatchModel
{ {
@ -29,14 +30,13 @@ class Department extends CatchModel
* 列表数据 * 列表数据
* *
* @time 2020年01月09日 * @time 2020年01月09日
* @param $params
* @return array * @return array
* @throws \think\db\exception\DbException * @throws DbException
*/ */
public function getList(): array public function getList(): array
{ {
return $this->catchSearch() return $this->catchSearch()
->catchOrder() ->catchOrder()
->select()->toArray(); ->select()->toTree();
} }
} }

View File

@ -37,7 +37,7 @@ class Roles extends CatchModel
return $this->catchSearch() return $this->catchSearch()
->order('id', 'desc') ->order('id', 'desc')
->select() ->select()
->toArray(); ->toTree();
} }
/** /**

View File

@ -0,0 +1,12 @@
<?php
namespace catcher;
use think\model\Collection;
class CatchModelCollection extends Collection
{
public function toTree($pid = 0, $pidField = 'parent_id', $children = 'children')
{
return Tree::done($this->items, $pid, $pidField, $children);
}
}

View File

@ -2,7 +2,9 @@
namespace catcher\traits\db; namespace catcher\traits\db;
use catcher\CatchModelCollection;
use catcher\Utils; use catcher\Utils;
use think\Collection;
trait BaseOptionsTrait trait BaseOptionsTrait
{ {
@ -186,4 +188,25 @@ trait BaseOptionsTrait
return $model->save(); 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;
}
} }