ProductController.php 5.6 KB

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