From ea0b6f1f65648d9ca3c400b87f6135acba88f91e Mon Sep 17 00:00:00 2001 From: lewis <604446095@qq.com> Date: Mon, 14 Jul 2025 12:16:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=95=86=E5=93=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProductCategoryController.php | 9 +- app/admin/lists/ProductCategoryLists.php | 132 +++++++++--------- app/admin/logic/ProductCategoryLogic.php | 10 +- app/functions.php | 7 +- 4 files changed, 88 insertions(+), 70 deletions(-) diff --git a/app/admin/controller/ProductCategoryController.php b/app/admin/controller/ProductCategoryController.php index c957c65..7774303 100644 --- a/app/admin/controller/ProductCategoryController.php +++ b/app/admin/controller/ProductCategoryController.php @@ -7,6 +7,7 @@ use app\admin\controller\BaseAdminController; use app\admin\lists\ProductCategoryLists; use app\admin\logic\ProductCategoryLogic; use app\admin\validate\ProductCategoryValidate; +use support\Request; /** @@ -91,5 +92,11 @@ class ProductCategoryController extends BaseAdminController return $this->data($result); } + public function all(Request $request) + { + $params = $request->get(); + $result = ProductCategoryLogic::all($params); + return $this->data($result); + } -} \ No newline at end of file +} diff --git a/app/admin/lists/ProductCategoryLists.php b/app/admin/lists/ProductCategoryLists.php index 946d64b..43b2135 100644 --- a/app/admin/lists/ProductCategoryLists.php +++ b/app/admin/lists/ProductCategoryLists.php @@ -1,65 +1,67 @@ - ['pid', 'name'], - ]; - } - - - /** - * @notes 获取列表 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function lists(): array - { - return ProductCategory::where($this->searchWhere) - ->field(['id', 'pid', 'name', 'create_time']) - ->limit($this->limitOffset, $this->limitLength) - ->order(['id' => 'desc']) - ->select() - ->toArray(); - } - - - /** - * @notes 获取数量 - * @return int - * @author likeadmin - * @date 2025/07/11 11:41 - */ - public function count(): int - { - return ProductCategory::where($this->searchWhere)->count(); - } - -} \ No newline at end of file + ['pid', 'name'], + + ]; + } + + + /** + * @notes 获取列表 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function lists(): array + { + $lists = ProductCategory::where($this->searchWhere) + ->field(['id', 'pid', 'name', 'create_time']) + ->limit($this->limitOffset, $this->limitLength) + ->order(['id' => 'desc']) + ->select() + ->toArray(); + return linear_to_tree($lists, 'children'); + } + + + /** + * @notes 获取数量 + * @return int + * @author likeadmin + * @date 2025/07/11 11:41 + */ + public function count(): int + { + return ProductCategory::where($this->searchWhere)->count(); + } + +} diff --git a/app/admin/logic/ProductCategoryLogic.php b/app/admin/logic/ProductCategoryLogic.php index 4ecf897..c02c9e2 100644 --- a/app/admin/logic/ProductCategoryLogic.php +++ b/app/admin/logic/ProductCategoryLogic.php @@ -93,4 +93,12 @@ class ProductCategoryLogic extends BaseLogic { return ProductCategory::findOrEmpty($params['id'])->toArray(); } -} \ No newline at end of file + + public static function all($params) + { + $query = ProductCategory::field(['id', 'pid', 'name', 'create_time']); + $list = $query->order(['id' => 'desc'])->select()->toArray(); + return linear_to_tree($list, 'children'); + } + +} diff --git a/app/functions.php b/app/functions.php index bfd66a8..24394e7 100644 --- a/app/functions.php +++ b/app/functions.php @@ -94,13 +94,14 @@ function create_password(string $plaintext, string $salt) : string * @param int $parent_id 此值请勿给参数 * @return array */ -function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0) +function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0, $level = 0) { $tree = []; foreach ($data as $row) { + $row['level'] = $level; if ($row[$parent_id_name] == $parent_id) { $temp = $row; - $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]); + $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name], $level + 1); if ($child) { $temp[$sub_key_name] = $child; } @@ -206,4 +207,4 @@ function get_file_domain($content) $preg = '/()/is'; $fileUrl = FileService::getFileUrl(); return preg_replace($preg, "\${1}$fileUrl\${2}\${3}", $content); -} \ No newline at end of file +}