ProductController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\StatProduct;
  11. use App\Models\StatProductDownload;
  12. use Dingo\Api\Http\Request;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Http\JsonResponse;
  15. class ProductController extends Controller
  16. {
  17. public function search(Request $request): JsonResponse
  18. {
  19. $limit = $request->input('limit');
  20. $keywords = $request->input('keywords', '');
  21. $page = request()->input('page',1);
  22. $offset = ($page - 1) * $limit;
  23. $lists = Product::withCount(['viewer'])->where('is_opened', 1)
  24. ->when($keywords, function ($query, $keywords) {
  25. /* @var Builder $query*/
  26. return $query->where('name','like', "%$keywords%");
  27. })
  28. ->orderByDesc('viewer_count')
  29. ->orderByDesc('sort')
  30. ->limit($limit)
  31. ->offset($offset)
  32. ->get();
  33. return $this->success($lists);
  34. }
  35. public function detail($id): JsonResponse
  36. {
  37. $info = Product::with(['specs' => function($query) {
  38. $query->select(['id','product_id','name','specs'])
  39. ->where('is_opened', 1)
  40. ->orderByDesc('sort');
  41. }])
  42. ->where('id',$id)
  43. ->first();
  44. $info->files = $info;
  45. return $this->success($info);
  46. }
  47. public function viewer($id): JsonResponse
  48. {
  49. $stat = new StatProduct();
  50. $stat->product_id = $id;
  51. $stat->user_id = \user()->id;
  52. $stat->save();
  53. return $this->success();
  54. }
  55. public function download($id): JsonResponse
  56. {
  57. $stat = new StatProductDownload();
  58. $stat->product_id = $id;
  59. $stat->user_id = \user()->id;
  60. $stat->save();
  61. return $this->success();
  62. }
  63. }