重构面包屑

This commit is contained in:
Edward Yang 2022-08-17 23:40:48 +08:00
parent 9984dfd3d0
commit 58b32aa523
13 changed files with 201 additions and 18 deletions

View File

@ -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]);

View File

@ -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'] ?? '';
}

View File

@ -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'] ?? '';
}

View File

@ -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(),
];

View File

@ -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
]);
}
}

View File

@ -0,0 +1,125 @@
<?php
/**
* BreadCrumb.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @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'],
]
];
}
}

View File

@ -0,0 +1,14 @@
<?php
/**
* account.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-04 10:59:15
* @modified 2022-08-04 10:59:15
*/
return [
'index' => 'Brand List',
];

View File

@ -0,0 +1,14 @@
<?php
/**
* common.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-17 23:10:20
* @modified 2022-08-17 23:10:20
*/
return [
'home' => 'Home'
];

View File

@ -0,0 +1,14 @@
<?php
/**
* common.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-17 23:10:20
* @modified 2022-08-17 23:10:20
*/
return [
'home' => '首页'
];

View File

@ -4,12 +4,7 @@
@section('content')
<div class="container">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active" aria-current="page">Library</li>
</ol>
</nav>
<x-shop-breadcrumb type="brand" :value="$brand" />
<div class="row">
@foreach ($products as $product)

View File

@ -4,7 +4,7 @@
@section('content')
<div class="container">
{{ Diglactic\Breadcrumbs\Breadcrumbs::render('category', $category) }}
<x-shop-breadcrumb type="category" :value="$category" />
<div class="row">
@foreach ($products as $product)

View File

@ -0,0 +1,15 @@
@unless ($breadcrumbs->isEmpty())
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
@foreach ($breadcrumbs as $breadcrumb)
@if (isset($breadcrumb['url']) && $breadcrumb['url'])
<li class="breadcrumb-item"><a href="{{ $breadcrumb['url'] }}">{{ $breadcrumb['title'] }}</a></li>
@else
<li class="breadcrumb-item active" aria-current="page">{{ $breadcrumb['title'] }}</li>
@endif
@endforeach
</ol>
</nav>
@endunless

View File

@ -12,7 +12,7 @@
@section('content')
<div class="container" id="product-app" v-cloak>
{{ Diglactic\Breadcrumbs\Breadcrumbs::render('product', $product) }}
<x-shop-breadcrumb type="product" :value="$product['id']" />
<div class="row mb-5" id="product-top">
<div class="col-12 col-lg-6 mb-3">