EpisodeController.php 9.6 KB

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