EpisodeController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\Banner;
  4. use App\Models\Episode;
  5. use App\Models\EpisodesCategory;
  6. use App\Models\NavBar;
  7. use App\Models\Tabbar;
  8. use App\Models\User;
  9. use App\Models\UserWatchRecord;
  10. use Carbon\Carbon;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Support\Collection;
  13. class EpisodeController extends Controller
  14. {
  15. /**
  16. * 推荐
  17. */
  18. public function recommend()
  19. {
  20. $history = UserWatchRecord::filterUser()
  21. ->withCount(['episode'])
  22. ->inRandomOrder()
  23. //->where('created_at','>=', Carbon::now()->subDays(7)->toDateTime())
  24. ->first();
  25. $limit = 3 - ($history?$history->count():0);
  26. // 推荐
  27. $episodes = Episode::withCount(['lists'])
  28. ->where('is_opend', 1)
  29. ->where('platform', \user()->info->platform)
  30. ->when($history,function ($query) use($history){
  31. /* @var Builder $query*/
  32. return $query->where('category_id', $history->episode->category_id)
  33. ->where('id','<>', $history->episode->id);
  34. })
  35. ->limit($limit)
  36. ->get();
  37. $lists = [];
  38. $ids = [];
  39. if($history){
  40. $ids[] = $history->episode->id;
  41. $history->episode->recent = true;
  42. $history->episode->status_text = $history->episode->status;
  43. $history->episode->total = $history->episode->lists->count();
  44. unset( $history->episode->lists);
  45. $lists[] = $history->episode;
  46. }
  47. /* @var Episode $episode*/
  48. foreach ($episodes as $episode){
  49. $ids[] = $episode->id;
  50. $episode->status_text = $episode->status;
  51. $episode->total = $episode->lists_count;
  52. $episode->guess = true;
  53. $lists[] = $episode;
  54. }
  55. // 推荐的不够两个 需要补充
  56. $limit = $limit -($episodes ? $episodes->count() : 0);
  57. $supplement = Episode::withCount(['lists'])
  58. ->where('is_opend', 1)
  59. ->where('platform', \user()->info->platform)
  60. ->when($ids, function ($query) use($ids){
  61. /* @var Builder $query*/
  62. return $query->whereNotIn('id',$ids);
  63. })
  64. ->limit($limit)
  65. ->get();
  66. /* @var Episode $episode*/
  67. foreach ($supplement as $episode){
  68. $episode->status_text = $episode->status;
  69. $episode->total = $episode->lists_count;
  70. $episode->guess = true;
  71. $lists[] = $episode;
  72. }
  73. return $this->success($lists);
  74. }
  75. /**
  76. * 最新
  77. */
  78. public function news()
  79. {
  80. $limit = request()->input('limit',3);
  81. $page = request()->input('page',1);
  82. $offset = ($page - 1) * $limit;
  83. $episodes = Episode::withCount(['lists'])
  84. ->where('is_opend', 1)
  85. ->where('platform', \user()->info->platform)
  86. ->inRandomOrder()
  87. ->orderByDesc('id')
  88. ->limit($limit)
  89. ->offset($offset)
  90. ->get();
  91. $lists = Episode::complete($episodes);
  92. return $this->success($lists);
  93. }
  94. /**
  95. * 排行
  96. */
  97. public function rank()
  98. {
  99. $limit = request()->input('limit',3);
  100. $page = request()->input('page',1);
  101. $offset = ($page - 1) * $limit;
  102. $episodes = Episode::withCount(['userEpisodesRecords','lists'])
  103. ->where('is_opend', 1)
  104. ->where('platform', \user()->info->platform)
  105. ->orderByDesc('user_episodes_records_count')
  106. ->limit($limit)
  107. ->offset($offset)
  108. ->get();
  109. $lists = [];
  110. $rank = (($page - 1) * $limit) + 1;
  111. /* @var Episode $episode*/
  112. foreach ($episodes as $episode){
  113. $episode->status_text = $episode->status;
  114. $episode->total = $episode->lists_count;
  115. $episode->rank = $rank++;
  116. $lists[] = $episode;
  117. }
  118. return $this->success($lists);
  119. }
  120. // 追剧
  121. public function trace()
  122. {
  123. $lists = EpisodesCategory::with(['episodes' =>function($query){
  124. /* @var Builder $query*/
  125. return $query->limit(3);
  126. }])->has('episodes')
  127. ->orderByDesc('sort')
  128. ->get();
  129. /* @var EpisodesCategory $list*/
  130. foreach ($lists as $list){
  131. /* @var Episode $episode*/
  132. foreach ($list->episodes as $episode){
  133. $count = $episode->withCount('lists')->first()->toArray();
  134. $episode->total = $count['lists_count'];
  135. $episode->status_text = $episode->status;
  136. }
  137. }
  138. return $this->success($lists);
  139. }
  140. /**
  141. * 最新
  142. */
  143. public function search()
  144. {
  145. $keywords = request()->input('keywords','');
  146. $limit = request()->input('limit',3);
  147. $page = request()->input('page',1);
  148. $offset = ($page - 1) * $limit;
  149. $episodes = Episode::withCount(['lists'])
  150. ->where('is_opend', 1)
  151. ->where('platform', \user()->info->platform)
  152. ->when($keywords, function ($query, $keywords) {
  153. /* @var Builder $query*/
  154. return $query->where('name','like', "%$keywords%");
  155. })
  156. ->inRandomOrder()
  157. ->orderByDesc('id')
  158. ->limit($limit)
  159. ->offset($offset)
  160. ->get();
  161. $lists = Episode::complete($episodes);
  162. return $this->success($lists);
  163. }
  164. public function vipFree()
  165. {
  166. $limit = request()->input('limit',3);
  167. $page = request()->input('page',1);
  168. $offset = ($page - 1) * $limit;
  169. $episodes = Episode::withCount(['lists'])
  170. ->where('is_opend', 1)
  171. ->where('is_vip_watch', 1)
  172. ->where('platform', \user()->info->platform)
  173. ->inRandomOrder()
  174. ->orderByDesc('id')
  175. ->limit($limit)
  176. ->offset($offset)
  177. ->get();
  178. $lists = Episode::complete($episodes);
  179. return $this->success($lists);
  180. }
  181. // 详情
  182. public function detail($id)
  183. {
  184. $episodes = Episode::withCount(['userWatchRecord','userCollect','userFavorite','lists'])
  185. ->with(['category:id,name','lists:id,episodes_id,sort,url,is_free,sale_price'])
  186. ->where('is_opend', 1)
  187. ->where('id', $id)
  188. ->where('platform', \user()->info->platform)
  189. ->first();
  190. return $this->success($episodes);
  191. }
  192. // 分享
  193. public function shared($id)
  194. {
  195. $res = Episode::find($id);
  196. if(!$res){
  197. return $this->success();
  198. }
  199. $res->share_count = $res->share_count + 1;
  200. $res->save();
  201. return $this->success();
  202. }
  203. }