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

317 lines
9.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\Category;
  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. $formData = request()->only(['category_id', 'type', 'by']);
  21. $category_id = $formData['category_id'] ?? 0;
  22. $type = $formData['type'] ?? 0;
  23. $by = $formData['by'] ?? 0;
  24. $list = AgentProduct::list($this->agent_id);
  25. if ($category_id) {
  26. $list = $list->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
  27. }
  28. $fields = ['id', 'sale', 'updated_at', 'price']; //排序字段 TODO 还有距离排序
  29. $field = $fields[$type] ?? $fields[0];
  30. $by = $by == 0 ? 'desc' : 'asc';
  31. $list = $list->orderBy($field, $by)->simplePaginate();
  32. $list = $this->paginatePicAddHost($list);
  33. $list = $this->insertAd($list);
  34. return $this->success($list);
  35. }
  36. //递归获取指定分类下的所有子分类
  37. private function get_category_child_ids($category_id): array
  38. {
  39. static $category = null;
  40. if ($category === null) {
  41. $category = Category::where('agent_id', $this->agent_id)->get()->toArray();
  42. }
  43. $child = [];
  44. foreach ($category as $cat) {
  45. if ($cat['pid'] == $category_id) {
  46. $child[] = $cat['id'];
  47. $child = array_merge($child, $this->get_category_child_ids($cat['id']));
  48. }
  49. }
  50. return $child;
  51. }
  52. //首页搜索框
  53. public function search()
  54. {
  55. $category_id = request()->input('category_id');
  56. $keywords = request()->input('keywords');
  57. $type = request()->input('type', 0);
  58. $by = request()->input('by', 0);
  59. $list = AgentProduct::list($this->agent_id);
  60. if ($category_id) {
  61. $list = $list->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
  62. }
  63. if ($keywords) {
  64. $list = $list->where('title', 'like', "%$keywords%");
  65. }
  66. $fields = ['id', 'sale', 'updated_at', 'price']; //排序字段
  67. $field = $fields[$type] ?? $fields[0];
  68. $by = $by == 0 ? 'desc' : 'asc';
  69. $list = $list->orderBy($field, $by)->simplePaginate();
  70. $list = $this->paginatePicAddHost($list);
  71. return $this->success($list);
  72. }
  73. //旅游线路搜索
  74. public function travel_search()
  75. {
  76. $formData = request()->only(['departure_place', 'destination', 'type', 'by']);
  77. $type = $formData['type'] ?? 0;
  78. $by = $formData['by'] ?? 0;
  79. if (empty($formData['departure_place']) && empty($formData['destination'])) {
  80. return $this->error('请输入出发地和目的地');
  81. }
  82. $fields = ['id', 'sale', 'updated_at', 'price']; //排序字段
  83. $field = $fields[$type] ?? $fields[0];
  84. $by = $by == 0 ? 'desc' : 'asc';
  85. $list = AgentProduct::list($this->agent_id)->whereHas('product', function($query) use ($formData) {
  86. //出发地
  87. if (!empty($formData['departure_place'])) {
  88. $query->whereRaw("extends->'$.field_0_departure_place' LIKE ?", ["%{$formData['departure_place']}%"]);
  89. }
  90. //目的地
  91. if (!empty($formData['destination'])) {
  92. $query->whereRaw("extends->'$.field_0_destination' LIKE ?", ["%{$formData['destination']}%"]);
  93. }
  94. })->orderBy($field, $by)->simplePaginate();
  95. $list = $this->paginatePicAddHost($list);
  96. return $this->success($list);
  97. }
  98. // 产品详情
  99. public function show()
  100. {
  101. $id = (int)request()->input('id');
  102. if (AgentProduct::where('agent_id', $this->agent_id)->withTrashed()->count() === 0) { //演示产品用
  103. $where = ['id' => $id, 'status' => ProductStatus::ON_SALE];
  104. } else {
  105. $where = ['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE];
  106. }
  107. $agent_product = AgentProduct::with([
  108. 'coupon:tag,agent_product_id',
  109. 'product' => function ($query) {
  110. $query->select(['id', 'type', 'extends', 'diy_form_id'])->with('diyForm', function ($query) {
  111. $query->select(['id', 'name'])->with('fields');
  112. });
  113. },
  114. 'spec' => function($query) {
  115. return $query->has('productSpec')->with('productSpec', function ($query) {
  116. $query->select(['id', 'name', 'date'])->where('date', '>=', date('Y-m-d'))->orderBy('date', 'asc');
  117. });
  118. }
  119. ])
  120. ->whereDoesntHave('agentProductItem', function ($query) {
  121. return $query->whereHas('product', function ($query) {
  122. return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
  123. });
  124. })
  125. ->where('stock', '>', 0)
  126. ->firstWhere($where);
  127. if (!$agent_product) {
  128. return $this->error('产品已下架或库存不足');
  129. }
  130. $prefix = Storage::disk('public')->url('');
  131. $agent_product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->pictures);
  132. if ($this->user_id) {
  133. $agent_product->is_collect = UserFav::query()->where(['user_id' => $this->user_id, 'agent_product_id' => $id])->exists() ? 1 : 0; //判断是否收藏
  134. } else {
  135. $agent_product->is_collect = 0;
  136. }
  137. //计算折扣
  138. if ($agent_product->price < $agent_product->original_price) {
  139. $agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
  140. } else {
  141. $agent_product->cost = '';
  142. }
  143. //处理自定义字段
  144. if (!empty($agent_product->product->diyForm->fields) && !$agent_product->product->diyForm->fields->isEmpty()) {
  145. foreach ($agent_product->product->diyForm->fields as &$v) {
  146. if ($v['type'] == 'checkbox' && is_array($v['options'])) {
  147. $v['options'] = array_map(function ($v2) {
  148. $arr['checked'] = false;
  149. $arr['disabled'] = false;
  150. $arr['name'] = $v2;
  151. return $arr;
  152. }, $v['options']);
  153. }
  154. }
  155. }
  156. //处理规格
  157. if (!$agent_product->spec->isEmpty()) {
  158. $specs = $agent_product->spec->toArray();
  159. //二维数组转一维
  160. $specs = array_map(function ($v) {
  161. if (is_array($v['product_spec'])) {
  162. unset($v['product_spec']['id']);
  163. $v = array_merge($v, $v['product_spec']);
  164. }
  165. unset($v['agent_product_id'], $v['product_spec_id'], $v['product_spec'], $v['deleted_at']);
  166. return $v;
  167. }, $specs);
  168. //去年name和date为空的数组
  169. $specs = array_filter($specs, fn($v) => isset($v['name'], $v['date']));
  170. $names = array_column($specs, 'name');
  171. $names = array_values(array_unique($names));
  172. $result = [];
  173. foreach ($names as $name) {
  174. $list = array_filter($specs, fn($v) => $v['name'] == $name);
  175. $result[] = [
  176. 'name' => $name,
  177. 'date_start' => min(array_column($list, 'date')),
  178. 'date_end' => max(array_column($list, 'date')),
  179. 'original_price' => min(array_column($list, 'original_price')),
  180. 'price' => min(array_column($list, 'price')),
  181. 'list' => array_values($list),
  182. ];
  183. }
  184. unset($agent_product->spec);
  185. $agent_product->spec = $result;
  186. }
  187. unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
  188. return $this->success($agent_product);
  189. }
  190. // 猜你喜欢
  191. public function guessLike()
  192. {
  193. $list = AgentProduct::list($this->agent_id)->orderBy('id', 'DESC')->simplePaginate();
  194. $list = $this->paginatePicAddHost($list);
  195. $list = $list->toArray();
  196. if (!empty($list['data']) && is_array($list['data'])) {
  197. shuffle($list['data']); //随机乱序
  198. }
  199. $list = $this->insertAd($list);
  200. return $this->success($list);
  201. }
  202. //【我的】页面下方推荐
  203. public function recommendList()
  204. {
  205. $list = AgentProduct::list($this->agent_id)->where(['is_rec' => 1])
  206. ->orderBy('id', 'DESC')->simplePaginate();
  207. $list = $this->paginatePicAddHost($list);
  208. $list = $this->insertAd($list);
  209. return $this->success($list);
  210. }
  211. //人气爆款列表,销量排序
  212. public function hotList()
  213. {
  214. $list = AgentProduct::list($this->agent_id)
  215. ->orderBy('sale', 'DESC')->orderBy('id', 'DESC')->simplePaginate();
  216. $list = $this->paginatePicAddHost($list);
  217. $list = $this->insertAd($list);
  218. return $this->success($list);
  219. }
  220. //分页列表产品图片加域名
  221. private function paginatePicAddHost($list)
  222. {
  223. //如果是新入驻代理商没有数据,且没有删除过的数据,则显示最早的几条
  224. if ($list->isEmpty()) {
  225. if (AgentProduct::where('agent_id', $this->agent_id)->withTrashed()->count() === 0 && request('page', 1) <= 2) { //只获取2页数据
  226. $list = AgentProduct::list($this->agent_id)->orWhere([['status', '=', ProductStatus::ON_SALE], ['price', '>', 500]])->orderBy('id')->simplePaginate();
  227. } else {
  228. return $list; //因为只获取2页数据,不return可能会导入下面的代码出错
  229. }
  230. }
  231. $prefix = Storage::disk('public')->url('');
  232. foreach ($list->items() as $k=>&$v) {
  233. $v->pictures = array_map(function($item) use ($prefix) {
  234. return strpos($item, $prefix) === false ? $prefix . $item : $item;
  235. }, $v->pictures);
  236. }
  237. return $list;
  238. }
  239. //插入瀑布流广告
  240. private function insertAd($list)
  241. {
  242. //插入瀑布流广告,分别在第8个和第16个插入,同时需要考虑到分页。当所有瀑布流广告插入完之后,再次循环插入
  243. if (is_object($list) && method_exists($list, 'toArray')) {
  244. $list = $list->toArray();
  245. }
  246. $ad_total = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])->count();
  247. if ($list['data'] && $ad_total > 0) {
  248. $page = (int)request()->input('page');
  249. $start = ($page ? $page - 1 : 0) * 2 % $ad_total;
  250. $ad = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])
  251. ->orderBy('sort')->orderBy('id', 'DESC')
  252. ->offset($start)->limit(2)->get(['title', 'picture', 'type', 'url'])->toArray();
  253. $prefix = Storage::disk('public')->url('');
  254. foreach ($ad as $k => &$v) {
  255. $v['is_ad'] = true;
  256. $v['picture'] = $prefix . $v['picture'];
  257. //每隔8个插入广告
  258. $temp = 8 * ($k+1);
  259. if (!empty($list['data'][$temp - 1]) && !empty($ad[$k])) {
  260. array_splice($list['data'], $temp + $k, 0, [$ad[$k]]);
  261. }
  262. }
  263. }
  264. return $list;
  265. }
  266. }