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

237 lines
7.2 KiB

4 years ago
4 years ago
  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\Product;
  8. use App\Models\UserFav;
  9. use Illuminate\Support\Facades\Storage;
  10. /**
  11. * 代理商产品
  12. * Class AgentProductController
  13. * @package App\Http\Controllers\Api
  14. */
  15. class AgentProductController extends Controller
  16. {
  17. // 代理商产品列表
  18. public function index()
  19. {
  20. $category_id = request()->input('category_id');
  21. $where = [];
  22. if ($category_id) {
  23. $where['category_id'] = $category_id;
  24. }
  25. $list = AgentProduct::list($this->agent_id)->where($where)->orderBy('id', 'DESC')->simplePaginate();
  26. $list = $this->paginatePicAddHost($list);
  27. $list = $this->insertAd($list);
  28. return $this->success($list);
  29. }
  30. //首页搜索框
  31. public function search()
  32. {
  33. $keywords = request()->input('keywords');
  34. $type = request()->input('type', 0);
  35. $by = request()->input('by', 0);
  36. if (!$keywords) {
  37. return $this->error('请输入关键词');
  38. }
  39. $fields = ['id', 'sale', 'updated_at', 'price'];
  40. $field = $fields[$type] ?? $fields[0];
  41. $by = $by == 0 ? 'desc' : 'asc';
  42. $list = AgentProduct::list($this->agent_id)->where('title', 'like', "%$keywords%")->orderBy($field, $by)->simplePaginate();
  43. $list = $this->paginatePicAddHost($list);
  44. return $this->success($list);
  45. }
  46. // 产品详情
  47. public function show()
  48. {
  49. $id = (int)request()->input('id');
  50. // TODO 优惠券查询待优化
  51. if (AgentProduct::withTrashed()->where('agent_id', $this->agent_id)->count() === 0) { //演示产品用
  52. $where = ['id' => $id, 'status' => ProductStatus::ON_SALE];
  53. } else {
  54. $where = ['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE];
  55. }
  56. $agent_product = AgentProduct::with([
  57. 'coupon:tag,agent_product_id',
  58. 'product:id,type,extends',
  59. 'spec' => function($query) {
  60. return $query->has('productSpec')->with('productSpec:id,name,date');
  61. }
  62. ])
  63. ->whereDoesntHave('agentProductItem', function ($query) {
  64. return $query->whereHas('product', function ($query) {
  65. return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
  66. });
  67. })
  68. ->where('stock', '>', 0)
  69. ->firstWhere($where);
  70. if (!$agent_product) {
  71. return $this->error('产品已下架或库存不足');
  72. }
  73. $prefix = Storage::disk('public')->url('');
  74. $agent_product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->pictures);
  75. if ($this->user_id) {
  76. $agent_product->is_collect = UserFav::query()->where(['user_id' => $this->user_id, 'agent_product_id' => $id])->exists() ? 1 : 0; //判断是否收藏
  77. } else {
  78. $agent_product->is_collect = 0;
  79. }
  80. //计算折扣
  81. if ($agent_product->price < $agent_product->original_price) {
  82. $agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
  83. } else {
  84. $agent_product->cost = '';
  85. }
  86. //如果是单品销售,显示附加信息字段,组合产品和计调产品不显示
  87. if ($agent_product->type == 0) {
  88. $agent_product->product = Product::query()->where('id', $agent_product->product_id)->first(['type', 'extends']);
  89. } else {
  90. $agent_product->product = null;
  91. }
  92. //处理规格
  93. if (!$agent_product->spec->isEmpty()) {
  94. $specs = $agent_product->spec->toArray();
  95. //二维数组转一维
  96. $specs = array_map(function ($v) {
  97. if (is_array($v['product_spec'])) {
  98. unset($v['product_spec']['id']);
  99. $v = array_merge($v, $v['product_spec']);
  100. }
  101. unset($v['agent_product_id'], $v['product_spec_id'], $v['product_spec']);
  102. return $v;
  103. }, $specs);
  104. //去年name和date为空的数组
  105. $specs = array_filter($specs, fn($v) => isset($v['name'], $v['date']));
  106. $names = array_column($specs, 'name');
  107. $names = array_values(array_unique($names));
  108. $result = [];
  109. foreach ($names as $name) {
  110. $list = array_filter($specs, fn($v) => $v['name'] == $name);
  111. $result[] = [
  112. 'name' => $name,
  113. 'original_price' => min(array_column($list, 'original_price')),
  114. 'price' => min(array_column($list, 'price')),
  115. 'list' => $list,
  116. ];
  117. }
  118. unset($agent_product->spec);
  119. $agent_product->spec = $result;
  120. }
  121. unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
  122. return $this->success($agent_product);
  123. }
  124. // 猜你喜欢
  125. public function guessLike()
  126. {
  127. $list = AgentProduct::list($this->agent_id)->orderBy('id', 'DESC')->simplePaginate();
  128. $list = $this->paginatePicAddHost($list);
  129. $list = $list->toArray();
  130. if (!empty($list['data']) && is_array($list['data'])) {
  131. shuffle($list['data']); //随机乱序
  132. }
  133. $list = $this->insertAd($list);
  134. return $this->success($list);
  135. }
  136. //【我的】页面下方推荐
  137. public function recommendList()
  138. {
  139. $list = AgentProduct::list($this->agent_id)->where(['is_rec' => 1])
  140. ->orderBy('id', 'DESC')->simplePaginate();
  141. $list = $this->paginatePicAddHost($list);
  142. $list = $this->insertAd($list);
  143. return $this->success($list);
  144. }
  145. //人气爆款列表,销量排序
  146. public function hotList()
  147. {
  148. $list = AgentProduct::list($this->agent_id)
  149. ->orderBy('sale', 'DESC')->orderBy('id', 'DESC')->simplePaginate();
  150. $list = $this->paginatePicAddHost($list);
  151. $list = $this->insertAd($list);
  152. return $this->success($list);
  153. }
  154. //分页列表产品图片加域名
  155. private function paginatePicAddHost($list)
  156. {
  157. //如果是新入驻代理商没有数据,且没有删除过的数据,则显示最早的几条
  158. if ($list->isEmpty()) {
  159. if (AgentProduct::list($this->agent_id)->withTrashed()->count() === 0 && request('page', 1) <= 2) { //只获取2页数据
  160. $list = AgentProduct::list($this->agent_id)->orWhere([['status', '=', ProductStatus::ON_SALE], ['price', '>', 500]])->orderBy('id')->simplePaginate();
  161. } else {
  162. return $list; //因为只获取2页数据,不return可能会导入下面的代码出错
  163. }
  164. }
  165. $prefix = Storage::disk('public')->url('');
  166. foreach ($list->items() as $k=>&$v) {
  167. $v->pictures = array_map(function($item) use ($prefix) {
  168. return strpos($item, $prefix) === false ? $prefix . $item : $item;
  169. }, $v->pictures);
  170. }
  171. return $list;
  172. }
  173. //插入瀑布流广告
  174. private function insertAd($list)
  175. {
  176. //插入瀑布流广告,分别在第8个和第16个插入,同时需要考虑到分页。当所有瀑布流广告插入完之后,再次循环插入
  177. if (is_object($list) && method_exists($list, 'toArray')) {
  178. $list = $list->toArray();
  179. }
  180. $ad_total = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])->count();
  181. if ($list['data'] && $ad_total > 0) {
  182. $page = (int)request()->input('page');
  183. $start = ($page ? $page - 1 : 0) * 2 % $ad_total;
  184. $ad = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])
  185. ->orderBy('sort')->orderBy('id', 'DESC')
  186. ->offset($start)->limit(2)->get(['title', 'picture', 'type', 'url'])->toArray();
  187. $prefix = Storage::disk('public')->url('');
  188. foreach ($ad as $k => &$v) {
  189. $v['is_ad'] = true;
  190. $v['picture'] = $prefix . $v['picture'];
  191. //每隔8个插入广告
  192. $temp = 8 * ($k+1);
  193. if (!empty($list['data'][$temp - 1]) && !empty($ad[$k])) {
  194. array_splice($list['data'], $temp + $k, 0, [$ad[$k]]);
  195. }
  196. }
  197. }
  198. return $list;
  199. }
  200. }