123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Services\LiquidRenderer;
- use App\Models\DistProduct;
- use App\Models\DistProductCategory; // 引入分类模型
- use App\Models\SitePage;
- class ProductController extends Controller
- {
- protected $liquidRenderer;
- public function __construct(LiquidRenderer $liquidRenderer)
- {
- $this->liquidRenderer = $liquidRenderer;
- }
- /**
- * Display a listing of the products.
- *
- * @return \Illuminate\Http\Response
- */
- // public function index()
- // {
- // $products = Product::paginate(10); // 每页显示10个产品
- // return $this->liquidRenderer->render('products.index', ['products' => $products]);
- // }
- public function category($slug)
- {
- // $products = DistProduct::paginate(10); // 每页显示10个产品
- // return $this->liquidRenderer->render('products_categories.liquid', ['products' => $products]);
- // 获取分类信息
- // 获取分类信息,并限定 dist_id
- $category = DistProductCategory::where(function ($query) use ($slug) {
- $query->where('slug', $slug)
- ->orWhere('id', $slug);
- })
- ->where('dist_id', getDistId())
- ->firstOrFail();
- if (!$category) {
- abort('404');
- }
- $categoryIds = [$category->id];
- if ($category) {
- //找下一级分类
- $subCategories = DistProductCategory::where('parent_id', $category->id)->get();
- foreach ($subCategories as $subCategory) {
- $categoryIds[] = $subCategory->id;
- }
- }
- // 获取分类下的所有产品,排序,然后进行分页
- $products = DistProduct::whereIn('category_id', $categoryIds)
- ->where('dist_id', getDistId())
- ->where('enabled', 1)
- ->where('status', 2)
- ->with('images') // Eager load images
- ->orderBy('order', 'desc') // 按 order 字段降序排序
- ->orderBy('id', 'desc') // 按 id 降序排序
- ->paginate(12);
- // 创建分页数据结构
- $paginator = [
- 'previous_page' => $products->previousPageUrl() ? true : false, // 是否有上一页
- 'previous_page_url' => $products->previousPageUrl(), // 上一页的 URL
- 'next_page' => $products->nextPageUrl() ? true : false, // 是否有下一页
- 'next_page_url' => $products->nextPageUrl(), // 下一页的 URL
- 'current_page' => $products->currentPage(), // 当前页
- 'total_pages' => $products->lastPage(), // 总页数
- 'pages' => range(1, $products->lastPage()), // 页码数组
- 'page_url' => array_combine(
- range(1, $products->lastPage()),
- array_map(fn($page) => $products->url($page), range(1, $products->lastPage()))
- ), // 每页的 URL
- ];
- // 构建导航数据 开始
- $breadcrumbs = [
- [
- 'url' => '/',
- 'name' => 'Home',
- ]
- ];
- $breadcrumbs[] = [
- 'url' => '#',
- 'name' => $category->name,
- ];
- // 构建缓存键
- $cacheKey = "product_category_{$category->id}_page_{$products->currentPage()}";
- // 渲染模板并传递数据
- return $this->liquidRenderer->render('products_categories.liquid', [
- 'category' => $category, // 分类名称
- 'products' => $products, // 分类下的产品
- 'paginator' => $paginator, // 分页信息
- 'breadcrumbs' => $breadcrumbs,
- ], $cacheKey);
- }
- /**
- * 产品详情
- * Display the specified product.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function detail($id)
- {
- $product = DistProduct::getProduct($id);
- if(!$product)
- {
- $product = DistProduct::getProductSlug($id);
- if(!$product)
- {
- abort('404');
- }
- }
- // 获取改产品分类下的相关产品,不包当前产品,限制条数 limit 4
- $relatedProducts = DistProduct::where('category_id', $product->category_id)
- ->where('dist_id', getDistId())
- ->where('enabled', 1)
- ->where('status', 2)
- ->where('id', '<>', $product->id)
- ->with('images') // Eager load images
- ->orderBy('order', 'desc') // 按 order 字段降序排序
- ->orderBy('id', 'desc') // 按 id 降序排序
- ->limit(4)
- ->get();
- // 构建导航数据 开始
- $category=$product->distProductCategory;
- $categoryUrl = $category->slug ? "/products/categories/{$category->slug}" : "/products/categories/{$category->id}";
- $breadcrumbs = [
- [
- 'url' => '/',
- 'name' => 'Home',
- ]
- ];
- $breadcrumbs[] = [
- 'url' => $categoryUrl,
- 'name' => $category->name,
- ];
- $breadcrumbs[] = [
- 'url' => '#',
- 'name' => $product->title,
- ];
- //能过 seo_keywords 查找相有相同 seo_keywords的产品, seo_keywords 可能是多个,用逗号分隔
- $seo_keywords = $product->seo_keywords;
- if($seo_keywords) {
- $seo_keywords_array = explode(',', $seo_keywords);
- }
- else {
- $seo_keywords_array = [];
- }
- $relatedPages= [];
- if(!empty($seo_keywords_array))
- {
- $seo_keywords_array = array_map('trim', $seo_keywords_array);
- $seo_keywords_array = array_unique($seo_keywords_array);
- // 构建查询条件
- $query = SitePage::where('dist_id', getDistId())
- ->where('status', 1);
- // 为每个关键词添加查询条件
- $query->where(function($q) use ($seo_keywords_array) {
- foreach ($seo_keywords_array as $keyword) {
- $q->orWhere(function($subQ) use ($keyword) {
- $subQ->where('seo_keywords', 'LIKE', $keyword)
- ->orWhere('seo_keywords', 'LIKE', $keyword . ',%')
- ->orWhere('seo_keywords', 'LIKE', '%,' . $keyword)
- ->orWhere('seo_keywords', 'LIKE', '%,' . $keyword . ',%');
- });
- }
- });
- // 获取相关产品
- $relatedPages = $query->orderBy('id', 'desc')
- ->limit(10)
- ->get();
- }
- // 渲染模板并传递数据
- return $this->liquidRenderer->render('products_detail.liquid',
- [
- 'product' => $product,
- 'relatedProducts' => $relatedProducts,
- 'breadcrumbs' => $breadcrumbs,
- 'relatedPages' => $relatedPages,
- ]);
- }
- }
|