You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.0 KiB
79 lines
2.0 KiB
<?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()
|
|
{
|
|
$category_id = request()->input('category_id');
|
|
if ($category_id) {
|
|
$where['category_id'] = $category_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);
|
|
}
|
|
}
|