From b5f6249f872df99bbf709ed523dba34d28f7e0ee Mon Sep 17 00:00:00 2001 From: TL Date: Thu, 21 Jul 2022 18:32:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E5=8F=B0=E5=95=86=E5=93=81=E8=AF=A6?= =?UTF-8?q?=E6=83=85variables=E5=A4=9A=E8=AF=AD=E8=A8=80=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Http/Controllers/ProductController.php | 2 +- beike/Shop/Http/Resources/ProductDetail.php | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/beike/Shop/Http/Controllers/ProductController.php b/beike/Shop/Http/Controllers/ProductController.php index d4452bf5..d04a7d42 100644 --- a/beike/Shop/Http/Controllers/ProductController.php +++ b/beike/Shop/Http/Controllers/ProductController.php @@ -16,7 +16,7 @@ class ProductController extends Controller $data = [ 'product' => (new ProductDetail($product))->jsonSerialize(), ]; - +dd($data); return view('product', $data); } } diff --git a/beike/Shop/Http/Resources/ProductDetail.php b/beike/Shop/Http/Resources/ProductDetail.php index caa97164..e9abfd9e 100644 --- a/beike/Shop/Http/Resources/ProductDetail.php +++ b/beike/Shop/Http/Resources/ProductDetail.php @@ -23,8 +23,24 @@ class ProductDetail extends JsonResource 'description' => $this->description->description ?? '', 'image' => image_resize($this->image), 'category_id' => $this->category_id ?? null, - 'variables' => $this->variables, + 'variables' => $this->decodeVariables($this->variables), 'skus' => SkuDetail::collection($this->skus)->jsonSerialize(), ]; } + + private function decodeVariables($variables) + { + $lang = current_language_code(); + return array_map(function ($item) use ($lang){ + return [ + 'name' => $item['name'][$lang], + 'values' => array_map(function ($item) use ($lang){ + return [ + 'name' => $item['name'][$lang], + 'image' => image_resize($item['image'], 100, 100), + ]; + }, $item['values']), + ]; + }, $variables); + } }