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.
		
		
		
		
		
			
		
			
				
					
					
						
							198 lines
						
					
					
						
							6.1 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							198 lines
						
					
					
						
							6.1 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Http\Controllers\Api;
							 | 
						|
								use App\Common\ProductStatus;
							 | 
						|
								use App\Http\Controllers\Controller;
							 | 
						|
								use App\Models\Advertising;
							 | 
						|
								use App\Models\AgentProduct;
							 | 
						|
								use App\Models\Product;
							 | 
						|
								use App\Models\UserFav;
							 | 
						|
								use Illuminate\Support\Facades\Storage;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 * 代理商产品
							 | 
						|
								 * Class AgentProductController
							 | 
						|
								 * @package App\Http\Controllers\Api
							 | 
						|
								 */
							 | 
						|
								class AgentProductController extends Controller
							 | 
						|
								{
							 | 
						|
									// 代理商产品列表
							 | 
						|
									public function index()
							 | 
						|
									{
							 | 
						|
										$category_id = request()->input('category_id');
							 | 
						|
										$where = [];
							 | 
						|
										if ($category_id) {
							 | 
						|
											$where['category_id'] = $category_id;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$list = AgentProduct::list($this->agent_id)->where($where)->orderBy('id', 'DESC')->simplePaginate();
							 | 
						|
										$list = $this->paginatePicAddHost($list);
							 | 
						|
										$list = $this->insertAd($list);
							 | 
						|
								
							 | 
						|
										return $this->success($list);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//首页搜索框
							 | 
						|
									public function search()
							 | 
						|
									{
							 | 
						|
										$keywords = request()->input('keywords');
							 | 
						|
										$type = request()->input('type', 0);
							 | 
						|
										$by = request()->input('by', 0);
							 | 
						|
										if (!$keywords) {
							 | 
						|
											return $this->error('请输入关键词');
							 | 
						|
										}
							 | 
						|
										$fields = ['id', 'sale', 'updated_at', 'price'];
							 | 
						|
								
							 | 
						|
										$field = $fields[$type] ?? $fields[0];
							 | 
						|
										$by = $by == 0 ? 'desc' : 'asc';
							 | 
						|
								
							 | 
						|
										$list = AgentProduct::list($this->agent_id)->where('title', 'like', "%$keywords%")->orderBy($field, $by)->simplePaginate();
							 | 
						|
								
							 | 
						|
										$list = $this->paginatePicAddHost($list);
							 | 
						|
								
							 | 
						|
										return $this->success($list);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									// 产品详情
							 | 
						|
									public function show()
							 | 
						|
									{
							 | 
						|
										$id = (int)request()->input('id');
							 | 
						|
								
							 | 
						|
										// TODO 优惠券查询待优化
							 | 
						|
										if (AgentProduct::withTrashed()->where('agent_id', $this->agent_id)->count() === 0) { //演示产品用
							 | 
						|
											$where = ['id' => $id, 'status' => ProductStatus::ON_SALE];
							 | 
						|
										} else {
							 | 
						|
											$where = ['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE];
							 | 
						|
										}
							 | 
						|
										$agent_product = AgentProduct::query()
							 | 
						|
											->with(['coupon:tag,agent_product_id'])
							 | 
						|
											->whereDoesntHave('agentProductItem', function ($query) {
							 | 
						|
												return $query->whereHas('product', function ($query) {
							 | 
						|
													return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
							 | 
						|
												});
							 | 
						|
											})
							 | 
						|
											->where('stock', '>', 0)
							 | 
						|
											->firstWhere($where);
							 | 
						|
								
							 | 
						|
										if (!$agent_product) {
							 | 
						|
											return $this->error('产品已下架或库存不足');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										$agent_product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->pictures);
							 | 
						|
								
							 | 
						|
										if ($this->user_id) {
							 | 
						|
											$agent_product->is_collect = UserFav::query()->where(['user_id' => $this->user_id, 'agent_product_id' => $id])->exists() ? 1 : 0; //判断是否收藏
							 | 
						|
										} else {
							 | 
						|
											$agent_product->is_collect = 0;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//计算折扣
							 | 
						|
										if ($agent_product->price < $agent_product->original_price) {
							 | 
						|
											$agent_product->cost = round($agent_product->price / $agent_product->original_price * 10, 1);
							 | 
						|
										} else {
							 | 
						|
											$agent_product->cost = '';
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//如果是单品销售,显示附加信息字段,组合产品和组团产品不显示
							 | 
						|
										if ($agent_product->type == 0) {
							 | 
						|
											$agent_product->product = Product::query()->where('id', $agent_product->product_id)->first(['type', 'extends']);
							 | 
						|
										} else {
							 | 
						|
											$agent_product->product = null;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
							 | 
						|
										return $this->success($agent_product);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									// 猜你喜欢
							 | 
						|
									public function guessLike()
							 | 
						|
									{
							 | 
						|
										$list = AgentProduct::list($this->agent_id)->orderBy('id', 'DESC')->simplePaginate();
							 | 
						|
										$list = $this->paginatePicAddHost($list);
							 | 
						|
										$list = $list->toArray();
							 | 
						|
										if (!empty($list['data']) && is_array($list['data'])) {
							 | 
						|
											shuffle($list['data']); //随机乱序
							 | 
						|
										}
							 | 
						|
										$list = $this->insertAd($list);
							 | 
						|
								
							 | 
						|
										return $this->success($list);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//【我的】页面下方推荐
							 | 
						|
									public function recommendList()
							 | 
						|
									{
							 | 
						|
										$list = AgentProduct::list($this->agent_id)->where(['is_rec' => 1])
							 | 
						|
											->orderBy('id', 'DESC')->simplePaginate();
							 | 
						|
										$list = $this->paginatePicAddHost($list);
							 | 
						|
										$list = $this->insertAd($list);
							 | 
						|
								
							 | 
						|
										return $this->success($list);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//人气爆款列表,销量排序
							 | 
						|
									public function hotList()
							 | 
						|
									{
							 | 
						|
										$list = AgentProduct::list($this->agent_id)
							 | 
						|
											->orderBy('sale', 'DESC')->orderBy('id', 'DESC')->simplePaginate();
							 | 
						|
										$list = $this->paginatePicAddHost($list);
							 | 
						|
										$list = $this->insertAd($list);
							 | 
						|
								
							 | 
						|
										return $this->success($list);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//分页列表产品图片加域名
							 | 
						|
									private function paginatePicAddHost($list)
							 | 
						|
									{
							 | 
						|
										//如果是新入驻代理商没有数据,且没有删除过的数据,则显示最早的几条
							 | 
						|
										if ($list->isEmpty()) {
							 | 
						|
											if (AgentProduct::list($this->agent_id)->withTrashed()->count() === 0 && request('page', 1) <= 2) { //只获取2页数据
							 | 
						|
												$list = AgentProduct::list($this->agent_id)->orWhere([['status', '=', ProductStatus::ON_SALE], ['price', '>', 500]])->orderBy('id')->simplePaginate();
							 | 
						|
											} else {
							 | 
						|
												return $list; //因为只获取2页数据,不return可能会导入下面的代码出错
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										foreach ($list->items() as $k=>&$v) {
							 | 
						|
											$v->pictures = array_map(function($item) use ($prefix) {
							 | 
						|
												return strpos($item, $prefix) === false ? $prefix . $item : $item;
							 | 
						|
											}, $v->pictures);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										return $list;
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//插入瀑布流广告
							 | 
						|
									private function insertAd($list)
							 | 
						|
									{
							 | 
						|
										//插入瀑布流广告,分别在第8个和第16个插入,同时需要考虑到分页。当所有瀑布流广告插入完之后,再次循环插入
							 | 
						|
										if (is_object($list) && method_exists($list, 'toArray')) {
							 | 
						|
											$list = $list->toArray();
							 | 
						|
										}
							 | 
						|
										$ad_total = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])->count();
							 | 
						|
										if ($list['data'] && $ad_total > 0) {
							 | 
						|
								
							 | 
						|
											$page = (int)request()->input('page');
							 | 
						|
											$start = ($page ? $page - 1 : 0) * 2 % $ad_total;
							 | 
						|
								
							 | 
						|
											$ad = Advertising::where(['agent_id' => $this->agent_id, 'status' => 1, 'display' => 2])
							 | 
						|
												->orderBy('sort')->orderBy('id', 'DESC')
							 | 
						|
												->offset($start)->limit(2)->get(['title', 'picture', 'type', 'url'])->toArray();
							 | 
						|
								
							 | 
						|
											$prefix = Storage::disk('public')->url('');
							 | 
						|
								
							 | 
						|
											foreach ($ad as $k => &$v) {
							 | 
						|
												$v['is_ad'] = true;
							 | 
						|
												$v['picture'] = $prefix . $v['picture'];
							 | 
						|
								
							 | 
						|
												//每隔8个插入广告
							 | 
						|
												$temp = 8 * ($k+1);
							 | 
						|
												if (!empty($list['data'][$temp - 1]) && !empty($ad[$k])) {
							 | 
						|
													array_splice($list['data'], $temp + $k, 0, [$ad[$k]]);
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
										return $list;
							 | 
						|
									}
							 | 
						|
								}
							 |