ProductController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Services\LiquidRenderer;
  5. use App\Models\DistProduct;
  6. use App\Models\DistProductCategory; // 引入分类模型
  7. class ProductController extends Controller
  8. {
  9. protected $liquidRenderer;
  10. public function __construct(LiquidRenderer $liquidRenderer)
  11. {
  12. $this->liquidRenderer = $liquidRenderer;
  13. }
  14. /**
  15. * Display a listing of the products.
  16. *
  17. * @return \Illuminate\Http\Response
  18. */
  19. // public function index()
  20. // {
  21. // $products = Product::paginate(10); // 每页显示10个产品
  22. // return $this->liquidRenderer->render('products.index', ['products' => $products]);
  23. // }
  24. public function category($slug)
  25. {
  26. // $products = DistProduct::paginate(10); // 每页显示10个产品
  27. // return $this->liquidRenderer->render('products_categories.liquid', ['products' => $products]);
  28. // 获取分类信息
  29. // 获取分类信息,并限定 dist_id
  30. $category = DistProductCategory::where(function ($query) use ($slug) {
  31. $query->where('slug', $slug)
  32. ->orWhere('id', $slug);
  33. })
  34. ->where('dist_id', getDistId())
  35. ->firstOrFail();
  36. // 获取分类下的所有产品,排序,然后进行分页
  37. $products = DistProduct::where('category_id', $category->id)
  38. ->where('dist_id', getDistId())
  39. ->where('enabled', 1)
  40. ->with('images') // Eager load images
  41. ->orderBy('order', 'desc') // 按 order 字段降序排序
  42. ->orderBy('id', 'desc') // 按 id 降序排序
  43. ->paginate(12);
  44. // 创建分页数据结构
  45. $paginator = [
  46. 'previous_page' => $products->previousPageUrl() ? true : false, // 是否有上一页
  47. 'previous_page_url' => $products->previousPageUrl(), // 上一页的 URL
  48. 'next_page' => $products->nextPageUrl() ? true : false, // 是否有下一页
  49. 'next_page_url' => $products->nextPageUrl(), // 下一页的 URL
  50. 'current_page' => $products->currentPage(), // 当前页
  51. 'total_pages' => $products->lastPage(), // 总页数
  52. 'pages' => range(1, $products->lastPage()), // 页码数组
  53. 'page_url' => array_combine(
  54. range(1, $products->lastPage()),
  55. array_map(fn($page) => $products->url($page), range(1, $products->lastPage()))
  56. ), // 每页的 URL
  57. ];
  58. // 构建导航数据 开始
  59. $breadcrumbs = [
  60. [
  61. 'url' => '/',
  62. 'name' => 'Home',
  63. ]
  64. ];
  65. $breadcrumbs[] = [
  66. 'url' => '#',
  67. 'name' => $category->name,
  68. ];
  69. // 构建缓存键
  70. $cacheKey = "product_category_{$category->id}_page_{$products->currentPage()}";
  71. // 渲染模板并传递数据
  72. return $this->liquidRenderer->render('products_categories.liquid', [
  73. 'category' => $category, // 分类名称
  74. 'products' => $products, // 分类下的产品
  75. 'paginator' => $paginator, // 分页信息
  76. 'breadcrumbs' => $breadcrumbs,
  77. ], $cacheKey);
  78. }
  79. /**
  80. * 产品详情
  81. * Display the specified product.
  82. *
  83. * @param int $id
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function detail($id)
  87. {
  88. $product = DistProduct::getProduct($id);
  89. if(!$product)
  90. {
  91. abort('404');
  92. }
  93. // 获取改产品分类下的相关产品,不包当前产品,限制条数 limit 4
  94. $relatedProducts = DistProduct::where('category_id', $product->category_id)
  95. ->where('dist_id', getDistId())
  96. ->where('enabled', 1)
  97. ->where('id', '<>', $product->id)
  98. ->with('images') // Eager load images
  99. ->orderBy('order', 'desc') // 按 order 字段降序排序
  100. ->orderBy('id', 'desc') // 按 id 降序排序
  101. ->limit(4)
  102. ->get();
  103. // 构建导航数据 开始
  104. $category=$product->distProductCategory;
  105. $categoryUrl = $category->slug ? "/products/categories/{$category->slug}" : "/products/categories/{$category->id}";
  106. $breadcrumbs = [
  107. [
  108. 'url' => '/',
  109. 'name' => 'Home',
  110. ]
  111. ];
  112. $breadcrumbs[] = [
  113. 'url' => $categoryUrl,
  114. 'name' => $category->name,
  115. ];
  116. $breadcrumbs[] = [
  117. 'url' => '#',
  118. 'name' => $product->title,
  119. ];
  120. // 渲染模板并传递数据
  121. return $this->liquidRenderer->render('products_detail.liquid',
  122. [
  123. 'product' => $product,
  124. 'relatedProducts' => $relatedProducts,
  125. 'breadcrumbs' => $breadcrumbs,
  126. ]);
  127. }
  128. }