CategoryRepository.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-15
  6. * Time: 11:16
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Category;
  10. use App\Models\CategorySuggest;
  11. use App\Models\CategoryTopic;
  12. class CategoryRepository {
  13. public function __construct(Category $category,CategoryTopic $categoryTopic, CategorySuggest $categorySuggest) {
  14. $this->category = $category;
  15. $this->categoryTopic = $categoryTopic;
  16. $this->categorySuggest = $categorySuggest;
  17. }
  18. //列表
  19. public function lists($request)
  20. {
  21. $where = [];
  22. if(isset($request['is_suggest']) && $request['is_suggest'] == 1){
  23. $where [] = ['is_suggest', 1];
  24. $data = [];
  25. }else{
  26. $where [] = ['is_open', 1];
  27. $data = [
  28. [
  29. 'id' => -2,
  30. 'name' => '热门',
  31. 'img' => config('customer.category_hot_img')
  32. ],
  33. [
  34. 'id' => -1,
  35. 'name' => '我的话题',
  36. 'img' => config('customer.category_my_img')
  37. ]
  38. ];
  39. }
  40. $categories = $this->category->select('id', 'name', 'img')->where($where)->orderBy('sort', 'asc')->limit(50)->get()->toArray();
  41. return array_merge($data, $categories);
  42. }
  43. //获取多个话题
  44. public function getTopics($ids){
  45. $ids = explode(',',$ids);
  46. return $this->categoryTopic
  47. ->leftJoin('topic', 'category_topic.topic_id', '=', 'topic.id')
  48. ->where('topic.is_suggest',1)
  49. ->whereIn('category_topic.category_id',$ids)
  50. ->select('topic.id','topic.name','topic.is_suggest')
  51. ->groupBy('topic.id')
  52. ->get();
  53. }
  54. //分类列表(推荐内容首页用)
  55. public function suggest()
  56. {
  57. $list = [
  58. [
  59. 'id' => 0,
  60. 'name' => '推荐',
  61. 'img' => config('customer.category_hot_img')
  62. ]
  63. ];
  64. $name = ['推荐'];
  65. $res = $this->categorySuggest->get();
  66. foreach($res as $item){
  67. $list[] = [
  68. 'id' => $item->category_id,
  69. 'name' => $item->category->name,
  70. 'img' => $item->category->img,
  71. ];
  72. $name[] = $item->category->name;
  73. }
  74. return [
  75. 'category_list' => $list,
  76. 'category_list_name' => $name,
  77. ];
  78. }
  79. }