From cefdb1b60fb116a7e6632378579b5834e428b079 Mon Sep 17 00:00:00 2001 From: liapples Date: Tue, 27 Jul 2021 18:40:44 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=A2=9E=E5=8A=A0recommend=E6=8E=A8?= =?UTF-8?q?=E8=8D=90=E4=BA=A7=E5=93=81=E5=88=97=E8=A1=A8=EF=BC=9B2?= =?UTF-8?q?=E3=80=81=E4=BA=A7=E5=93=81=E8=AF=A6=E6=83=85=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=8A=98=E6=89=A3=E8=AE=A1=E7=AE=97=EF=BC=9B3=E3=80=81?= =?UTF-8?q?=E5=85=B3=E8=81=94=E6=9F=A5=E8=AF=A2=E6=8C=87=E5=AE=9A=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=BB=A3=E7=A0=81=E7=AE=80=E5=8C=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/AgentProductController.php | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index 550561b..2b4e91a 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/app/Http/Controllers/Api/AgentProductController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\AgentProduct; +use App\Models\UserFav; /** * 代理商产品 @@ -22,7 +23,7 @@ class AgentProductController extends Controller $where['agent_id'] = $this->agent_id; $list = AgentProduct::where($where) - ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')]) + ->with('product:id,title,pictures') ->select('id', 'sale', 'product_id', 'price', 'original_price') ->orderBy('id', 'DESC') ->simplePaginate(); @@ -35,16 +36,27 @@ class AgentProductController extends Controller $id = (int)request()->input('id'); // TODO 优惠券查询待优化 - $data = AgentProduct::query() - ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures', 'know', 'content')]) - ->with(['coupon' => fn($query) => $query->select('id', 'tag', 'agent_product_id')]) + $agent_product = AgentProduct::query() + ->with('product:id,title,pictures,know,stock,content') + ->with('coupon:tag,agent_product_id') + ->with('fav:agent_product_id') ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => 1]); - if (!$data) { + if (!$agent_product || !$agent_product->product) { return $this->error('产品不存在或已下架'); } - unset($data->agent_id, $data->status, $data->created_at, $data->deleted_at); - return $this->success($data); + + $agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏 + //计算折扣 + if ($agent_product->price < $agent_product->original_price) { + $agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1); + } else { + $agent_product->cost = ''; + } + + + unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at); + return $this->success($agent_product); } // 猜你喜欢 @@ -52,10 +64,17 @@ class AgentProductController extends Controller { // TODO 此处需要再优化排序规则,并增加广告功能 $list = AgentProduct::where('agent_id', $this->agent_id) - ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')]) + ->with('product:id,title,pictures') ->select('id', 'sale', 'product_id', 'price', 'original_price') ->orderBy('id', 'DESC') ->simplePaginate(); return $this->success($list); } + + //我的下方推荐 + public function recommend() + { + //TODO 推荐数据暂时使用产品列表,后期需要通过后台设置获取或根据用户购买过的关键词获取 + return $this->index(); + } }