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

337 lines
10 KiB

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