123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
-
- namespace App\Http\Controllers\Api;
-
- use App\Http\Controllers\Controller;
- use App\Models\Esg;
- use App\Models\EsgMain;
- use App\Supports\Response;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use App\Http\Helper\Helper;
- use Illuminate\Support\Facades\Storage;
-
- /**
- * @group Lottery Prize
- */
- class EsgController extends Controller
- {
- public function __construct(
- )
- {
- }
-
- public function main($locate = 'tw')
- {
- $data = [];
- $esgMain = EsgMain::first();
- $locate = $locate == "tw" ? "zh_TW" : $locate;
- $data = [
- "meta" => [
- "meta_title" => $esgMain->meta_title,
- "meta_keyword" => $esgMain->meta_keyword,
- "meta_description" => $esgMain->meta_description,
- "og_img" => $esgMain->og_img_url
- ],
- "img_pc" => $esgMain->img_pc_url,
- "img_mobile" => $esgMain->img_mobile_url,
- "title" => $esgMain->getTranslation("title", $locate, false),
- "description" => $esgMain->getTranslation("description", $locate, false),
- ];
- $esgs = Esg::select("title", "keyword")->get();
- foreach($esgs as $esg){
- $data["tab_list"][] = [
- "text" => $esg->getTranslation("title", $locate, false),
- "key" => $esg->keyword
- ];
- }
- return response()->json(["data" => $data], 200);
- }
-
- public function esgContent($locate, $keyword){
-
- $locate = $locate == "tw" ? "zh_TW" : $locate;
- if (!$esg = Esg::getContentByKeyword($keyword)) {
- return response()->json([
- 'status' => "fail",
- 'message' => '查無資料'
- ]);
- }
-
- $data = [
- "banner" => [
- "img_pc" => $esg->banner_pc_url,
- "img_mobile" => $esg->banner_mobile_url,
- "img_alt" => $esg->img_alt,
- "text" => $esg->description ?? "",
- ],
- "paragraphs" => $this->prepareOutputContent($esg->paragraphs->toArray(), $locate)
- ];
-
- return response()->json(["data" => $data], 200);
- }
-
- private function prepareOutputContent($paragraphs, $locate): array
- {
- $output = [];
- // 調試關聯資料
- if($paragraphs){
- foreach($paragraphs as $paragraph){
- $content = $paragraph["content"];
- switch ($paragraph["type"]){
- case "1":
- $output[] = [
- "type" => "text",
- "content" => $content["text_content"][$locate]
- ];
- break;
- case "2":
- $output[] = [
- "type" => "grid",
- "columns" => count($content["text_blocks"]),
- "content" => array_map(function($block) use ($locate) {
- return [
- "type" => "text",
- "content" => $block["block_content"][$locate]
- ];
- }, $content["text_blocks"])
- ];
- break;
- case "3":
- $column_count = $content["column_count"];
- $table_header = [];
- $table_content = [];
- for($i=1; $i<=$column_count; $i++){
- $table_header[] = [
- "align" => $content["head_align" . strval($i)][$locate],
- "text" => $content["head" . strval($i)][$locate]
- ];
- $table_row_content = [];
- foreach($content["simple_table_rows"] as $table_row){
- $table_row_content[] = [
- "align" => $table_row["align" . strval($i)][$locate],
- "text" => $table_row["col" . strval($i)][$locate]
- ];
- }
- $table_content[] = $table_row_content;
- }
- $output[] = [
- "type" => "table",
- "is_card" => boolval($content["is_card"]),
- "header" => $table_header,
- "body" => $table_content
- ];
- break;
- case "4":
- $output[] = [
- "type" => "image",
- "images" => array_map(function($image) use ($locate) {
- return [
- "image_url" => Storage::url($image["image_url"]),
- "alt" => $image["image_alt"][$locate]
- ];
- }, $content["multiple_images"])
- ];
- break;
- }
- }
- }
- return $output;
- }
- }
|