海南旅游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.

61 lines
1.7 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AgentProduct;
  5. /**
  6. * 代理商产品
  7. * Class AgentProductController
  8. * @package App\Http\Controllers\Api
  9. */
  10. class AgentProductController extends Controller
  11. {
  12. // 代理商产品列表
  13. public function index()
  14. {
  15. $category_id = request()->input('category_id');
  16. if ($category_id) {
  17. $where['category_id'] = $category_id;
  18. }
  19. $where['agent_id'] = $this->agent_id;
  20. $list = AgentProduct::where($where)
  21. ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')])
  22. ->select('id', 'sale', 'product_id', 'price', 'original_price')
  23. ->orderBy('id', 'DESC')
  24. ->simplePaginate();
  25. return $this->success($list);
  26. }
  27. // 产品详情
  28. public function show()
  29. {
  30. $id = (int)request()->input('id');
  31. // TODO 优惠券查询待优化
  32. $data = AgentProduct::query()
  33. ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures', 'know', 'content')])
  34. ->with(['coupon' => fn($query) => $query->select('id', 'tag', 'agent_product_id')])
  35. ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => 1]);
  36. if (!$data) {
  37. return $this->error('产品不存在或已下架');
  38. }
  39. unset($data->agent_id, $data->status, $data->created_at, $data->deleted_at);
  40. return $this->success($data);
  41. }
  42. // 猜你喜欢
  43. public function guessLike()
  44. {
  45. // TODO 此处需要再优化排序规则,并增加广告功能
  46. $list = AgentProduct::where('agent_id', $this->agent_id)
  47. ->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')])
  48. ->select('id', 'sale', 'product_id', 'price', 'original_price')
  49. ->orderBy('id', 'DESC')
  50. ->simplePaginate();
  51. return $this->success($list);
  52. }
  53. }