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

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