<?php namespace App\Modules\Mini\Services; use App\Base\BaseService; use App\Exceptions\ClientException; use App\Models\Goods\Goods; use App\Models\Goods\GoodsCategoryMap; use App\Models\Goods\GoodsSku; use App\Models\Goods\GoodsSpecMap; use App\Models\Goods\Spec; use App\Models\Goods\SpecAttr; use App\Models\User\UserFav; class GoodsService extends BaseService { public function info($params) { $id = $params['id']; $goods = Goods::where("status", Goods::STATUS_OK)->where("id", $id)->firstOrFail(); $goods->view_total++; $goods->save(); return [ "id" => $goods->id, "name" => $goods->name, "category_name" => $goods->map->where("is_public", 1)->map(function (GoodsCategoryMap $map) { return optional($map->category)->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, "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, "faq_html" => $goods->faq_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, ]; }), ]; } 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; } }