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

153 lines
4.5 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\ProductStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Advertising;
  6. use App\Models\AgentProduct;
  7. use App\Models\UserFav;
  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. $where = [];
  21. if ($category_id) {
  22. $where['category_id'] = $category_id;
  23. }
  24. $list = AgentProduct::list($this->agent_id)->where($where)->orderBy('id', 'DESC')->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('coupon:tag,agent_product_id')
  36. ->with('fav:agent_product_id')
  37. ->whereDoesntHave('agentProductItem', function ($query) {
  38. return $query->whereHas('product', function ($query) {
  39. return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
  40. });
  41. })
  42. ->where('stock', '>', 0)
  43. ->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
  44. if (!$agent_product) {
  45. return $this->error('产品已下架或库存不足');
  46. }
  47. $prefix = Storage::disk('public')->url('');
  48. $agent_product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->pictures);
  49. $agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏
  50. unset($agent_product->fav);
  51. //计算折扣
  52. if ($agent_product->price < $agent_product->original_price) {
  53. $agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
  54. } else {
  55. $agent_product->cost = '';
  56. }
  57. unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
  58. return $this->success($agent_product);
  59. }
  60. // 猜你喜欢
  61. public function guessLike()
  62. {
  63. $list = AgentProduct::list($this->agent_id)->orderBy('id', 'DESC')->simplePaginate();
  64. $list = $this->paginatePicAddHost($list);
  65. $list = $list->toArray();
  66. if (!empty($list['data']) && is_array($list['data'])) {
  67. shuffle($list['data']); //随机乱序
  68. }
  69. $list = $this->insertAd($list);
  70. return $this->success($list);
  71. }
  72. //【我的】页面下方推荐
  73. public function recommendList()
  74. {
  75. $list = AgentProduct::list($this->agent_id)->where(['is_rec' => 1])
  76. ->orderBy('id', 'DESC')->simplePaginate();
  77. $list = $this->paginatePicAddHost($list);
  78. $list = $this->insertAd($list);
  79. return $this->success($list);
  80. }
  81. //人气爆款列表,销量排序
  82. public function hotList()
  83. {
  84. $list = AgentProduct::list($this->agent_id)
  85. ->orderBy('sale', 'DESC')->orderBy('id', 'DESC')->simplePaginate();
  86. $list = $this->paginatePicAddHost($list);
  87. $list = $this->insertAd($list);
  88. return $this->success($list);
  89. }
  90. //分页列表产品图片加域名
  91. private function paginatePicAddHost($list)
  92. {
  93. if (!$list->isEmpty()) {
  94. $prefix = Storage::disk('public')->url('');
  95. foreach ($list->items() as $k=>&$v) {
  96. $v->pictures = array_map(function($item) use ($prefix) {
  97. return strpos($item, $prefix) === false ? $prefix . $item : $item;
  98. }, $v->pictures);
  99. }
  100. }
  101. return $list;
  102. }
  103. //插入瀑布流广告
  104. private function insertAd($list)
  105. {
  106. //插入瀑布流广告,分别在第8个和第16个插入,同时需要考虑到分页。当所有瀑布流广告插入完之后,再次循环插入
  107. if (is_object($list) && method_exists($list, 'toArray')) {
  108. $list = $list->toArray();
  109. }
  110. $ad_total = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])->count();
  111. if ($list['data'] && $ad_total > 0) {
  112. $page = (int)request()->input('page');
  113. $start = ($page ? $page - 1 : 0) * 2 % $ad_total;
  114. $ad = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])
  115. ->orderBy('sort')->orderBy('id', 'DESC')
  116. ->offset($start)->limit(2)->get(['title', 'picture', 'type', 'url'])->toArray();
  117. $prefix = Storage::disk('public')->url('');
  118. foreach ($ad as $k => &$v) {
  119. $v['is_ad'] = true;
  120. $v['picture'] = $prefix . $v['picture'];
  121. //每隔8个插入广告
  122. $temp = 8 * ($k+1);
  123. if (!empty($list['data'][$temp - 1]) && !empty($ad[$k])) {
  124. array_splice($list['data'], $temp + $k, 0, [$ad[$k]]);
  125. }
  126. }
  127. }
  128. return $list;
  129. }
  130. }