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

251 lines
7.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
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. if (AgentProduct::withTrashed()->where('agent_id', $this->agent_id)->count() === 0) { //演示产品用
  51. $where = ['id' => $id, 'status' => ProductStatus::ON_SALE];
  52. } else {
  53. $where = ['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE];
  54. }
  55. $agent_product = AgentProduct::with([
  56. 'coupon:tag,agent_product_id',
  57. 'product' => function ($query) {
  58. $query->select(['id', 'type', 'extends', 'diy_form_id'])->with('diyForm', function ($query) {
  59. $query->select(['id', 'name'])->with('fields');
  60. });
  61. },
  62. 'spec' => function($query) {
  63. return $query->has('productSpec')->with('productSpec', function ($query) {
  64. $query->select(['id', 'name', 'date'])->where('date', '>=', date('Y-m-d'))->orderBy('date', 'asc');
  65. });
  66. }
  67. ])
  68. ->whereDoesntHave('agentProductItem', function ($query) {
  69. return $query->whereHas('product', function ($query) {
  70. return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
  71. });
  72. })
  73. ->where('stock', '>', 0)
  74. ->firstWhere($where);
  75. if (!$agent_product) {
  76. return $this->error('产品已下架或库存不足');
  77. }
  78. $prefix = Storage::disk('public')->url('');
  79. $agent_product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->pictures);
  80. if ($this->user_id) {
  81. $agent_product->is_collect = UserFav::query()->where(['user_id' => $this->user_id, 'agent_product_id' => $id])->exists() ? 1 : 0; //判断是否收藏
  82. } else {
  83. $agent_product->is_collect = 0;
  84. }
  85. //计算折扣
  86. if ($agent_product->price < $agent_product->original_price) {
  87. $agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
  88. } else {
  89. $agent_product->cost = '';
  90. }
  91. //处理自定义字段
  92. if (!empty($agent_product->product->diyForm->fields) && !$agent_product->product->diyForm->fields->isEmpty()) {
  93. foreach ($agent_product->product->diyForm->fields as &$v) {
  94. if ($v['type'] == 'checkbox' && is_array($v['options'])) {
  95. $v['options'] = array_map(function ($v2) {
  96. $arr['checked'] = false;
  97. $arr['disabled'] = false;
  98. $arr['name'] = $v2;
  99. return $arr;
  100. }, $v['options']);
  101. }
  102. }
  103. }
  104. //处理规格
  105. if (!$agent_product->spec->isEmpty()) {
  106. $specs = $agent_product->spec->toArray();
  107. //二维数组转一维
  108. $specs = array_map(function ($v) {
  109. if (is_array($v['product_spec'])) {
  110. unset($v['product_spec']['id']);
  111. $v = array_merge($v, $v['product_spec']);
  112. }
  113. unset($v['agent_product_id'], $v['product_spec_id'], $v['product_spec'], $v['deleted_at']);
  114. return $v;
  115. }, $specs);
  116. //去年name和date为空的数组
  117. $specs = array_filter($specs, fn($v) => isset($v['name'], $v['date']));
  118. $names = array_column($specs, 'name');
  119. $names = array_values(array_unique($names));
  120. $result = [];
  121. foreach ($names as $name) {
  122. $list = array_filter($specs, fn($v) => $v['name'] == $name);
  123. $result[] = [
  124. 'name' => $name,
  125. 'date_start' => min(array_column($list, 'date')),
  126. 'date_end' => max(array_column($list, 'date')),
  127. 'original_price' => min(array_column($list, 'original_price')),
  128. 'price' => min(array_column($list, 'price')),
  129. 'list' => array_values($list),
  130. ];
  131. }
  132. unset($agent_product->spec);
  133. $agent_product->spec = $result;
  134. }
  135. unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
  136. return $this->success($agent_product);
  137. }
  138. // 猜你喜欢
  139. public function guessLike()
  140. {
  141. $list = AgentProduct::list($this->agent_id)->orderBy('id', 'DESC')->simplePaginate();
  142. $list = $this->paginatePicAddHost($list);
  143. $list = $list->toArray();
  144. if (!empty($list['data']) && is_array($list['data'])) {
  145. shuffle($list['data']); //随机乱序
  146. }
  147. $list = $this->insertAd($list);
  148. return $this->success($list);
  149. }
  150. //【我的】页面下方推荐
  151. public function recommendList()
  152. {
  153. $list = AgentProduct::list($this->agent_id)->where(['is_rec' => 1])
  154. ->orderBy('id', 'DESC')->simplePaginate();
  155. $list = $this->paginatePicAddHost($list);
  156. $list = $this->insertAd($list);
  157. return $this->success($list);
  158. }
  159. //人气爆款列表,销量排序
  160. public function hotList()
  161. {
  162. $list = AgentProduct::list($this->agent_id)
  163. ->orderBy('sale', 'DESC')->orderBy('id', 'DESC')->simplePaginate();
  164. $list = $this->paginatePicAddHost($list);
  165. $list = $this->insertAd($list);
  166. return $this->success($list);
  167. }
  168. //分页列表产品图片加域名
  169. private function paginatePicAddHost($list)
  170. {
  171. //如果是新入驻代理商没有数据,且没有删除过的数据,则显示最早的几条
  172. if ($list->isEmpty()) {
  173. if (AgentProduct::list($this->agent_id)->withTrashed()->count() === 0 && request('page', 1) <= 2) { //只获取2页数据
  174. $list = AgentProduct::list($this->agent_id)->orWhere([['status', '=', ProductStatus::ON_SALE], ['price', '>', 500]])->orderBy('id')->simplePaginate();
  175. } else {
  176. return $list; //因为只获取2页数据,不return可能会导入下面的代码出错
  177. }
  178. }
  179. $prefix = Storage::disk('public')->url('');
  180. foreach ($list->items() as $k=>&$v) {
  181. $v->pictures = array_map(function($item) use ($prefix) {
  182. return strpos($item, $prefix) === false ? $prefix . $item : $item;
  183. }, $v->pictures);
  184. }
  185. return $list;
  186. }
  187. //插入瀑布流广告
  188. private function insertAd($list)
  189. {
  190. //插入瀑布流广告,分别在第8个和第16个插入,同时需要考虑到分页。当所有瀑布流广告插入完之后,再次循环插入
  191. if (is_object($list) && method_exists($list, 'toArray')) {
  192. $list = $list->toArray();
  193. }
  194. $ad_total = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])->count();
  195. if ($list['data'] && $ad_total > 0) {
  196. $page = (int)request()->input('page');
  197. $start = ($page ? $page - 1 : 0) * 2 % $ad_total;
  198. $ad = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])
  199. ->orderBy('sort')->orderBy('id', 'DESC')
  200. ->offset($start)->limit(2)->get(['title', 'picture', 'type', 'url'])->toArray();
  201. $prefix = Storage::disk('public')->url('');
  202. foreach ($ad as $k => &$v) {
  203. $v['is_ad'] = true;
  204. $v['picture'] = $prefix . $v['picture'];
  205. //每隔8个插入广告
  206. $temp = 8 * ($k+1);
  207. if (!empty($list['data'][$temp - 1]) && !empty($ad[$k])) {
  208. array_splice($list['data'], $temp + $k, 0, [$ad[$k]]);
  209. }
  210. }
  211. }
  212. return $list;
  213. }
  214. }