EpisodeController.php 9.0 KB

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