|
|
<?php
namespace App\Http\Controllers\Api;use App\Http\Controllers\Controller;use App\Models\AgentProduct;use App\Models\Product;use Illuminate\Support\Facades\DB;
/** * 代理商产品 * Class AgentProductController * @package App\Http\Controllers\Api */class AgentProductController extends Controller{ // 代理商产品列表
public function index() { $channel_id = request()->input('channel_id'); if ($channel_id) { $where['channel_id'] = $channel_id; }
$where['agent_id'] = $this->agent_id;
$list = AgentProduct::where($where) ->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); }
// 产品详情
public function show() { $id = request()->input('id'); if (!$id || !ctype_digit($id)) { return $this->error('无效的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();
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); 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(); return $this->success($list); }}
|