From 0d09cb7bf8a5841406f491faf845a9a473f468ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Tue, 30 Nov 2021 14:00:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E8=B7=AF=E7=94=B1=E5=88=86?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CategoryController.php} | 5 +-- .../HotController.php} | 12 +++---- .../{ => Product}/ProductController.php | 34 ++++++------------- app/Endpoint/Api/routes.php | 14 ++++---- app/Helpers/Paginator.php | 29 ++++++++++++++++ 5 files changed, 57 insertions(+), 37 deletions(-) rename app/Endpoint/Api/Http/Controllers/{ProductCategoryController.php => Product/CategoryController.php} (76%) rename app/Endpoint/Api/Http/Controllers/{HotProductController.php => Product/HotController.php} (59%) rename app/Endpoint/Api/Http/Controllers/{ => Product}/ProductController.php (74%) create mode 100644 app/Helpers/Paginator.php diff --git a/app/Endpoint/Api/Http/Controllers/ProductCategoryController.php b/app/Endpoint/Api/Http/Controllers/Product/CategoryController.php similarity index 76% rename from app/Endpoint/Api/Http/Controllers/ProductCategoryController.php rename to app/Endpoint/Api/Http/Controllers/Product/CategoryController.php index a0cde45d..cf61bec7 100644 --- a/app/Endpoint/Api/Http/Controllers/ProductCategoryController.php +++ b/app/Endpoint/Api/Http/Controllers/Product/CategoryController.php @@ -1,12 +1,13 @@ whereRelation('category', 'is_show', true) ->latest('sales') ->limit(20) ->get(); - return ProductSkuSimpleResource::collection($productSkus); + return ProductSkuSimpleResource::collection($skus); } } diff --git a/app/Endpoint/Api/Http/Controllers/ProductController.php b/app/Endpoint/Api/Http/Controllers/Product/ProductController.php similarity index 74% rename from app/Endpoint/Api/Http/Controllers/ProductController.php rename to app/Endpoint/Api/Http/Controllers/Product/ProductController.php index 40bb1524..85fda6ff 100644 --- a/app/Endpoint/Api/Http/Controllers/ProductController.php +++ b/app/Endpoint/Api/Http/Controllers/Product/ProductController.php @@ -1,8 +1,10 @@ filled('part') - ? $this->filterProductsByPart($request) - : $this->filterProducts($request) - ); + $skus = $request->filled('part') + ? $this->filterProductsByPart($request) + : $this->filterProducts($request); + + return ProductSkuSimpleResource::collection($skus); } /** @@ -44,7 +46,7 @@ class ProductController extends Controller ->filter($input) ->isRelease() ->whereRelation('category', 'is_show', true) - ->simplePaginate($this->getPerPage($request)); + ->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); } /** @@ -67,24 +69,10 @@ class ProductController extends Controller }) ->where('part_id', $productPart->id) ->latest('sort') - ->simplePaginate($this->getPerPage($request)); + ->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); 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; - } } diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index f6640748..c2c778f9 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -2,11 +2,11 @@ use App\Endpoint\Api\Http\Controllers\AdController; use App\Endpoint\Api\Http\Controllers\CaptchaController; -use App\Endpoint\Api\Http\Controllers\HotProductController; 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\Product\CategoryController; +use App\Endpoint\Api\Http\Controllers\Product\HotController; +use App\Endpoint\Api\Http\Controllers\Product\ProductController; use App\Endpoint\Api\Http\Controllers\RegisterController; use App\Endpoint\Api\Http\Controllers\SmsCodeController; use Illuminate\Support\Facades\Route; @@ -28,7 +28,9 @@ Route::group([ Route::get('ads', [AdController::class, 'index']); - Route::get('product-categories', [ProductCategoryController::class, 'index']); - Route::get('products', [ProductController::class, 'index']); - Route::get('hot-products', [HotProductController::class, 'index']); + Route::prefix('product')->group(function () { + Route::get('categories', [CategoryController::class, 'index']); + Route::get('hot', HotController::class); + Route::get('products', [ProductController::class, 'index']); + }); }); diff --git a/app/Helpers/Paginator.php b/app/Helpers/Paginator.php new file mode 100644 index 00000000..be680e16 --- /dev/null +++ b/app/Helpers/Paginator.php @@ -0,0 +1,29 @@ +input($perPageName); + + if ($perPage >= 1) { + if ($max !== null && $max >= 1 && $perPage >= $max) { + return $max; + } + + return $perPage; + } + + return $default; + } +}