Browse Source

距离排序

master
Shuixiang 4 years ago
parent
commit
c56b238189
  1. 26
      app/Http/Controllers/Api/AgentProductController.php
  2. 8
      app/Models/Views/AgentProduct.php

26
app/Http/Controllers/Api/AgentProductController.php

@ -7,6 +7,8 @@ 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;
/**
@ -19,17 +21,35 @@ class AgentProductController extends Controller
// 代理商产品列表
public function index()
{
$formData = request()->only(['category_id', 'type', 'by']);
$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 = AgentProduct::list($this->agent_id);
$list = AgentProductView::list($this->agent_id);
if ($category_id) {
$list = $list->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
}
$fields = ['id', 'sale', 'updated_at', 'price']; //排序字段 TODO 还有距离排序
// 距离排序,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';

8
app/Models/Views/AgentProduct.php

@ -0,0 +1,8 @@
<?php
namespace App\Models\Views;
class AgentProduct extends \App\Models\AgentProduct
{
protected $table = 'view_agent_products';
}
Loading…
Cancel
Save