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

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