From ad90a9f5ca377340c0aaa92ab1fe0de0018a2188 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Mon, 15 Aug 2022 12:47:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A1=B5=E5=A4=B4=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Helpers.php | 60 ++++++++++++++++-- beike/Repositories/BrandRepo.php | 95 +++++++++++++++++++---------- beike/Repositories/CategoryRepo.php | 4 +- beike/Repositories/MenuRepo.php | 43 +++---------- beike/Repositories/PageRepo.php | 58 ++++++++++++++++++ 5 files changed, 186 insertions(+), 74 deletions(-) create mode 100644 beike/Repositories/PageRepo.php diff --git a/beike/Helpers.php b/beike/Helpers.php index 8cf86bde..160e9aa2 100644 --- a/beike/Helpers.php +++ b/beike/Helpers.php @@ -3,7 +3,11 @@ use Beike\Models\Customer; use Beike\Models\Language; use Beike\Models\AdminUser; +use Beike\Repositories\BrandRepo; +use Beike\Repositories\CategoryRepo; use Beike\Repositories\CurrencyRepo; +use Beike\Repositories\PageRepo; +use Beike\Repositories\ProductRepo; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Str; use Illuminate\Support\Collection; @@ -109,7 +113,7 @@ function plugin_route($route, $params = []): string } /** - * 获取 product, category, brand, page 路由链接 + * 获取 category, product, brand, page, static, custom 路由链接 * * @param $type * @param $value @@ -117,22 +121,66 @@ function plugin_route($route, $params = []): string */ function type_route($type, $value): string { - if (empty($type) || empty($value)) { + $types = ['category', 'product', 'brand', 'page', 'static', 'custom']; + if (empty($type) || empty($value) || !in_array($type, $types)) { return ''; } - if ($type == 'product') { - return shop_route('products.show', ['product' => $value]); - } elseif ($type == 'category') { + + if ($type == 'category') { return shop_route('categories.show', ['category' => $value]); + } elseif ($type == 'product') { + return shop_route('products.show', ['product' => $value]); } elseif ($type == 'brand') { return shop_route('brands.show', [$value]); } elseif ($type == 'page') { return shop_route('pages.show', ['page' => $value]); + } elseif ($type == 'static') { + return shop_route($value); + } elseif ($type == 'custom') { + return $value; } - return ''; } + +/** + * 获取 category, product, brand, page, static, custom 链接名称 + * + * @param $type + * @param $value + * @param array $texts + * @return string + */ +function type_label($type, $value, array $texts = []): string +{ + $types = ['category', 'product', 'brand', 'page', 'static', 'custom']; + if (empty($type) || empty($value) || !in_array($type, $types)) { + return ''; + } + + $locale = locale(); + $text = $texts[$locale] ?? ''; + if ($text) { + return $text; + } + + if ($type == 'category') { + return CategoryRepo::getName($value); + } elseif ($type == 'product') { + return ProductRepo::getName($value); + } elseif ($type == 'brand') { + return BrandRepo::getName($value); + } elseif ($type == 'page') { + return PageRepo::getName($value); + } elseif ($type == 'static') { + return trans('shop/' . $value); + } elseif ($type == 'custom') { + return $text; + } + return ''; +} + + /** * 是否访问的后端 * @return bool diff --git a/beike/Repositories/BrandRepo.php b/beike/Repositories/BrandRepo.php index 8fb4ab5a..996be1ac 100644 --- a/beike/Repositories/BrandRepo.php +++ b/beike/Repositories/BrandRepo.php @@ -22,6 +22,8 @@ use Illuminate\Database\Eloquent\Model; class BrandRepo { + private static $allBrandsWithName; + /** * 创建一个记录 * @param $data @@ -72,27 +74,38 @@ class BrandRepo } /** - * @param $data + * @param $filters * @return LengthAwarePaginator */ - public static function list($data): LengthAwarePaginator + public static function list($filters): LengthAwarePaginator { - $builder = Brand::query(); - - if (isset($data['name'])) { - $builder->where('name', 'like', "%{$data['name']}%"); - } - if (isset($data['first'])) { - $builder->where('first', $data['email']); - } - if (isset($data['status'])) { - $builder->where('status', $data['status']); - } - $builder->orderByDesc('created_at'); - + $builder = self::getBuilder($filters); return $builder->paginate(10)->withQueryString(); } + + /** + * 获取产品品牌筛选builder + * @param array $filters + * @return Builder + */ + public static function getBuilder(array $filters = []): Builder + { + $builder = Brand::query(); + if (isset($filters['name'])) { + $builder->where('name', 'like', "%{$filters['name']}%"); + } + if (isset($filters['first'])) { + $builder->where('first', $filters['email']); + } + if (isset($filters['status'])) { + $builder->where('status', $filters['status']); + } + $builder->orderByDesc('created_at'); + return $builder; + } + + public static function listGroupByFirst(): array { $brands = Brand::query()->where('status', true)->get(); @@ -117,21 +130,6 @@ class BrandRepo return $builder->limit(10)->get(); } - /** - * 获取商品名称 - * @param $id - * @return HigherOrderBuilderProxy|mixed|string - */ - public static function getName($id) - { - $brand = Brand::query()->find($id); - - if ($brand) { - return $brand->name; - } - return ''; - } - /** * @param $ids @@ -157,9 +155,42 @@ class BrandRepo if (empty($ids)) { return []; } - $brands = Brand::query() + return Brand::query() ->whereIn('id', $ids) ->get(); - return $brands; + } + + + /** + * 通过品牌ID获取品牌名称 + * @param $id + * @return mixed|string + */ + public static function getName($id) + { + $categories = self::getAllBrandsWithName(); + return $categories[$id]['name'] ?? ''; + } + + + /** + * 获取所有商品分类ID和名称列表 + * @return array|null + */ + public static function getAllBrandsWithName(): ?array + { + if (self::$allBrandsWithName !== null) { + return self::$allBrandsWithName; + } + + $items = []; + $brands = self::getBuilder()->select('id')->get(); + foreach ($brands as $brand) { + $items[$brand->id] = [ + 'id' => $brand->id, + 'name' => $brand->name ?? '', + ]; + } + return self::$allBrandsWithName = $items; } } diff --git a/beike/Repositories/CategoryRepo.php b/beike/Repositories/CategoryRepo.php index 264d64fa..227bed06 100644 --- a/beike/Repositories/CategoryRepo.php +++ b/beike/Repositories/CategoryRepo.php @@ -145,7 +145,7 @@ class CategoryRepo */ public static function getName($id) { - $categories = self::getAllCategoryWithName(); + $categories = self::getAllCategoriesWithName(); return $categories[$id]['name'] ?? ''; } @@ -154,7 +154,7 @@ class CategoryRepo * 获取所有商品分类ID和名称列表 * @return array|null */ - public static function getAllCategoryWithName(): ?array + public static function getAllCategoriesWithName(): ?array { if (self::$allCategoryWithName !== null) { return self::$allCategoryWithName; diff --git a/beike/Repositories/MenuRepo.php b/beike/Repositories/MenuRepo.php index 6713a1cc..3ad94b53 100644 --- a/beike/Repositories/MenuRepo.php +++ b/beike/Repositories/MenuRepo.php @@ -22,9 +22,10 @@ use Beike\Repositories\BrandRepo; class MenuRepo { /** - * 处理页尾编辑器数据 + * 处理页头编辑器数据 * * @return array|mixed + * @throws \Exception */ public static function handleMenuData($MenuSetting = []) { @@ -71,43 +72,17 @@ class MenuRepo /** * 处理链接 * - * @param $type - * @param $value - * @return string + * @param $link + * @return array */ private static function handleLink($link): array { - $locale = locale(); + $type = $link['type'] ?? ''; + $value = $link['value'] ?? ''; + $texts = $link['text'] ?? []; - if ($link['type'] == 'custom') { - $link['link'] = $link['value']; - $link['text'] = $link['text'][$locale]; - } elseif ($link['type'] == 'static') { - $link['link'] = shop_route($link['value']); - $link['text'] = $link['text'][$locale] ?: trans('shop/' . $link['value']); - } elseif ($link['type'] == 'page') { - $pageId = $link['value']; - $page = Page::query()->find($pageId); - if ($page) { - $link['link'] = type_route('page', $link['value']); - $link['text'] = $link['text'][$locale] ?: $page->description->title; - } - } elseif ($link['type'] == 'category') { - $categoryName = CategoryRepo::getName($link['value']); - if ($categoryName) { - $link['link'] = type_route('category', $link['value']); - $link['text'] = $categoryName; - } - } elseif ($link['type'] == 'product') { - $link['link'] = type_route('product', $link['value']); - $link['text'] = $link['text'][$locale] ?: ProductRepo::getName($link['value']); - } elseif ($link['type'] == 'brand') { - $link['link'] = type_route('brand', $link['value']); - $link['text'] = $link['text'][$locale] ?: BrandRepo::getName($link['value']); - } else { - $link['link'] = ''; - $link['text'] = $link['text'][$locale]; - } + $link['link'] = type_route($type, $value); + $link['text'] = type_label($type, $value, $texts); return $link; } diff --git a/beike/Repositories/PageRepo.php b/beike/Repositories/PageRepo.php new file mode 100644 index 00000000..d620dbb0 --- /dev/null +++ b/beike/Repositories/PageRepo.php @@ -0,0 +1,58 @@ + + * @created 2022-08-15 12:37:25 + * @modified 2022-08-15 12:37:25 + */ + +namespace Beike\Repositories; + +use Beike\Models\Page; +use Illuminate\Database\Eloquent\Builder; + +class PageRepo +{ + private static $allPagesWithName; + + public static function getBuilder(): Builder + { + return Page::query()->with('description'); + } + + /** + * 通过品牌ID获取单页名称 + * @param $id + * @return mixed|string + */ + public static function getName($id) + { + $categories = self::getAllPagesWithName(); + return $categories[$id]['name'] ?? ''; + } + + + /** + * 获取所有单页ID和名称列表 + * @return array|null + */ + public static function getAllPagesWithName(): ?array + { + if (self::$allPagesWithName !== null) { + return self::$allPagesWithName; + } + + $items = []; + $pages = self::getBuilder()->select('id')->get(); + foreach ($pages as $brand) { + $items[$brand->id] = [ + 'id' => $brand->id, + 'name' => $brand->description->title ?? '', + ]; + } + return self::$allPagesWithName = $items; + } +}