ProductController.php 5.4 KB

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