海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.3 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AgentProduct;
  5. use App\Models\UserFav;
  6. /**
  7. * 用户收藏
  8. * Class UserFavController
  9. * @package App\Http\Controllers\Api
  10. */
  11. class UserFavController extends Controller
  12. {
  13. public function index()
  14. {
  15. $list = UserFav::query()
  16. ->with([
  17. 'agentProduct:id,price,original_price',
  18. 'product:id,title,pictures',
  19. ])
  20. ->where('user_id', $this->user_id)
  21. ->select('id', 'agent_product_id', 'product_id', 'created_at')
  22. ->orderBy('id', 'DESC')
  23. ->simplePaginate(15);
  24. return $this->success($list);
  25. }
  26. public function create()
  27. {
  28. $id = (int)request()->input('agent_product_id');
  29. $agent_product = AgentProduct::find($id);
  30. if (!$agent_product) {
  31. return $this->error('产品不存在或已被删除');
  32. }
  33. //如果不存在则创建
  34. UserFav::query()->firstOrCreate([
  35. 'user_id' => $this->user_id,
  36. 'agent_product_id' => $agent_product->id,
  37. 'product_id' => $agent_product->product_id,
  38. ]);
  39. return $this->success();
  40. }
  41. public function delete()
  42. {
  43. $id = (int)request()->input('agent_product_id');
  44. $fav = UserFav::query()->where(['user_id' => $this->user_id, 'agent_product_id' => $id])->first();
  45. if (!$fav) {
  46. return $this->error('收藏不存在');
  47. }
  48. $fav->delete();
  49. return $this->success();
  50. }
  51. }