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

137 lines
3.9 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Common\ProductStatus;
use App\Http\Controllers\Controller;
use App\Models\AgentProduct;
use App\Models\UserFav;
use App\Models\WaterfallAd;
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');
if ($category_id) {
$where['category_id'] = $category_id;
}
$where['agent_id'] = $this->agent_id;
$list = AgentProduct::list()->where($where)->simplePaginate();
$list = $this->paginatePicAddHost($list);
$list = $this->insertAd($list);
return $this->success($list);
}
// 产品详情
public function show()
{
$id = (int)request()->input('id');
// TODO 优惠券查询待优化
$agent_product = AgentProduct::query()
->with('product:id,title,pictures,know,stock,content')
->with('coupon:tag,agent_product_id')
->with('fav:agent_product_id')
->whereHas('product', function ($query) {
return $query->where('status', ProductStatus::ON_SALE)->where('stock', '>', 0);
})
->where('stock', '>', 0)
->firstWhere(['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]);
if (!$agent_product) {
return $this->error('产品不存在或已下架');
}
$prefix = Storage::disk('public')->url('');
$agent_product->product->pictures = array_map(fn($item) => ($prefix . $item), $agent_product->product->pictures);
$agent_product->is_collect = !is_null($agent_product->fav); //判断是否收藏
//计算折扣
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 = '';
}
unset($agent_product->agent_id, $agent_product->status, $agent_product->deleted_at);
return $this->success($agent_product);
}
// 猜你喜欢
public function guessLike()
{
// TODO 此处需要再优化排序规则,并增加广告功能
return $this->index();
}
//【我的】页面下方推荐
public function recommendList()
{
//TODO 推荐数据暂时使用产品列表,后期需要通过后台设置获取或根据用户购买过的关键词获取
return $this->index();
}
//人气爆款列表
public function hotList()
{
//TODO 具体排序规则,后期再做修改
return $this->index();
}
//分页列表产品图片加域名
private function paginatePicAddHost($list)
{
if (!$list->isEmpty()) {
$prefix = Storage::disk('public')->url('');
foreach ($list->items() as $k=>&$v) {
$v->product->pictures = array_map(function($item) use ($prefix) {
return strpos($item, $prefix) === false ? $prefix . $item : $item;
}, $v->product->pictures);
}
}
return $list;
}
//插入瀑布流广告
private function insertAd($list)
{
//插入瀑布流广告,分别在第8个和第16个插入,同时需要考虑到分页。当所有瀑布流广告插入完之后,再次循环插入
$list = $list->toArray();
if ($list['data']) {
$ad_total = WaterfallAd::where(['agent_id' => $this->agent_id, 'status' => 1])->count();
$page = (int)request()->input('page');
$start = ($page ? $page - 1 : 0) * 2 % $ad_total;
$ad = WaterfallAd::where(['agent_id' => $this->agent_id, 'status' => 1])
->orderBy('sort')->orderBy('id', 'DESC')
->offset($start)->limit(2)->get(['title', 'picture', 'type', 'url'])->toArray();
$prefix = Storage::disk('public')->url('');
//每隔8个插入广告
$gap = 4;
foreach ($ad as $k => &$v) {
$v['is_ad'] = true;
$v['picture'] = $prefix . $v['picture'];
//插入广告
$temp = $gap * ($k+1);
if (!empty($list['data'][$temp - 1]) && !empty($ad[$k])) {
array_splice($list['data'], $temp + $k, 0, [$ad[$k]]);
}
}
}
return $list;
}
}