diff --git a/app/admin/controller/store_category/StoreCategoryController.php b/app/admin/controller/store_category/StoreCategoryController.php index 381ab139a..9da77d182 100644 --- a/app/admin/controller/store_category/StoreCategoryController.php +++ b/app/admin/controller/store_category/StoreCategoryController.php @@ -91,5 +91,10 @@ class StoreCategoryController extends BaseAdminController return $this->data($result); } + public function tree() + { + $result = StoreCategoryLogic::tree(); + return $this->data($result); + } } \ No newline at end of file diff --git a/app/admin/lists/store_product/StoreProductLists.php b/app/admin/lists/store_product/StoreProductLists.php index b2c5095ab..e19576e6a 100644 --- a/app/admin/lists/store_product/StoreProductLists.php +++ b/app/admin/lists/store_product/StoreProductLists.php @@ -57,12 +57,12 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa $class_all = $this->request->get('class_all'); if ($class_all) { //查3级别的 - $arr = Cate::where('pid', $class_all)->column('id'); - if ($arr) { - $arr2 = Cate::where('pid', 'in', $arr)->column('id'); - $this->searchWhere[] = ['cate_id', 'in', array_merge($arr, $arr2)]; + if (count($class_all) == 1) { + $this->searchWhere[] = ['top_cate_id', '=', $class_all[0]]; + } elseif (count($class_all) == 2) { + $this->searchWhere[] = ['two_cate_id', '=', $class_all[1]]; } else { - $this->searchWhere[] = ['cate_id', '=', $class_all]; + $this->searchWhere[] = ['cate_id', '=', $class_all[2]]; } } $is_warehouse = $this->request->get('is_warehouse', 0); diff --git a/app/admin/logic/store_category/StoreCategoryLogic.php b/app/admin/logic/store_category/StoreCategoryLogic.php index 0dcd944f9..32d04daf7 100644 --- a/app/admin/logic/store_category/StoreCategoryLogic.php +++ b/app/admin/logic/store_category/StoreCategoryLogic.php @@ -108,4 +108,11 @@ class StoreCategoryLogic extends BaseLogic { return StoreCategory::findOrEmpty($params['id'])->toArray(); } + + public static function tree() + { + $data = StoreCategory::field('id,pid,name')->order('id desc')->select()->toArray(); + return list2tree($data); + } + } \ No newline at end of file diff --git a/app/functions.php b/app/functions.php index 62d3cde79..f9b5006c5 100644 --- a/app/functions.php +++ b/app/functions.php @@ -583,4 +583,31 @@ function SqlChannelPriceLog($product_id=0, $group_id=0, $before_price=0,$after_p // $orderId = $type . date('YmdHis', time()); // return $orderId; // } -// } \ No newline at end of file +// } + +if (!function_exists('list2tree')) { + function list2tree(array $list, $idKey = 'id', $parentKey = 'pid', $childrenKey = 'children') + { + $tree = []; + $itemsByReference = []; + + // 首先将所有项目按id存入引用数组 + foreach ($list as &$item) { + $itemsByReference[$item[$idKey]] = &$item; + $item[$childrenKey] = []; + } + + // 构建树 + foreach ($list as &$item) { + if ($item[$parentKey] && isset($itemsByReference[$item[$parentKey]])) { + $itemsByReference[$item[$parentKey]][$childrenKey][] = &$item; + } else { + $tree[] = &$item; + } + } + + return $tree; + } + + +} \ No newline at end of file