RankController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Controllers\Goodnight;
  3. use App\Http\Controllers\Miniprogram\Auth;
  4. use App\Http\Resources\Goodnight\RankCollection;
  5. use App\Models\Goodnight\ThumbModel;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. class RankController extends Controller
  9. {
  10. //
  11. public function total(Request $request)
  12. {
  13. $page = $request->input('page', 1);
  14. $per_page = $request->input('per_page', 100);
  15. $builder = \Cache::get("goodnight:rank:total", collect());
  16. $total = $builder->count();
  17. $last_page = ceil($total / $per_page);
  18. $voices = $builder->slice(($page - 1) * $per_page, $per_page)->values();
  19. foreach ($voices as $voice) {
  20. try {
  21. $uid = Auth::auth();
  22. // 自己对这张卡片的操作
  23. $self = array(
  24. 'like' => false,
  25. 'is_self' => false
  26. );
  27. if ($voice->uid == $uid) {
  28. $self['is_self'] = true;
  29. }
  30. if (
  31. ThumbModel::where([
  32. ['voice_id', $voice->id],
  33. ['uid', $uid]
  34. ])->exists()
  35. ) {
  36. $self['like'] = true;
  37. }
  38. $voice->self = $self;
  39. } catch (\Exception $e) {
  40. }
  41. }
  42. return (new RankCollection($voices))
  43. ->additional([
  44. 'meta' => [
  45. 'current_page' => $page,
  46. 'per_page' => $per_page,
  47. 'last_page' => $last_page,
  48. 'total' => $total,
  49. ]
  50. ]);
  51. }
  52. public function daily(Request $request)
  53. {
  54. $page = $request->input('page', 1);
  55. $per_page = $request->input('per_page', 100);
  56. $builder = \Cache::get("goodnight:rank:daily:" . date("Y-m-d"), collect());
  57. $total = $builder->count();
  58. $last_page = ceil($total / $per_page);
  59. $voices = $builder->slice(($page - 1) * $per_page, $per_page)->values();
  60. foreach ($voices as $voice) {
  61. try {
  62. $uid = Auth::auth();
  63. // 自己对这张卡片的操作
  64. $self = array(
  65. 'like' => false,
  66. 'is_self' => false
  67. );
  68. if ($voice->uid == $uid) {
  69. $self['is_self'] = true;
  70. }
  71. if (
  72. ThumbModel::where([
  73. ['voice_id', $voice->id],
  74. ['uid', $uid]
  75. ])->exists()
  76. ) {
  77. $self['like'] = true;
  78. }
  79. $voice->self = $self;
  80. } catch (\Exception $e) {
  81. }
  82. }
  83. return (new RankCollection($voices))
  84. ->additional([
  85. 'meta' => [
  86. 'current_page' => $page,
  87. 'per_page' => $per_page,
  88. 'last_page' => $last_page,
  89. 'total' => $total,
  90. ]
  91. ]);
  92. }
  93. public function topic(Request $request)
  94. {
  95. $page = $request->input('page', 1);
  96. $per_page = $request->input('per_page', 100);
  97. $builder = \Cache::get("goodnight:rank:topic", collect());
  98. $total = $builder->count();
  99. $last_page = ceil($total / $per_page);
  100. $voices = $builder->slice(($page - 1) * $per_page, $per_page)->values();
  101. foreach ($voices as $voice) {
  102. try {
  103. $uid = Auth::auth();
  104. // 自己对这张卡片的操作
  105. $self = array(
  106. 'like' => false,
  107. 'is_self' => false
  108. );
  109. if ($voice->uid == $uid) {
  110. $self['is_self'] = true;
  111. }
  112. if (
  113. ThumbModel::where([
  114. ['voice_id', $voice->id],
  115. ['uid', $uid]
  116. ])->exists()
  117. ) {
  118. $self['like'] = true;
  119. }
  120. $voice->self = $self;
  121. } catch (\Exception $e) {
  122. }
  123. }
  124. return (new RankCollection($voices))
  125. ->additional([
  126. 'meta' => [
  127. 'current_page' => $page,
  128. 'per_page' => $per_page,
  129. 'last_page' => $last_page,
  130. 'total' => $total,
  131. ]
  132. ]);
  133. }
  134. }