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

97 lines
2.7 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\ProductStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\AgentProduct;
  6. use App\Models\UserFav;
  7. /**
  8. * 代理商产品
  9. * Class AgentProductController
  10. * @package App\Http\Controllers\Api
  11. */
  12. class AgentProductController extends Controller
  13. {
  14. // 代理商产品列表
  15. public function index()
  16. {
  17. $category_id = request()->input('category_id');
  18. if ($category_id) {
  19. $where['category_id'] = $category_id;
  20. }
  21. $where['agent_id'] = $this->agent_id;
  22. $list = AgentProduct::where($where)
  23. ->with('product:id,title,pictures')
  24. ->whereHas('product', function ($query) {
  25. return $query->where('status', ProductStatus::ON_SALE);
  26. })
  27. ->select('id', 'sale', 'product_id', 'price', 'original_price')
  28. ->orderBy('id', 'DESC')
  29. ->simplePaginate();
  30. return $this->success($list);
  31. }
  32. // 产品详情
  33. public function show()
  34. {
  35. $id = (int)request()->input('id');
  36. // TODO 优惠券查询待优化
  37. $agent_product = AgentProduct::query()
  38. ->with('product:id,title,pictures,know,stock,content')
  39. ->with('coupon:tag,agent_product_id')
  40. ->with('fav:agent_product_id')
  41. ->whereHas('product', function ($query) {
  42. return $query->where('status', ProductStatus::ON_SALE);
  43. })
  44. ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
  45. if (!$agent_product || !$agent_product->product) {
  46. return $this->error('产品不存在或已下架');
  47. }
  48. $agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏
  49. //计算折扣
  50. if ($agent_product->price < $agent_product->original_price) {
  51. $agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
  52. } else {
  53. $agent_product->cost = '';
  54. }
  55. unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
  56. return $this->success($agent_product);
  57. }
  58. // 猜你喜欢
  59. public function guessLike()
  60. {
  61. // TODO 此处需要再优化排序规则,并增加广告功能
  62. $list = AgentProduct::where('agent_id', $this->agent_id)
  63. ->with('product:id,title,pictures')
  64. ->whereHas('product', function ($query) {
  65. return $query->where('status', ProductStatus::ON_SALE);
  66. })
  67. ->select('id', 'sale', 'product_id', 'price', 'original_price')
  68. ->orderBy('id', 'DESC')
  69. ->simplePaginate();
  70. return $this->success($list);
  71. }
  72. //【我的】页面下方推荐
  73. public function recommendList()
  74. {
  75. //TODO 推荐数据暂时使用产品列表,后期需要通过后台设置获取或根据用户购买过的关键词获取
  76. return $this->index();
  77. }
  78. //人气爆款列表
  79. public function hotList()
  80. {
  81. //TODO 具体排序规则,后期再做修改
  82. return $this->index();
  83. }
  84. }