AlbumController.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Album;
  5. use App\Models\AlbumCategory;
  6. use App\Supports\Response;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * @group Lottery Prize
  13. */
  14. class AlbumController extends Controller
  15. {
  16. public function __construct(
  17. )
  18. {
  19. }
  20. public function list(Request $request, $locate = 'tw')
  21. {
  22. $categoryId = $request->input("categoryId") ?? "";
  23. $locate = $locate == "tw" ? "zh_TW" : $locate;
  24. $result = [];
  25. //年份清單
  26. $yearList = Album::select(DB::raw("DATE_FORMAT(post_date, '%Y') as years"))->distinct()->orderBy("years", "desc")->pluck("years");
  27. $result["yearList"] = $yearList;
  28. //置頂影音
  29. $topVideos = Album::where("visible", 1)->where("on_top", 1)->orderBy("order")->orderByDesc("post_date")->get();
  30. foreach($topVideos as $topVideo){
  31. $result["top"][] = [
  32. "id" => $topVideo->id,
  33. "categoryId" => $topVideo->albumCategory->id,
  34. "category" => $topVideo->albumCategory->getTranslation("name", $locate),
  35. "postDate" => Carbon::parse($topVideo->post_date)->format("Y.m.d"),
  36. "title" => $topVideo->getTranslation("title", $locate),
  37. "imgBanner" => $topVideo->album_img_banner_url,
  38. "imgPC" => $topVideo->album_img_pc_url,
  39. "imgMobile" => $topVideo->album_img_mobile_url,
  40. "type" => $topVideo->album_upload_type,
  41. "link" => $topVideo->album_video_link,
  42. ];
  43. }
  44. //分類列表
  45. $categoryList = AlbumCategory::orderByDesc('order')->orderByDesc('id')->get();
  46. foreach($categoryList as $listItem){
  47. $result["categoryList"][] = [
  48. "id" => $listItem->id,
  49. "name" => $listItem->getTranslation("name", $locate),
  50. ];
  51. }
  52. //影音列表
  53. $albums = Album::where("visible", 1);
  54. if($categoryId){
  55. $albums->where("album_category_id", $categoryId);
  56. }
  57. $albums = $albums->orderByDesc("order")->orderByDesc("post_date")->get();
  58. foreach($albums as $item){
  59. $result["list"][] = [
  60. "id" => $item->id,
  61. "categoryId" => $item->albumCategory->id,
  62. "category" => $item->albumCategory->getTranslation("name", $locate),
  63. "postDate" => Carbon::parse($item->post_date)->format("Y.m.d"),
  64. "title" => $item->getTranslation("title", $locate),
  65. "imgBanner" => $item->album_img_banner_url,
  66. "imgPC" => $item->album_img_pc_url,
  67. "imgMobile" => $item->album_img_mobile_url,
  68. "type" => $item->album_upload_type,
  69. "link" => $item->album_video_link,
  70. ];
  71. }
  72. return Response::ok($result);
  73. }
  74. }