CategoryService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\common\Service\Goods;
  3. use app\common\model\Category;
  4. use app\common\Enum\StatusEnum;
  5. class CategoryService
  6. {
  7. /**
  8. * 获取栏目所有子级的ID
  9. * @param mixed $ids 栏目ID或集合ID
  10. * @param bool $withself 是否包含自身
  11. * @return array
  12. */
  13. public static function getCategoryChildrenIds($ids, $withself = true)
  14. {
  15. // $cacheName = 'shop-childrens-' . $ids . '-' . $withself;
  16. // $result = Cache::get($cacheName);
  17. // if ($result === false) {
  18. $categoryList = Category::where('status', StatusEnum::ENABLED)
  19. ->order('weigh desc,id desc')
  20. ->select();
  21. $result = [];
  22. $tree = \fast\Tree::instance();
  23. $tree->init(collection($categoryList)->toArray(), 'pid');
  24. $CategoryIds = is_array($ids) ? $ids : explode(',', $ids);
  25. foreach ($CategoryIds as $index => $CategoryId) {
  26. $result = array_merge($result, $tree->getChildrenIds($CategoryId, $withself));
  27. }
  28. // Cache::set($cacheName, $result);
  29. // }
  30. return $result;
  31. }
  32. /**
  33. * 根据分类ID获取分类信息
  34. * @param array $arrCategoryIds 分类ID集合
  35. * @return array
  36. */
  37. public static function getCategoryByIds($arrCategoryIds = []){
  38. $categoryList = Category::where('id', 'IN', $arrCategoryIds)
  39. ->where('status', StatusEnum::ENABLED)
  40. ->order('weigh desc,id desc')
  41. ->field('id,name,image,weigh,pid')
  42. ->select();
  43. $arrCategory = collection($categoryList)->toArray();
  44. return $arrCategory;
  45. }
  46. }