优化页头数据读取

This commit is contained in:
Edward Yang 2022-08-15 12:47:18 +08:00
parent 0dbf34348c
commit ad90a9f5ca
5 changed files with 186 additions and 74 deletions

View File

@ -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

View File

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

View File

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

View File

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

View File

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