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

137 lines
3.9 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. use App\Models\WaterfallAd;
  8. use Illuminate\Support\Facades\Storage;
  9. /**
  10. * 代理商产品
  11. * Class AgentProductController
  12. * @package App\Http\Controllers\Api
  13. */
  14. class AgentProductController extends Controller
  15. {
  16. // 代理商产品列表
  17. public function index()
  18. {
  19. $category_id = request()->input('category_id');
  20. if ($category_id) {
  21. $where['category_id'] = $category_id;
  22. }
  23. $where['agent_id'] = $this->agent_id;
  24. $list = AgentProduct::list()->where($where)->simplePaginate();
  25. $list = $this->paginatePicAddHost($list);
  26. $list = $this->insertAd($list);
  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. ->whereHas('product', function ($query) {
  39. return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0);
  40. })
  41. ->where('stock', '>', 0)
  42. ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
  43. if (!$agent_product) {
  44. return $this->error('产品不存在或已下架');
  45. }
  46. $prefix = Storage::disk('public')->url('');
  47. $agent_product->product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->product->pictures);
  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. return $this->index();
  63. }
  64. //【我的】页面下方推荐
  65. public function recommendList()
  66. {
  67. //TODO 推荐数据暂时使用产品列表,后期需要通过后台设置获取或根据用户购买过的关键词获取
  68. return $this->index();
  69. }
  70. //人气爆款列表
  71. public function hotList()
  72. {
  73. //TODO 具体排序规则,后期再做修改
  74. return $this->index();
  75. }
  76. //分页列表产品图片加域名
  77. private function paginatePicAddHost($list)
  78. {
  79. if (!$list->isEmpty()) {
  80. $prefix = Storage::disk('public')->url('');
  81. foreach ($list->items() as $k=>&$v) {
  82. $v->product->pictures = array_map(function($item) use ($prefix) {
  83. return strpos($item, $prefix) === false ? $prefix . $item : $item;
  84. }, $v->product->pictures);
  85. }
  86. }
  87. return $list;
  88. }
  89. //插入瀑布流广告
  90. private function insertAd($list)
  91. {
  92. //插入瀑布流广告,分别在第8个和第16个插入,同时需要考虑到分页。当所有瀑布流广告插入完之后,再次循环插入
  93. $list = $list->toArray();
  94. if ($list['data']) {
  95. $ad_total = WaterfallAd::where(['agent_id' => $this->agent_id, 'status' => 1])->count();
  96. $page = (int)request()->input('page');
  97. $start = ($page ? $page - 1 : 0) * 2 % $ad_total;
  98. $ad = WaterfallAd::where(['agent_id' => $this->agent_id, 'status' => 1])
  99. ->orderBy('sort')->orderBy('id', 'DESC')
  100. ->offset($start)->limit(2)->get(['title', 'picture', 'type', 'url'])->toArray();
  101. $prefix = Storage::disk('public')->url('');
  102. //每隔8个插入广告
  103. $gap = 4;
  104. foreach ($ad as $k => &$v) {
  105. $v['is_ad'] = true;
  106. $v['picture'] = $prefix . $v['picture'];
  107. //插入广告
  108. $temp = $gap * ($k+1);
  109. if (!empty($list['data'][$temp - 1]) && !empty($ad[$k])) {
  110. array_splice($list['data'], $temp + $k, 0, [$ad[$k]]);
  111. }
  112. }
  113. }
  114. return $list;
  115. }
  116. }