From 995848e0d828cd5e908bc4b8050e2909550c3e49 Mon Sep 17 00:00:00 2001 From: liapples Date: Sun, 26 Sep 2021 14:03:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B0=E7=BB=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/AgentProductController.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index e7fcdd0..a6062de 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/app/Http/Controllers/Api/AgentProductController.php @@ -106,6 +106,35 @@ class AgentProductController extends Controller $agent_product->product = null; } + //处理规格 + if (!$agent_product->spec->isEmpty()) { + $specs = $agent_product->spec->toArray(); + + //二维数组转一维 + $specs = array_map(function ($v) { + if (is_array($v['product_spec'])) { + unset($v['product_spec']['id']); + $v = array_merge($v, $v['product_spec']); + } + unset($v['agent_product_id'], $v['product_spec_id'], $v['product_spec']); + return $v; + }, $specs); + + //去年name和date为空的数组 + $specs = array_filter($specs, fn($v) => isset($v['name'], $v['date'])); + + $names = array_column($specs, 'name'); + $names = array_values(array_unique($names)); + + $result = []; + foreach ($names as $name) { + $list = array_filter($specs, fn($v) => $v['name'] == $name); + $result[] = ['name' => $name, 'list' => array_column($list, null, 'date')]; + } + unset($agent_product->spec); + $agent_product->spec = $result; + } + unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at); return $this->success($agent_product); }