BookCategoryService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/4
  6. * Time: 上午11:49
  7. */
  8. namespace App\Modules\Book\Services;
  9. use App\Modules\Book\Models\BookCategory;
  10. use App\Modules\Book\Models\Book;
  11. class BookCategoryService
  12. {
  13. public static function getSecondCategories($id_arr=[]){
  14. return BookCategory::getSecondCategories($id_arr);
  15. }
  16. /**
  17. * 获取分类
  18. * @param bool $all true获取所有分类,false获取存在图书的分类
  19. * @return array [ ['id'=>'1','name'=>'男频','children'=>[['id'=>'','name'=>''],...]] ,[] ]
  20. */
  21. public static function getCategory($all=false){
  22. if($all){
  23. $res = BookCategory::getSecondCategories();
  24. }else{
  25. $res = BookCategory::getSecondCategories(Book::getCategoryId());
  26. }
  27. $male = [];
  28. $female = [];
  29. foreach ($res as $v){
  30. if($v->channel_name == '男频'){
  31. $male[] = ['id'=>$v->id,'name'=>$v->category_name];
  32. }
  33. if($v->channel_name == '女频'){
  34. $female[] = ['id'=>$v->id,'name'=>$v->category_name];
  35. }
  36. }
  37. $data = [
  38. ['id'=>1,'name'=>'男频','children'=>$male],
  39. ['id'=>2,'name'=>'女频','children'=>$female],
  40. ];
  41. return $data;
  42. }
  43. }