diff --git a/beike/Helpers.php b/beike/Helpers.php index 7962f34e..ede6b668 100644 --- a/beike/Helpers.php +++ b/beike/Helpers.php @@ -118,6 +118,7 @@ function plugin_route($route, $params = []): string * @param $type * @param $value * @return string + * @throws Exception */ function type_route($type, $value): string { @@ -125,6 +126,9 @@ function type_route($type, $value): string if (empty($type) || empty($value) || !in_array($type, $types)) { return ''; } + if (is_array($value)) { + throw new \Exception('Value must be integer, string or object'); + } if ($type == 'category') { return shop_route('categories.show', ['category' => $value]); diff --git a/beike/Repositories/BrandRepo.php b/beike/Repositories/BrandRepo.php index 7e3148d2..907de8cc 100644 --- a/beike/Repositories/BrandRepo.php +++ b/beike/Repositories/BrandRepo.php @@ -163,11 +163,12 @@ class BrandRepo /** * 通过品牌ID获取品牌名称 - * @param $id + * @param $brand * @return mixed|string */ - public static function getName($id) + public static function getName($brand) { + $id = is_int($brand) ? $brand : $brand->id; $categories = self::getAllBrandsWithName(); return $categories[$id]['name'] ?? ''; } diff --git a/beike/Repositories/CategoryRepo.php b/beike/Repositories/CategoryRepo.php index 227bed06..fb845e29 100644 --- a/beike/Repositories/CategoryRepo.php +++ b/beike/Repositories/CategoryRepo.php @@ -140,11 +140,12 @@ class CategoryRepo /** * 通过分类ID获取产品名称 - * @param $id + * @param $category * @return mixed|string */ - public static function getName($id) + public static function getName($category) { + $id = is_int($category) ? $category : $category->id; $categories = self::getAllCategoriesWithName(); return $categories[$id]['name'] ?? ''; } diff --git a/beike/Shop/Http/Controllers/BrandController.php b/beike/Shop/Http/Controllers/BrandController.php index fe4929eb..b02b4906 100644 --- a/beike/Shop/Http/Controllers/BrandController.php +++ b/beike/Shop/Http/Controllers/BrandController.php @@ -20,8 +20,8 @@ class BrandController extends Controller public function show(int $id) { - $products = BrandRepo::find($id) - ->products() + $brand = BrandRepo::find($id); + $products = $brand->products() ->with([ 'master_sku', 'description', @@ -30,6 +30,7 @@ class BrandController extends Controller ->paginate(20); $data = [ + 'brand' => $brand, 'products' => ProductSimple::collection($products)->jsonSerialize(), ]; diff --git a/beike/Shop/Providers/ShopServiceProvider.php b/beike/Shop/Providers/ShopServiceProvider.php index 88e4480a..fe94a284 100644 --- a/beike/Shop/Providers/ShopServiceProvider.php +++ b/beike/Shop/Providers/ShopServiceProvider.php @@ -6,13 +6,11 @@ use Beike\Libraries\Tax; use Beike\Models\Customer; use Illuminate\Support\Str; use Illuminate\View\FileViewFinder; -use TorMorten\Eventy\Facades\Eventy; -use Beike\Repositories\CategoryRepo; -use Illuminate\Support\Facades\View; +use Beike\Shop\View\Components\Alert; use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; +use Beike\Shop\View\Components\Breadcrumb; use Beike\Shop\View\Components\AccountSidebar; -use Beike\Shop\View\Components\Alert; class ShopServiceProvider extends ServiceProvider { @@ -88,6 +86,7 @@ class ShopServiceProvider extends ServiceProvider $this->loadViewComponentsAs('shop', [ 'sidebar' => AccountSidebar::class, 'alert' => Alert::class, + 'breadcrumb' => Breadcrumb::class ]); } } diff --git a/beike/Shop/View/Components/Breadcrumb.php b/beike/Shop/View/Components/Breadcrumb.php new file mode 100644 index 00000000..8b869888 --- /dev/null +++ b/beike/Shop/View/Components/Breadcrumb.php @@ -0,0 +1,125 @@ + + * @created 2022-08-17 22:45:42 + * @modified 2022-08-17 22:45:42 + */ + +namespace Beike\Shop\View\Components; + +use Beike\Models\Product; +use Illuminate\Contracts\View\View; +use Illuminate\Support\Collection; +use Illuminate\View\Component; + +class Breadcrumb extends Component +{ + public Collection $breadcrumbs; + + /** + * Create a new component instance. + * + * @return void + */ + public function __construct($type, $value, array $text = []) + { + $breadcrumbs[] = [ + 'title' => trans('shop/common.home'), + 'url' => shop_route('home.index') + ]; + + if ($type == 'category') { + $breadcrumbs = array_merge($breadcrumbs, $this->handleCategoryLinks($value)); + } elseif ($type == 'product') { + $breadcrumbs = array_merge($breadcrumbs, $this->handleProductLinks($value)); + } else { + $breadcrumbs = array_merge($breadcrumbs, $this->handleLinks($type, $value, $text)); + } + + $this->breadcrumbs = collect($breadcrumbs); + } + + /** + * Get the view / contents that represent the component. + * + * @return View| + */ + public function render(): View + { + return view('components.breadcrumbs'); + } + + + /** + * 获取分类以及路径 + * + * @param $value + * @return array + */ + private function handleCategoryLinks($value): array + { + $link = handle_link(['type' => 'category', 'value' => $value]); + return [ + [ + 'title' => $link['text'], + 'url' => $link['link'], + ] + ]; + } + + /** + * 获取产品以及分类路径 + * + * @param $value + * @return array + */ + private function handleProductLinks($value): array + { + $links = []; + $productId = 0; + if (is_array($value)) { + $productId = $value['id'] ?? 0; + } elseif (is_int($value)) { + $productId = $value; + } + $product = Product::query()->find($productId); + $category = $product->categories()->first(); + if ($category) { + $categoryLink = handle_link(['type' => 'category', 'value' => $category]); + $links[] = ['title' => $categoryLink['text'], 'url' => $categoryLink['link']]; + } + + $productLink = handle_link(['type' => 'product', 'value' => $value]); + $links[] = ['title' => $productLink['text'], 'url' => $productLink['link']]; + + return $links; + } + + /** + * 获取普通链接 + * + * @param $type + * @param $value + * @param array $text + * @return array + */ + private function handleLinks($type, $value, array $text = []): array + { + $data = [ + 'type' => $type, + 'value' => $value, + 'text' => $text, + ]; + $link = handle_link($data); + return [ + [ + 'title' => $link['text'], + 'url' => $link['link'], + ] + ]; + } +} diff --git a/resources/lang/en/shop/brands.php b/resources/lang/en/shop/brands.php new file mode 100644 index 00000000..0b901214 --- /dev/null +++ b/resources/lang/en/shop/brands.php @@ -0,0 +1,14 @@ + + * @created 2022-08-04 10:59:15 + * @modified 2022-08-04 10:59:15 + */ + +return [ + 'index' => 'Brand List', +]; diff --git a/resources/lang/en/shop/common.php b/resources/lang/en/shop/common.php new file mode 100644 index 00000000..da514df7 --- /dev/null +++ b/resources/lang/en/shop/common.php @@ -0,0 +1,14 @@ + + * @created 2022-08-17 23:10:20 + * @modified 2022-08-17 23:10:20 + */ + +return [ + 'home' => 'Home' +]; diff --git a/resources/lang/zh_cn/shop/common.php b/resources/lang/zh_cn/shop/common.php new file mode 100644 index 00000000..ef59b627 --- /dev/null +++ b/resources/lang/zh_cn/shop/common.php @@ -0,0 +1,14 @@ + + * @created 2022-08-17 23:10:20 + * @modified 2022-08-17 23:10:20 + */ + +return [ + 'home' => '首页' +]; diff --git a/themes/default/brand/info.blade.php b/themes/default/brand/info.blade.php index 9b241a31..1dc59468 100644 --- a/themes/default/brand/info.blade.php +++ b/themes/default/brand/info.blade.php @@ -4,12 +4,7 @@ @section('content')