CategoryService.php 1.6 KB

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