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
337 lines
10 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\Category;
|
|
use App\Models\UserFav;
|
|
use App\Models\Views\AgentProduct as AgentProductView;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* 代理商产品
|
|
* Class AgentProductController
|
|
* @package App\Http\Controllers\Api
|
|
*/
|
|
class AgentProductController extends Controller
|
|
{
|
|
// 代理商产品列表
|
|
public function index()
|
|
{
|
|
$formData = request()->only(['category_id', 'type', 'by', 'lat', 'lng']);
|
|
$category_id = $formData['category_id'] ?? 0;
|
|
$type = $formData['type'] ?? 0;
|
|
$by = $formData['by'] ?? 0;
|
|
$lat = $formData['lat'] ?? 0;
|
|
$lng = $formData['lng'] ?? 0;
|
|
|
|
$list = AgentProductView::list($this->agent_id)->addSelect('latitude', 'logitude', 'address');
|
|
if ($category_id) {
|
|
$list = $list->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
|
|
}
|
|
|
|
// 距离排序,TODO 优化
|
|
if ($type == 4) {
|
|
$list = $list->addSelect(DB::raw(<<<SQL
|
|
round(
|
|
(((6371.393 * 2) * asin(
|
|
sqrt((
|
|
pow( sin((((( {$lat} * 3.1415926 ) / 180 ) - (( `latitude` * pi()) / 180 )) / 2 )), 2 )
|
|
+
|
|
(
|
|
(cos((({$lat} * 3.1415926) / 180)) * cos(((`latitude` * pi()) / 180)))
|
|
*
|
|
pow( sin((((( {$lng} * 3.1415926 ) / 180 ) - (( `logitude` * pi()) / 180 )) / 2 )), 2 ))))
|
|
)) * 1000),0) AS `distance_m`
|
|
SQL));
|
|
}
|
|
|
|
$fields = ['id', 'sale', 'updated_at', 'price', 'distance_m']; //排序字段 TODO 还有距离排序
|
|
|
|
$field = $fields[$type] ?? $fields[0];
|
|
$by = $by == 0 ? 'desc' : 'asc';
|
|
|
|
$list = $list->orderBy($field, $by)->simplePaginate();
|
|
$list = $this->paginatePicAddHost($list);
|
|
$list = $this->insertAd($list);
|
|
|
|
return $this->success($list);
|
|
}
|
|
|
|
//递归获取指定分类下的所有子分类
|
|
private function get_category_child_ids($category_id): array
|
|
{
|
|
static $category = null;
|
|
if ($category === null) {
|
|
$category = Category::where('agent_id', $this->agent_id)->get()->toArray();
|
|
}
|
|
|
|
$child = [];
|
|
foreach ($category as $cat) {
|
|
if ($cat['pid'] == $category_id) {
|
|
$child[] = $cat['id'];
|
|
$child = array_merge($child, $this->get_category_child_ids($cat['id']));
|
|
}
|
|
}
|
|
|
|
return $child;
|
|
}
|
|
|
|
//首页搜索框
|
|
public function search()
|
|
{
|
|
$category_id = request()->input('category_id');
|
|
$keywords = request()->input('keywords');
|
|
$type = request()->input('type', 0);
|
|
$by = request()->input('by', 0);
|
|
|
|
$list = AgentProduct::list($this->agent_id);
|
|
|
|
if ($category_id) {
|
|
$list = $list->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
|
|
}
|
|
if ($keywords) {
|
|
$list = $list->where('title', 'like', "%$keywords%");
|
|
}
|
|
|
|
$fields = ['id', 'sale', 'updated_at', 'price']; //排序字段
|
|
|
|
$field = $fields[$type] ?? $fields[0];
|
|
$by = $by == 0 ? 'desc' : 'asc';
|
|
|
|
$list = $list->orderBy($field, $by)->simplePaginate();
|
|
$list = $this->paginatePicAddHost($list);
|
|
|
|
return $this->success($list);
|
|
}
|
|
|
|
//旅游线路搜索
|
|
public function travel_search()
|
|
{
|
|
$formData = request()->only(['departure_place', 'destination', 'type', 'by']);
|
|
$type = $formData['type'] ?? 0;
|
|
$by = $formData['by'] ?? 0;
|
|
|
|
if (empty($formData['departure_place']) && empty($formData['destination'])) {
|
|
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)->whereHas('product', function($query) use ($formData) {
|
|
//出发地
|
|
if (!empty($formData['departure_place'])) {
|
|
$query->whereRaw("extends->'$.field_0_departure_place' LIKE ?", ["%{$formData['departure_place']}%"]);
|
|
}
|
|
//目的地
|
|
if (!empty($formData['destination'])) {
|
|
$query->whereRaw("extends->'$.field_0_destination' LIKE ?", ["%{$formData['destination']}%"]);
|
|
}
|
|
})->orderBy($field, $by)->simplePaginate();
|
|
|
|
$list = $this->paginatePicAddHost($list);
|
|
return $this->success($list);
|
|
}
|
|
|
|
// 产品详情
|
|
public function show()
|
|
{
|
|
$id = (int)request()->input('id');
|
|
|
|
if (AgentProduct::where('agent_id', $this->agent_id)->withTrashed()->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::with([
|
|
'coupon:tag,agent_product_id',
|
|
'product' => function ($query) {
|
|
$query->select(['id', 'type', 'extends', 'diy_form_id'])->with('diyForm', function ($query) {
|
|
$query->select(['id', 'name'])->with('fields');
|
|
});
|
|
},
|
|
'spec' => function($query) {
|
|
return $query->has('productSpec')->with('productSpec', function ($query) {
|
|
$query->select(['id', 'name', 'date'])->where('date', '>=', date('Y-m-d'))->orderBy('date', 'asc');
|
|
});
|
|
}
|
|
])
|
|
->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 (!empty($agent_product->product->diyForm->fields) && !$agent_product->product->diyForm->fields->isEmpty()) {
|
|
foreach ($agent_product->product->diyForm->fields as &$v) {
|
|
if ($v['type'] == 'checkbox' && is_array($v['options'])) {
|
|
$v['options'] = array_map(function ($v2) {
|
|
$arr['checked'] = false;
|
|
$arr['disabled'] = false;
|
|
$arr['name'] = $v2;
|
|
return $arr;
|
|
}, $v['options']);
|
|
}
|
|
}
|
|
}
|
|
|
|
//处理规格
|
|
if (!$agent_product->spec->isEmpty()) {
|
|
$specs = $agent_product->spec->toArray();
|
|
|
|
//二维数组转一维
|
|
$specs = array_map(function ($v) {
|
|
if (is_array($v['product_spec'])) {
|
|
unset($v['product_spec']['id']);
|
|
$v = array_merge($v, $v['product_spec']);
|
|
}
|
|
unset($v['agent_product_id'], $v['product_spec_id'], $v['product_spec'], $v['deleted_at']);
|
|
return $v;
|
|
}, $specs);
|
|
|
|
//去年name和date为空的数组
|
|
$specs = array_filter($specs, fn($v) => isset($v['name'], $v['date']));
|
|
|
|
$names = array_column($specs, 'name');
|
|
$names = array_values(array_unique($names));
|
|
|
|
$result = [];
|
|
foreach ($names as $name) {
|
|
$list = array_filter($specs, fn($v) => $v['name'] == $name);
|
|
$result[] = [
|
|
'name' => $name,
|
|
'date_start' => min(array_column($list, 'date')),
|
|
'date_end' => max(array_column($list, 'date')),
|
|
'original_price' => min(array_column($list, 'original_price')),
|
|
'price' => min(array_column($list, 'price')),
|
|
'list' => array_values($list),
|
|
];
|
|
}
|
|
unset($agent_product->spec);
|
|
$agent_product->spec = $result;
|
|
}
|
|
|
|
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::where('agent_id', $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;
|
|
}
|
|
}
|