过滤商品
parent
b436bbfdd7
commit
67b7bf3f55
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Filters;
|
||||
|
||||
use App\Models\ProductCategory;
|
||||
use EloquentFilter\ModelFilter;
|
||||
|
||||
class ProductSkuFilter extends ModelFilter
|
||||
{
|
||||
/**
|
||||
* 按商品分类搜索商品
|
||||
*
|
||||
* @param int $id
|
||||
*/
|
||||
public function category($id)
|
||||
{
|
||||
$category = ProductCategory::showable()->find($id);
|
||||
|
||||
if ($category === null) {
|
||||
return $this->whereRaw('1=0');
|
||||
}
|
||||
|
||||
$ids = $category->descendants()->showable()->pluck('id');
|
||||
|
||||
if ($ids->isEmpty()) {
|
||||
$this->where('category_id', $id);
|
||||
} else {
|
||||
$this->whereIn('category_id', $ids);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关键字搜索商品
|
||||
*
|
||||
* @param string $keyword
|
||||
*/
|
||||
public function keyword($keyword)
|
||||
{
|
||||
$this->whereLike('name', $keyword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品排序
|
||||
*
|
||||
* @param string $sort
|
||||
*/
|
||||
public function sort($sort)
|
||||
{
|
||||
$column = str_ireplace('-', '', $sort);
|
||||
|
||||
if (in_array($column, ['price', 'sales', 'release_at'])) {
|
||||
if ($column === 'price') {
|
||||
$column = 'sell_price';
|
||||
}
|
||||
|
||||
return $this->orderBy($column, strpos($sort, '-') === 0 ? 'desc' : 'asc');
|
||||
}
|
||||
|
||||
$this->orderBy('id', 'desc');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Controllers;
|
||||
|
||||
use App\Endpoint\Api\Http\Resources\ProductSku\ProductSkuSimpleResource;
|
||||
use App\Models\ProductPart;
|
||||
use App\Models\ProductPartSku;
|
||||
use App\Models\ProductSku;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
/**
|
||||
* 商品列表
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
return ProductSkuSimpleResource::collection(
|
||||
$request->filled('part')
|
||||
? $this->filterProductsByPart($request)
|
||||
: $this->filterProducts($request)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤商品
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Pagination\Paginator
|
||||
*/
|
||||
protected function filterProducts(Request $request): Paginator
|
||||
{
|
||||
$input = $request->input();
|
||||
|
||||
if ($request->isNotFilled('sort')) {
|
||||
$input['sort'] = '-id';
|
||||
}
|
||||
|
||||
return ProductSku::select(['id', 'name', 'cover', 'sell_price', 'vip_price', 'sales'])
|
||||
->filter($input)
|
||||
->isRelease()
|
||||
->whereRelation('category', 'is_show', true)
|
||||
->simplePaginate($this->getPerPage($request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 按分区过滤商品
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Pagination\Paginator
|
||||
*/
|
||||
protected function filterProductsByPart(Request $request): Paginator
|
||||
{
|
||||
$productPart = ProductPart::where('key', $request->input('part'))->first();
|
||||
|
||||
if ($productPart === null) {
|
||||
return new Paginator([], $this->getPerPage($request), Paginator::resolveCurrentPage());
|
||||
}
|
||||
|
||||
$paginator = ProductPartSku::with('sku:id,name,cover,sell_price,vip_price,sales')
|
||||
->whereHas('sku', function ($query) {
|
||||
$query->isRelease()->whereRelation('category', 'is_show', true);
|
||||
})
|
||||
->where('part_id', $productPart->id)
|
||||
->latest('sort')
|
||||
->simplePaginate($this->getPerPage($request));
|
||||
|
||||
return $paginator->through(function ($item) {
|
||||
return $item->sku;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
protected function getPerPage(Request $request): int
|
||||
{
|
||||
$perPage = (int) $request->input('per_page');
|
||||
|
||||
if ($perPage > 0) {
|
||||
return $perPage > 50 ? 50 : $perPage;
|
||||
}
|
||||
|
||||
return 20;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Resources\ProductSku;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductSkuSimpleResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'cover' => (string) $this->cover,
|
||||
'sell_price' => (string) $this->sell_price,
|
||||
'vip_price' => (string) $this->vip_price,
|
||||
'sales' => (string) $this->sales,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ use App\Endpoint\Api\Http\Controllers\CaptchaController;
|
|||
use App\Endpoint\Api\Http\Controllers\LoginController;
|
||||
use App\Endpoint\Api\Http\Controllers\LogoutController;
|
||||
use App\Endpoint\Api\Http\Controllers\ProductCategoryController;
|
||||
use App\Endpoint\Api\Http\Controllers\ProductController;
|
||||
use App\Endpoint\Api\Http\Controllers\RegisterController;
|
||||
use App\Endpoint\Api\Http\Controllers\SmsCodeController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
|
@ -27,4 +28,5 @@ Route::group([
|
|||
Route::get('ads', [AdController::class, 'index']);
|
||||
|
||||
Route::get('product-categories', [ProductCategoryController::class, 'index']);
|
||||
Route::get('products', [ProductController::class, 'index']);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -17,13 +17,34 @@ class ProductCategory extends Model
|
|||
use ModelTree;
|
||||
use HasDateTimeFormatter;
|
||||
|
||||
protected $table = 'product_categories';
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [
|
||||
'is_show' => false,
|
||||
'is_recommend' => false,
|
||||
'sort' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'is_show' => 'boolean',
|
||||
'is_recommend' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'icon',
|
||||
'is_show',
|
||||
'is_recommend',
|
||||
'sort',
|
||||
];
|
||||
|
||||
// 排序字段名称,默认值为 order
|
||||
protected $orderColumn = 'sort';
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue