where("id", $id)->firstOrFail(); $goods->view_total++; $goods->save(); $gmap = $goods->map; if (!\Auth::id()) { $gmap = $goods->map->where("is_public", 1); } return [ "id" => $goods->id, "name" => $goods->name, "en_name" => $goods->en_name, "category_name" => $gmap->where("is_public", 1)->map(function (GoodsCategoryMap $map) { return optional($map->category)->name; })->implode("/"), "en_category_name" => $gmap->where("is_public", 1)->map(function (GoodsCategoryMap $map) { return optional($map->category)->en_name; })->implode("/"), "image_list" => $goods->image_list, "spec" => array_map(function ($specId) { try { /** @var Spec $spec */ $spec = Spec::find($specId); return [ "id" => $spec->id, "name" => $spec->name, "en_name" => $spec->en_name, "is_custom" => $spec->is_custom, "attr" => $spec->attrs->map(function (SpecAttr $a) { return [ "id" => $a->id, "name" => $a->name, ]; }), ]; } catch (\Exception $e) { throw new ClientException("商品规格有误,请联系管理员"); } }, $goods->spec), "desc_html" => $goods->desc_html, "en_desc_html" => $goods->en_desc_html, "faq_html" => $goods->faq_html, "en_faq_html" => $goods->en_faq_html, "en_spec_attr_html" => $goods->en_spec_attr_html, "spec_attr_html" => $goods->spec_attr_html, "is_fav" => (int)$this->goodsIsFav($goods->id), "sn" => $goods->sn, "url_3d" => $goods->url_3d, "spec_map" => $goods->specMap->map(function (GoodsSpecMap $m) { return [ "id" => $m->id, "spec_id" => $m->spec_id, "value" => $m->value, "name" => $m->spec->name, "en_name" => $m->spec->en_name, ]; }), "custom" => array_map(function ($custom) { return [ "id" => $custom['id'], "title" => $custom['title'] ?? "", "en_title" => $custom['en_title'] ?? "", "attr" => array_filter(array_values(array_map(function ($attr) { if (is_null($attr['value'])) { return null; } return $attr; }, $custom['attr']))), ]; }, $goods->custom), ]; } protected function goodsIsFav($gid): bool { $ret = false; $uid = \Auth::id(); if ($uid) { if (UserFav::where("user_id", $uid)->whereGoodsId($gid)->first()) { $ret = true; } } return $ret; } }