EsgController.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Esg;
  5. use App\Models\EsgMain;
  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. use App\Http\Helper\Helper;
  12. use Illuminate\Support\Facades\Storage;
  13. /**
  14. * @group Lottery Prize
  15. */
  16. class EsgController extends Controller
  17. {
  18. public function __construct(
  19. )
  20. {
  21. }
  22. public function main($locate = 'tw')
  23. {
  24. $data = [];
  25. $esgMain = EsgMain::first();
  26. $locate = $locate == "tw" ? "zh_TW" : $locate;
  27. $data = [
  28. "meta" => [
  29. "meta_title" => $esgMain->meta_title,
  30. "meta_keyword" => $esgMain->meta_keyword,
  31. "meta_description" => $esgMain->meta_description,
  32. "og_img" => $esgMain->og_img_url
  33. ],
  34. "img_pc" => $esgMain->img_pc_url,
  35. "img_mobile" => $esgMain->img_mobile_url,
  36. "title" => $esgMain->getTranslation("title", $locate, false),
  37. "description" => $esgMain->getTranslation("description", $locate, false),
  38. ];
  39. $esgs = Esg::select("title", "keyword")->get();
  40. foreach($esgs as $esg){
  41. $data["tab_list"][] = [
  42. "text" => $esg->getTranslation("title", $locate, false),
  43. "key" => $esg->keyword
  44. ];
  45. }
  46. return response()->json(["data" => $data], 200);
  47. }
  48. public function esgContent($locate, $keyword){
  49. $locate = $locate == "tw" ? "zh_TW" : $locate;
  50. if (!$esg = Esg::getContentByKeyword($keyword)) {
  51. return response()->json([
  52. 'status' => "fail",
  53. 'message' => '查無資料'
  54. ]);
  55. }
  56. $data = [
  57. "banner" => [
  58. "img_pc" => $esg->banner_pc_url,
  59. "img_mobile" => $esg->banner_mobile_url,
  60. "img_alt" => $esg->img_alt,
  61. "text" => $esg->description ?? "",
  62. ],
  63. "paragraphs" => $this->prepareOutputContent($esg->paragraphs->toArray(), $locate)
  64. ];
  65. return response()->json(["data" => $data], 200);
  66. }
  67. private function prepareOutputContent($paragraphs, $locate): array
  68. {
  69. $output = [];
  70. // 調試關聯資料
  71. if($paragraphs){
  72. foreach($paragraphs as $paragraph){
  73. $content = $paragraph["content"];
  74. switch ($paragraph["type"]){
  75. case "1":
  76. $output[] = [
  77. "type" => "text",
  78. "content" => $content["text_content"][$locate]
  79. ];
  80. break;
  81. case "2":
  82. $output[] = [
  83. "type" => "grid",
  84. "columns" => count($content["text_blocks"]),
  85. "content" => array_map(function($block) use ($locate) {
  86. return [
  87. "type" => "text",
  88. "content" => $block["block_content"][$locate]
  89. ];
  90. }, $content["text_blocks"])
  91. ];
  92. break;
  93. case "3":
  94. $column_count = $content["column_count"];
  95. $table_header = [];
  96. $table_content = [];
  97. for($i=1; $i<=$column_count; $i++){
  98. $table_header[] = [
  99. "align" => $content["head_align" . strval($i)][$locate],
  100. "text" => $content["head" . strval($i)][$locate]
  101. ];
  102. $table_row_content = [];
  103. foreach($content["simple_table_rows"] as $table_row){
  104. $table_row_content[] = [
  105. "align" => $table_row["align" . strval($i)][$locate],
  106. "text" => $table_row["col" . strval($i)][$locate]
  107. ];
  108. }
  109. $table_content[] = $table_row_content;
  110. }
  111. $output[] = [
  112. "type" => "table",
  113. "is_card" => boolval($content["is_card"]),
  114. "header" => $table_header,
  115. "body" => $table_content
  116. ];
  117. break;
  118. case "4":
  119. $output[] = [
  120. "type" => "image",
  121. "images" => array_map(function($image) use ($locate) {
  122. return [
  123. "image_url" => Storage::disk("public")->url($image["image_url"]),
  124. "alt" => $image["image_alt"][$locate]
  125. ];
  126. }, $content["multiple_images"])
  127. ];
  128. break;
  129. }
  130. }
  131. }
  132. return $output;
  133. }
  134. }