EpisodeController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\UserEpisodesRecord;
  10. use App\Models\UserWatchRecord;
  11. use Carbon\Carbon;
  12. use Illuminate\Database\Eloquent\Builder;
  13. use Illuminate\Support\Collection;
  14. use function GuzzleHttp\Promise\all;
  15. class EpisodeController extends Controller
  16. {
  17. private $number;
  18. public function __construct()
  19. {
  20. $this->number = env('HOME_RECOMMEND_NUMBER', 6);
  21. }
  22. /**
  23. * 推荐
  24. */
  25. public function recommend()
  26. {
  27. $history = UserWatchRecord::filterUser()
  28. ->withCount(['episode'])
  29. ->whereHas('episode', function (Builder $query){
  30. $query->where('is_opend', 1);
  31. })
  32. ->inRandomOrder()
  33. //->where('created_at','>=', Carbon::now()->subDays(7)->toDateTime())
  34. ->first();
  35. $lists = [];
  36. $ids = [];
  37. if($history){
  38. $ids[] = $history->episode->id;
  39. $history->episode->recent = true;
  40. $history->episode->status_text = $history->episode->status;
  41. $history->episode->total = $history->episode->lists->count();
  42. unset( $history->episode->lists);
  43. $lists[] = $history->episode;
  44. }
  45. $limit = $this->number - count($lists);
  46. // 推荐
  47. $episodes = Episode::withCount(['lists'])
  48. ->where('is_opend', 1)
  49. ->where('platform', \user()->info->platform)
  50. ->when($history,function ($query) use($history){
  51. /* @var Builder $query*/
  52. return $query->where('category_id', $history->episode->category_id)
  53. ->where('id','<>', $history->episode->id);
  54. })
  55. ->limit($limit)
  56. ->get();
  57. /* @var Episode $episode*/
  58. foreach ($episodes as $episode){
  59. $ids[] = $episode->id;
  60. $episode->status_text = $episode->status;
  61. $episode->total = $episode->lists_count;
  62. $episode->guess = true;
  63. $lists[] = $episode;
  64. }
  65. // 推荐的不够两个 需要补充
  66. $limit = $this->number - count($lists);
  67. if($limit){
  68. $supplement = Episode::withCount(['lists'])
  69. ->where('is_opend', 1)
  70. ->where('platform', \user()->info->platform)
  71. ->when($ids, function ($query) use($ids){
  72. /* @var Builder $query*/
  73. return $query->whereNotIn('id',$ids);
  74. })
  75. ->limit($limit)
  76. ->get();
  77. /* @var Episode $episode*/
  78. foreach ($supplement as $episode){
  79. $episode->status_text = $episode->status;
  80. $episode->total = $episode->lists_count;
  81. $episode->guess = true;
  82. $lists[] = $episode;
  83. }
  84. }
  85. return $this->success($lists);
  86. }
  87. /**
  88. * 最新
  89. */
  90. public function news()
  91. {
  92. $limit = request()->input('limit', $this->number);
  93. $page = request()->input('page',1);
  94. $offset = ($page - 1) * $limit;
  95. $episodes = Episode::withCount(['lists'])
  96. ->where('is_opend', 1)
  97. ->where('platform', \user()->info->platform)
  98. //->inRandomOrder()
  99. ->orderByDesc('created_at')
  100. ->limit($limit)
  101. ->offset($offset)
  102. ->get();
  103. $lists = Episode::complete($episodes);
  104. return $this->success($lists);
  105. }
  106. /**
  107. * 今日推荐
  108. */
  109. public function todayRecommend()
  110. {
  111. $limit = request()->input('limit', $this->number);
  112. $page = request()->input('page',1);
  113. $offset = ($page - 1) * $limit;
  114. $episodes = Episode::withCount(['lists'])
  115. ->where('is_opend', 1)
  116. ->where('platform', \user()->info->platform)
  117. //->inRandomOrder()
  118. ->orderBy('sort')
  119. ->limit($limit)
  120. ->offset($offset)
  121. ->get();
  122. $lists = Episode::complete($episodes);
  123. return $this->success($lists);
  124. }
  125. /**
  126. * 排行
  127. */
  128. public function rank()
  129. {
  130. $limit = request()->input('limit', $this->number);
  131. $page = request()->input('page',1);
  132. $offset = ($page - 1) * $limit;
  133. $episodes = Episode::withCount(['userEpisodesRecords','lists'])
  134. ->where('is_opend', 1)
  135. ->where('platform', \user()->info->platform)
  136. ->orderByDesc('user_episodes_records_count')
  137. ->limit($limit)
  138. ->offset($offset)
  139. ->get();
  140. $lists = [];
  141. $rank = (($page - 1) * $limit) + 1;
  142. /* @var Episode $episode*/
  143. foreach ($episodes as $episode){
  144. $episode->status_text = $episode->status;
  145. $episode->total = $episode->lists_count;
  146. $episode->rank = $rank++;
  147. $lists[] = $episode;
  148. }
  149. return $this->success($lists);
  150. }
  151. // 追剧
  152. public function trace()
  153. {
  154. $category = EpisodesCategory::with(['child:id,pid'])
  155. ->where('level',1)
  156. ->orderBy('sort')
  157. ->get(['id','name']);
  158. $lists = [];
  159. /* @var EpisodesCategory $item*/
  160. foreach ($category as $item){
  161. $childIds = $item->child->pluck('id');
  162. $childIds[] = $item->id;
  163. $episodes = Episode::whereIn('category_id', $childIds)
  164. ->where('platform',\user()->info->platform)
  165. ->where('is_opend', 1)
  166. ->limit(3)
  167. ->get();
  168. /* @var Episode $episode*/
  169. foreach ($episodes as $episode){
  170. $count = $episode->withCount('lists')->first()->toArray();
  171. $episode->total = $count['lists_count'];
  172. $episode->status_text = $episode->status;
  173. }
  174. $item->episodes = $episodes;
  175. unset($item->child);
  176. if($episodes->count()){
  177. $lists[] = $item;
  178. }
  179. }
  180. // $lists = EpisodesCategory::with(['episodes' =>function($query){
  181. // /* @var Builder $query*/
  182. // return $query->where('platform',\user()->info->platform)->where('is_opend', 1);
  183. // }])->has('episodes','>=', 1)
  184. // ->orderBy('sort')
  185. // ->get()
  186. // ->map(function ($item) {
  187. // $item->setRelation('episodes', $item->episodes->take(3));
  188. // return $item;
  189. // });
  190. //
  191. // /* @var EpisodesCategory $list*/
  192. // foreach ($lists as $key => $list){
  193. // /* @var Episode $episode*/
  194. // foreach ($list->episodes as $episode){
  195. // $count = $episode->withCount('lists')->first()->toArray();
  196. // $episode->total = $count['lists_count'];
  197. // $episode->status_text = $episode->status;
  198. // }
  199. // }
  200. return $this->success($lists);
  201. }
  202. /**
  203. * 最新
  204. */
  205. public function search()
  206. {
  207. $keywords = request()->input('keywords','');
  208. $limit = request()->input('limit',3);
  209. $page = request()->input('page',1);
  210. $offset = ($page - 1) * $limit;
  211. $episodes = Episode::withCount(['lists'])
  212. ->where('is_opend', 1)
  213. ->where('platform', \user()->info->platform)
  214. ->when($keywords, function ($query, $keywords) {
  215. /* @var Builder $query*/
  216. return $query->where('name','like', "%$keywords%");
  217. })
  218. ->inRandomOrder()
  219. ->orderByDesc('id')
  220. ->limit($limit)
  221. ->offset($offset)
  222. ->get();
  223. $lists = Episode::complete($episodes);
  224. return $this->success($lists);
  225. }
  226. public function vipFree()
  227. {
  228. $limit = request()->input('limit',3);
  229. $page = request()->input('page',1);
  230. $offset = ($page - 1) * $limit;
  231. $episodes = Episode::withCount(['lists'])
  232. ->where('is_opend', 1)
  233. ->where('is_vip_watch', 1)
  234. ->where('platform', \user()->info->platform)
  235. ->inRandomOrder()
  236. ->orderByDesc('id')
  237. ->limit($limit)
  238. ->offset($offset)
  239. ->get();
  240. $lists = Episode::complete($episodes);
  241. return $this->success($lists);
  242. }
  243. // 详情
  244. public function detail($id)
  245. {
  246. $episodes = Episode::withCount(['userWatchRecord','userCollect','userFavorite','lists'])
  247. ->with(['category:id,name','lists:id,episodes_id,sort,url,is_free,origin_price,sale_price'])
  248. ->where('is_opend', 1)
  249. ->where('id', $id)
  250. ->where('platform', \user()->info->platform)
  251. ->first();
  252. return $this->success($episodes);
  253. }
  254. public function listBuyNum($listId)
  255. {
  256. $count = UserEpisodesRecord::where('list_id',$listId)->count();
  257. return $this->success($count);
  258. }
  259. // 分享
  260. public function shared($id)
  261. {
  262. $res = Episode::find($id);
  263. if(!$res){
  264. return $this->success();
  265. }
  266. $res->share_count = $res->share_count + 1;
  267. $res->save();
  268. return $this->success();
  269. }
  270. }