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

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