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

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