ProductController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * DateTime: 2022/10/2 13:32
  5. *
  6. * @description
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Models\Product;
  10. use App\Models\ProductCategory;
  11. use App\Models\StatProduct;
  12. use App\Models\StatProductDownload;
  13. use Dingo\Api\Http\Request;
  14. use Illuminate\Database\Eloquent\Builder;
  15. use Illuminate\Http\JsonResponse;
  16. class ProductController extends Controller
  17. {
  18. public function cate(Request $request): JsonResponse
  19. {
  20. $limit = $request->input('limit', 3);
  21. $keywords = $request->input('keywords', '');
  22. $page = request()->input('page',1);
  23. $offset = ($page - 1) * $limit;
  24. $lists = ProductCategory::where('is_opened', 1)
  25. ->when($keywords, function ($query, $keywords) {
  26. /* @var Builder $query*/
  27. return $query->where('name','like', "%$keywords%");
  28. })
  29. ->orderByDesc('sort')
  30. ->limit($limit)
  31. ->offset($offset)
  32. ->get();
  33. return $this->success($lists);
  34. }
  35. public function search(Request $request): JsonResponse
  36. {
  37. $limit = $request->input('limit');
  38. $keywords = $request->input('keywords', '');
  39. $page = request()->input('page',1);
  40. $cateId = request()->input('cate_id',0);
  41. $ids = request()->input('ids','');
  42. $offset = ($page - 1) * $limit;
  43. $ids = explode(",",$ids);
  44. $lists = Product::withCount(['viewer'])->where('is_opened', 1)
  45. ->when($keywords, function ($query, $keywords) {
  46. /* @var Builder $query*/
  47. return $query->where('name','like', "%$keywords%");
  48. })
  49. ->when($cateId, function ($query, $cateId) {
  50. /* @var Builder $query*/
  51. return $query->where('cate_id', $cateId);
  52. })->when($ids, function ($query, $ids) {
  53. /* @var Builder $query*/
  54. return $query->whereIn('id',$ids);
  55. })
  56. ->orderByDesc('viewer_count')
  57. ->orderByDesc('sort')
  58. ->limit($limit)
  59. ->offset($offset)
  60. ->get();
  61. return $this->success($lists);
  62. }
  63. public function detail($id): JsonResponse
  64. {
  65. $info = Product::with(['specs' => function($query) {
  66. $query->select(['id','product_id','name','specs'])
  67. ->where('is_opened', 1)
  68. ->orderByDesc('sort');
  69. }])
  70. ->where('id',$id)
  71. ->first();
  72. $info->files = $info;
  73. return $this->success($info);
  74. }
  75. public function viewer($id): JsonResponse
  76. {
  77. $stat = new StatProduct();
  78. $stat->product_id = $id;
  79. $stat->user_id = \user()->id;
  80. $stat->save();
  81. return $this->success();
  82. }
  83. public function download($id): JsonResponse
  84. {
  85. $stat = new StatProductDownload();
  86. $stat->product_id = $id;
  87. $stat->user_id = \user()->id;
  88. $stat->save();
  89. return $this->success();
  90. }
  91. }