From 79f6f77e2db111267cfc90c65abd3e3d825c5337 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Wed, 11 Jan 2023 17:30:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=88=86=E9=A1=B5=E6=95=B0?= =?UTF-8?q?=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Repositories/CategoryRepo.php | 27 ++++++++++++++----- .../Http/Controllers/CategoryController.php | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/beike/Repositories/CategoryRepo.php b/beike/Repositories/CategoryRepo.php index b2dc85e0..3c7ead05 100644 --- a/beike/Repositories/CategoryRepo.php +++ b/beike/Repositories/CategoryRepo.php @@ -67,6 +67,21 @@ class CategoryRepo return DB::select($sql); } + /** + * 生成每页产品数列表, 客户可以自己选择如何分页 + * + * @return array + */ + public static function getPerPages(): array + { + $perPages = []; + $configPerPage = system_setting('base.product_per_page', 20); + for ($index = 1; $index <= 5; $index++) { + $perPages[] = $configPerPage * $index; + } + return $perPages; + } + /** * 获取顶级及其子分类 */ @@ -150,9 +165,9 @@ class CategoryRepo $pathName .= $path->pathCategory->description->name; } $results[] = [ - 'id' => $category->id, + 'id' => $category->id, 'status' => $category->active, - 'name' => $pathName, + 'name' => $pathName, ]; } @@ -167,7 +182,7 @@ class CategoryRepo { if (is_int($category)) { $category = Category::query()->findOrFail($category); - } elseif (! ($category instanceof Category)) { + } elseif (!($category instanceof Category)) { throw new \Exception('invalid category'); } $category->descriptions()->delete(); @@ -183,7 +198,7 @@ class CategoryRepo */ public static function getName($category) { - $id = is_int($category) ? $category : $category->id; + $id = is_int($category) ? $category : $category->id; $categories = self::getAllCategoriesWithName(); return $categories[$id]['name'] ?? ''; @@ -199,11 +214,11 @@ class CategoryRepo return self::$allCategoryWithName; } - $items = []; + $items = []; $categories = self::getBuilder()->select('id')->get(); foreach ($categories as $category) { $items[$category->id] = [ - 'id' => $category->id, + 'id' => $category->id, 'name' => $category->description->name ?? '', ]; } diff --git a/beike/Shop/Http/Controllers/CategoryController.php b/beike/Shop/Http/Controllers/CategoryController.php index 49c45153..723f04e9 100644 --- a/beike/Shop/Http/Controllers/CategoryController.php +++ b/beike/Shop/Http/Controllers/CategoryController.php @@ -27,6 +27,7 @@ class CategoryController extends Controller 'filter_data' => ['attr' => ProductRepo::getFilterAttribute($filterData), 'price' => ProductRepo::getFilterPrice($filterData)], 'products_format' => $products->jsonSerialize(), 'products' => $products, + 'per_pages' => CategoryRepo::getPerPages(), ]; return view('category', $data);