123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?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();
- $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;
- }
- }
|