| xqd
@@ -14,6 +14,13 @@ use Illuminate\Support\Collection;
|
|
|
|
|
|
class EpisodeController extends Controller
|
|
|
{
|
|
|
+
|
|
|
+ private $number;
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->number = env('HOME_RECOMMEND_NUMBER', 6);
|
|
|
+ }
|
|
|
/**
|
|
|
* 推荐
|
|
|
*/
|
| xqd
@@ -24,20 +31,6 @@ class EpisodeController extends Controller
|
|
|
->inRandomOrder()
|
|
|
//->where('created_at','>=', Carbon::now()->subDays(7)->toDateTime())
|
|
|
->first();
|
|
|
-
|
|
|
- $limit = 3 - ($history?$history->count():0);
|
|
|
- // 推荐
|
|
|
- $episodes = Episode::withCount(['lists'])
|
|
|
- ->where('is_opend', 1)
|
|
|
- ->where('platform', \user()->info->platform)
|
|
|
- ->when($history,function ($query) use($history){
|
|
|
- /* @var Builder $query*/
|
|
|
- return $query->where('category_id', $history->episode->category_id)
|
|
|
- ->where('id','<>', $history->episode->id);
|
|
|
- })
|
|
|
- ->limit($limit)
|
|
|
- ->get();
|
|
|
-
|
|
|
$lists = [];
|
|
|
|
|
|
$ids = [];
|
| xqd
@@ -50,33 +43,49 @@ class EpisodeController extends Controller
|
|
|
$lists[] = $history->episode;
|
|
|
}
|
|
|
|
|
|
- /* @var Episode $episode*/
|
|
|
- foreach ($episodes as $episode){
|
|
|
- $ids[] = $episode->id;
|
|
|
- $episode->status_text = $episode->status;
|
|
|
- $episode->total = $episode->lists_count;
|
|
|
- $episode->guess = true;
|
|
|
- $lists[] = $episode;
|
|
|
- }
|
|
|
- // 推荐的不够两个 需要补充
|
|
|
- $limit = $limit -($episodes ? $episodes->count() : 0);
|
|
|
- $supplement = Episode::withCount(['lists'])
|
|
|
+ $limit = $this->number - count($lists);
|
|
|
+ // 推荐
|
|
|
+ $episodes = Episode::withCount(['lists'])
|
|
|
->where('is_opend', 1)
|
|
|
->where('platform', \user()->info->platform)
|
|
|
- ->when($ids, function ($query) use($ids){
|
|
|
+ ->when($history,function ($query) use($history){
|
|
|
/* @var Builder $query*/
|
|
|
- return $query->whereNotIn('id',$ids);
|
|
|
+ return $query->where('category_id', $history->episode->category_id)
|
|
|
+ ->where('id','<>', $history->episode->id);
|
|
|
})
|
|
|
->limit($limit)
|
|
|
->get();
|
|
|
|
|
|
/* @var Episode $episode*/
|
|
|
- foreach ($supplement as $episode){
|
|
|
+ foreach ($episodes as $episode){
|
|
|
+ $ids[] = $episode->id;
|
|
|
$episode->status_text = $episode->status;
|
|
|
$episode->total = $episode->lists_count;
|
|
|
$episode->guess = true;
|
|
|
$lists[] = $episode;
|
|
|
}
|
|
|
+ // 推荐的不够两个 需要补充
|
|
|
+ $limit = $this->number - count($lists);
|
|
|
+ if($limit){
|
|
|
+ $supplement = Episode::withCount(['lists'])
|
|
|
+ ->where('is_opend', 1)
|
|
|
+ ->where('platform', \user()->info->platform)
|
|
|
+ ->when($ids, function ($query) use($ids){
|
|
|
+ /* @var Builder $query*/
|
|
|
+ return $query->whereNotIn('id',$ids);
|
|
|
+ })
|
|
|
+ ->limit($limit)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ /* @var Episode $episode*/
|
|
|
+ foreach ($supplement as $episode){
|
|
|
+ $episode->status_text = $episode->status;
|
|
|
+ $episode->total = $episode->lists_count;
|
|
|
+ $episode->guess = true;
|
|
|
+ $lists[] = $episode;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
return $this->success($lists);
|
|
|
}
|
| xqd
@@ -87,14 +96,37 @@ class EpisodeController extends Controller
|
|
|
*/
|
|
|
public function news()
|
|
|
{
|
|
|
- $limit = request()->input('limit',3);
|
|
|
+ $limit = request()->input('limit', $this->number);
|
|
|
$page = request()->input('page',1);
|
|
|
$offset = ($page - 1) * $limit;
|
|
|
$episodes = Episode::withCount(['lists'])
|
|
|
->where('is_opend', 1)
|
|
|
->where('platform', \user()->info->platform)
|
|
|
->inRandomOrder()
|
|
|
- ->orderByDesc('id')
|
|
|
+ ->orderByDesc('created_at')
|
|
|
+ ->limit($limit)
|
|
|
+ ->offset($offset)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+
|
|
|
+ $lists = Episode::complete($episodes);
|
|
|
+
|
|
|
+ return $this->success($lists);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 今日推荐
|
|
|
+ */
|
|
|
+ public function todayRecommend()
|
|
|
+ {
|
|
|
+ $limit = request()->input('limit', $this->number);
|
|
|
+ $page = request()->input('page',1);
|
|
|
+ $offset = ($page - 1) * $limit;
|
|
|
+ $episodes = Episode::withCount(['lists'])
|
|
|
+ ->where('is_opend', 1)
|
|
|
+ ->where('platform', \user()->info->platform)
|
|
|
+ ->inRandomOrder()
|
|
|
+ ->orderByDesc('sort')
|
|
|
->limit($limit)
|
|
|
->offset($offset)
|
|
|
->get();
|
| xqd
@@ -111,7 +143,7 @@ class EpisodeController extends Controller
|
|
|
*/
|
|
|
public function rank()
|
|
|
{
|
|
|
- $limit = request()->input('limit',3);
|
|
|
+ $limit = request()->input('limit', $this->number);
|
|
|
$page = request()->input('page',1);
|
|
|
$offset = ($page - 1) * $limit;
|
|
|
$episodes = Episode::withCount(['userEpisodesRecords','lists'])
|