From 85b7365a01ca5489beccaebb4e2dd0785d13ade6 Mon Sep 17 00:00:00 2001 From: liapples Date: Tue, 26 Oct 2021 18:21:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A7=E5=93=81=E5=88=86=E7=B1=BB=E7=AD=9B?= =?UTF-8?q?=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/ProductController.php | 27 +++++++++++++++++++ .../Controllers/ProductController.php | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/app/Admin/Controllers/ProductController.php b/app/Admin/Controllers/ProductController.php index 4ff1635..54cc54c 100644 --- a/app/Admin/Controllers/ProductController.php +++ b/app/Admin/Controllers/ProductController.php @@ -32,6 +32,11 @@ class ProductController extends AdminController $grid->model()->where('status', ProductStatus::UNAUDITED); } + $category_id = request()->input('cid'); + if ($category_id) { + $grid->model()->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]); + } + /*$grid->batchActions([ new BatchAuditProduct(null, 1), new BatchAuditProduct(null, 2), @@ -71,11 +76,33 @@ class ProductController extends AdminController $filter->equal('id')->width(2); $filter->like('title')->width(3); $filter->equal('status')->select(ProductStatus::array())->width(2); + + $options = array_slice(Category::selectOptions(fn($query) => $query->where('agent_id', 0)), 1, null, true); + $filter->equal('cid', '产品分类')->ignore()->select($options)->width(3); $filter->equal('type')->select(admin_trans('product.options.publish_type'))->width(2); }); }); } + //递归获取指定分类下的所有子分类 + private function get_category_child_ids($category_id): array + { + static $category = null; + if ($category === null) { + $category = Category::where('agent_id', 0)->get()->toArray(); + } + + $child = []; + foreach ($category as $cat) { + if ($cat['pid'] == $category_id) { + $child[] = $cat['id']; + $child = array_merge($child, $this->get_category_child_ids($cat['id'])); + } + } + + return $child; + } + /** * Make a show builder. * diff --git a/app/AdminSupplier/Controllers/ProductController.php b/app/AdminSupplier/Controllers/ProductController.php index 4534e14..36bacf9 100644 --- a/app/AdminSupplier/Controllers/ProductController.php +++ b/app/AdminSupplier/Controllers/ProductController.php @@ -31,6 +31,11 @@ class ProductController extends AdminController return Grid::make(new Product(['category:id,name']), function (Grid $grid) { $grid->model()->where('supplier_id', Admin::user()->id); + $category_id = request()->input('cid'); + if ($category_id) { + $grid->model()->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]); + } + $grid->column('id')->sortable(); $grid->column('type')->using(admin_trans('product.options.publish_type')); $grid->column('category.name', '产品分类'); @@ -63,11 +68,33 @@ class ProductController extends AdminController $filter->equal('id')->width(2); $filter->like('title')->width(3); $filter->equal('status')->select(ProductStatus::array())->width(2); + + $options = array_slice(Category::selectOptions(fn($query) => $query->where('agent_id', 0)), 1, null, true); + $filter->equal('cid', '产品分类')->ignore()->select($options)->width(3); $filter->equal('type')->select(admin_trans('product.options.publish_type'))->width(2); }); }); } + //递归获取指定分类下的所有子分类 + private function get_category_child_ids($category_id): array + { + static $category = null; + if ($category === null) { + $category = Category::where('agent_id', 0)->get()->toArray(); + } + + $child = []; + foreach ($category as $cat) { + if ($cat['pid'] == $category_id) { + $child[] = $cat['id']; + $child = array_merge($child, $this->get_category_child_ids($cat['id'])); + } + } + + return $child; + } + /** * Make a show builder. *