ProductController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $category = DistProductCategory::findOrFail($slug);
  30. // 获取分类下的所有产品,并按 is_pinned 排序,然后进行分页
  31. $products = DistProduct::where('category_id', $category->id)
  32. ->where('dist_id', getDistId())
  33. ->where('enabled', 1)
  34. ->with('images') // Eager load images
  35. ->orderBy('is_pinned', 'desc') // 按 is_pinned 降序排序
  36. ->paginate(12);
  37. // 创建分页数据结构
  38. $paginator = [
  39. 'previous_page' => $products->previousPageUrl() ? true : false, // 是否有上一页
  40. 'previous_page_url' => $products->previousPageUrl(), // 上一页的 URL
  41. 'next_page' => $products->nextPageUrl() ? true : false, // 是否有下一页
  42. 'next_page_url' => $products->nextPageUrl(), // 下一页的 URL
  43. 'current_page' => $products->currentPage(), // 当前页
  44. 'total_pages' => $products->lastPage(), // 总页数
  45. 'pages' => range(1, $products->lastPage()), // 页码数组
  46. 'page_url' => array_combine(
  47. range(1, $products->lastPage()),
  48. array_map(fn($page) => $products->url($page), range(1, $products->lastPage()))
  49. ), // 每页的 URL
  50. ];
  51. // 构建导航数据 开始
  52. $breadcrumbs = [
  53. [
  54. 'url' => '/',
  55. 'name' => 'Home',
  56. ]
  57. ];
  58. $breadcrumbs[] = [
  59. 'url' => '#',
  60. 'name' => $category->name,
  61. ];
  62. // 渲染模板并传递数据
  63. return $this->liquidRenderer->render('products_categories.liquid', [
  64. 'category' => $category, // 分类名称
  65. 'products' => $products, // 分类下的产品
  66. 'paginator' => $paginator, // 分页信息
  67. 'breadcrumbs' => $breadcrumbs,
  68. ]);
  69. }
  70. /**
  71. * Display the specified product.
  72. *
  73. * @param int $id
  74. * @return \Illuminate\Http\Response
  75. */
  76. public function detail($id)
  77. {
  78. $product = DistProduct::getProduct($id);
  79. // 构建导航数据 开始
  80. $breadcrumbs = [
  81. [
  82. 'url' => '/',
  83. 'name' => 'Home',
  84. ]
  85. ];
  86. $category=$product->distProductCategory;
  87. $breadcrumbs[] = [
  88. 'url' => "/products/{$category->name}",
  89. 'name' => $category->name,
  90. ];
  91. $breadcrumbs[] = [
  92. 'url' => '#',
  93. 'name' => $product->title,
  94. ];
  95. return $this->liquidRenderer->render('products_detail.liquid',
  96. [
  97. 'product' => $product,
  98. 'csrf_token' => csrf_token(),
  99. 'breadcrumbs' => $breadcrumbs,
  100. ]);
  101. }
  102. }