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); } }