From 4de04aec20165ff1455dc8907843676786be8ec7 Mon Sep 17 00:00:00 2001 From: liapples Date: Mon, 26 Jul 2021 18:43:30 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BA=A7=E5=93=81=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=A8=A1=E5=9E=8B=E6=9F=A5=E8=AF=A2=EF=BC=9B?= =?UTF-8?q?2=E3=80=81=E7=8C=9C=E4=BD=A0=E5=96=9C=E6=AC=A2=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E4=BC=98=E5=8C=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/AgentProductController.php | 44 ++++++------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index 4026e83..550561b 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/app/Http/Controllers/Api/AgentProductController.php @@ -3,8 +3,6 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\AgentProduct; -use App\Models\Product; -use Illuminate\Support\Facades\DB; /** * 代理商产品 @@ -34,46 +32,30 @@ class AgentProductController extends Controller // 产品详情 public function show() { - $id = request()->input('id'); - if (!$id || !ctype_digit($id)) { - return $this->error('无效的ID'); - } + $id = (int)request()->input('id'); - // TODO 此处待优化 - $data = DB::table('agent_products AS ap') - ->leftJoin('products AS p', 'p.id', '=', 'ap.product_id') - //->leftJoin('product_infos AS pi', 'p.id', '=', 'pi.product_id') - ->select('ap.*', 'p.title', 'p.pictures') - ->where('ap.id', $id) - ->where(['ap.status' => 1, 'p.status' => 1]) - ->first(); + // 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')]) + ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => 1]); if (!$data) { return $this->error('产品不存在或已下架'); } - $coupon_ids = explode(',', $data->coupon_ids); - $data->coupon_ids = DB::table('coupons')->whereIn('id', $coupon_ids)->pluck('tag'); - $data->pictures = json_decode($data->pictures, true); - unset($data->deleted_at); + unset($data->agent_id, $data->status, $data->created_at, $data->deleted_at); return $this->success($data); } - // 人气爆款 - /*public function hot() - { - $list = Product::get(['title', 'price', 'original_price', 'pictures', 'sale']); - return $this->success($list); - }*/ - // 猜你喜欢 public function guessLike() { - $page = request()->only('page'); - if ($page && !ctype_digit($page)) { - return $this->error('页码错误'); - } - - $list = Product::get(['title', 'price', 'original_price', 'pictures', 'sale'])->simplePaginate(); + // TODO 此处需要再优化排序规则,并增加广告功能 + $list = AgentProduct::where('agent_id', $this->agent_id) + ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')]) + ->select('id', 'sale', 'product_id', 'price', 'original_price') + ->orderBy('id', 'DESC') + ->simplePaginate(); return $this->success($list); } }