ProductController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. ->where('status', 2)
  41. ->with('images') // Eager load images
  42. ->orderBy('order', 'desc') // 按 order 字段降序排序
  43. ->orderBy('id', 'desc') // 按 id 降序排序
  44. ->paginate(12);
  45. // 创建分页数据结构
  46. $paginator = [
  47. 'previous_page' => $products->previousPageUrl() ? true : false, // 是否有上一页
  48. 'previous_page_url' => $products->previousPageUrl(), // 上一页的 URL
  49. 'next_page' => $products->nextPageUrl() ? true : false, // 是否有下一页
  50. 'next_page_url' => $products->nextPageUrl(), // 下一页的 URL
  51. 'current_page' => $products->currentPage(), // 当前页
  52. 'total_pages' => $products->lastPage(), // 总页数
  53. 'pages' => range(1, $products->lastPage()), // 页码数组
  54. 'page_url' => array_combine(
  55. range(1, $products->lastPage()),
  56. array_map(fn($page) => $products->url($page), range(1, $products->lastPage()))
  57. ), // 每页的 URL
  58. ];
  59. // 构建导航数据 开始
  60. $breadcrumbs = [
  61. [
  62. 'url' => '/',
  63. 'name' => 'Home',
  64. ]
  65. ];
  66. $breadcrumbs[] = [
  67. 'url' => '#',
  68. 'name' => $category->name,
  69. ];
  70. // 构建缓存键
  71. $cacheKey = "product_category_{$category->id}_page_{$products->currentPage()}";
  72. // 渲染模板并传递数据
  73. return $this->liquidRenderer->render('products_categories.liquid', [
  74. 'category' => $category, // 分类名称
  75. 'products' => $products, // 分类下的产品
  76. 'paginator' => $paginator, // 分页信息
  77. 'breadcrumbs' => $breadcrumbs,
  78. ], $cacheKey);
  79. }
  80. /**
  81. * 产品详情
  82. * Display the specified product.
  83. *
  84. * @param int $id
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function detail($id)
  88. {
  89. $product = DistProduct::getProduct($id);
  90. if(!$product)
  91. {
  92. $product = DistProduct::getProductSlug($id);
  93. if(!$product)
  94. {
  95. abort('404');
  96. }
  97. }
  98. // 获取改产品分类下的相关产品,不包当前产品,限制条数 limit 4
  99. $relatedProducts = DistProduct::where('category_id', $product->category_id)
  100. ->where('dist_id', getDistId())
  101. ->where('enabled', 1)
  102. ->where('status', 2)
  103. ->where('id', '<>', $product->id)
  104. ->with('images') // Eager load images
  105. ->orderBy('order', 'desc') // 按 order 字段降序排序
  106. ->orderBy('id', 'desc') // 按 id 降序排序
  107. ->limit(4)
  108. ->get();
  109. // 构建导航数据 开始
  110. $category=$product->distProductCategory;
  111. $categoryUrl = $category->slug ? "/products/categories/{$category->slug}" : "/products/categories/{$category->id}";
  112. $breadcrumbs = [
  113. [
  114. 'url' => '/',
  115. 'name' => 'Home',
  116. ]
  117. ];
  118. $breadcrumbs[] = [
  119. 'url' => $categoryUrl,
  120. 'name' => $category->name,
  121. ];
  122. $breadcrumbs[] = [
  123. 'url' => '#',
  124. 'name' => $product->title,
  125. ];
  126. // 渲染模板并传递数据
  127. return $this->liquidRenderer->render('products_detail.liquid',
  128. [
  129. 'product' => $product,
  130. 'relatedProducts' => $relatedProducts,
  131. 'breadcrumbs' => $breadcrumbs,
  132. ]);
  133. }
  134. }