Commit d1e2c132 by banice

附近网点附近站点站点搜索

parent c331301f
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
namespace App\Console\Commands\station; namespace App\Console\Commands\station;
use App\model\ServiceModel;
use App\model\SiteModel; use App\model\SiteModel;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use League\Geotools\Coordinate\Coordinate; use League\Geotools\Coordinate\Coordinate;
...@@ -27,8 +28,8 @@ class GeoHash extends Command ...@@ -27,8 +28,8 @@ class GeoHash extends Command
*/ */
public function handle() public function handle()
{ {
$sites = SiteModel::all(['id', 'longitude', 'latitude'])->toArray(); // $sites = SiteModel::all(['id', 'longitude', 'latitude'])->toArray();
$sites = ServiceModel::all(['id', 'longitude', 'latitude'])->toArray();
$geoTools = new Geotools(); $geoTools = new Geotools();
foreach ($sites as $site) { foreach ($sites as $site) {
...@@ -38,7 +39,7 @@ class GeoHash extends Command ...@@ -38,7 +39,7 @@ class GeoHash extends Command
$encoded = $geoTools->geohash()->encode($coorToGeoHash, 5); $encoded = $geoTools->geohash()->encode($coorToGeoHash, 5);
$geoHashValue = $encoded->getGeohash(); $geoHashValue = $encoded->getGeohash();
DB::table('sites')->where('id', $site['id'])->update([ DB::table('services')->where('id', $site['id'])->update([
'geo_hash' => $geoHashValue 'geo_hash' => $geoHashValue
]); ]);
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/26
* Time: 下午2:25
*/
namespace App\Http\Controllers\v1\service;
use App\Http\Controllers\Controller;
use App\model\ServiceModel;
use Illuminate\Http\Request;
use League\Geotools\Coordinate\Coordinate;
use League\Geotools\Geotools;
use Validator;
class NeighbourController extends Controller
{
public function get(Request $request)
{
$validator = Validator::make($request->all(),
[
'lng' => 'required',
'lat' => 'required'
]);
if ($validator->fails()) {
abort(400, $validator->errors()->first());
}
$geoTools = new Geotools();
$lng = $request->get('lng');
$lat = $request->get('lat');
$centerPoint = new Coordinate([$lat, $lng]);
$encoded = $geoTools->geohash()->encode($centerPoint, 5);
$geoHashValue = $encoded->getGeohash();//比对相同的value
$services = ServiceModel::where('geo_hash', $geoHashValue)->get(['id', 'type', 'service_time', 'address', 'longitude', 'latitude', 'service_phone'])->toArray();
$neighbourSite = [];
foreach ($services as $service) {
$point = new Coordinate([$service['latitude'], $service['longitude']]);
$distance = $geoTools->distance()->setFrom($centerPoint)->setTo($point);
$distanceValue = $distance->flat();
if ($distanceValue <= 2000) {
$tmp = $service;
if (blank($service['address'])) {
$tmp['address'] = '';
}
$tmp['distance'] = $distanceValue;
array_push($neighbourSite, $tmp);
}
}
$serviceCount = count($neighbourSite);
$data = [];
$data['service_count'] = $serviceCount;
if ($serviceCount !== 0) {
$data['service'] = $neighbourSite;
}
return $this->response->array($this->getResponseCode() + ['data' => $data]);
}
}
\ No newline at end of file
...@@ -10,6 +10,7 @@ namespace App\Http\Controllers\v1\station; ...@@ -10,6 +10,7 @@ namespace App\Http\Controllers\v1\station;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\model\DoorModel;
use App\model\SiteModel; use App\model\SiteModel;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
...@@ -21,8 +22,40 @@ use League\Geotools\Coordinate\Ellipsoid; ...@@ -21,8 +22,40 @@ use League\Geotools\Coordinate\Ellipsoid;
class NeighbourController extends Controller class NeighbourController extends Controller
{ {
private function test() public function siteDetail(Request $request, $id)
{ {
$validator = Validator::make(['site_id' => $id], [
'site_id' => 'required|exists:sites,id'
]);
if ($validator->fails() === true) {
abort(400, $validator->errors()->first());
}
$boxCount = DoorModel::where('site_id', $id)->count();
$boxes = DoorModel::where('site_id', $id)->get()->groupBy('box_id')->toArray();
$responseBox = [];
foreach ($boxes as $boxId => $doors) {
$tmp = [];
$tmp['box_code'] = $doors[0]['box_code'];
$tmp['box_id'] = $doors[0]['box_id'];
$tmp['door_count'] = count($doors);
$tmp['door'] = $doors;
array_push($responseBox, $tmp);
}
return $this->response->array($this->getResponseCode() + [
'data' => [
'box_code' => $boxCount,
'box' => $responseBox
]
]);
} }
...@@ -45,11 +78,9 @@ class NeighbourController extends Controller ...@@ -45,11 +78,9 @@ class NeighbourController extends Controller
$centerPoint = new Coordinate([$lat, $lng]); $centerPoint = new Coordinate([$lat, $lng]);
$encoded = $geoTools->geohash()->encode($centerPoint, 5); $encoded = $geoTools->geohash()->encode($centerPoint, 5);
$geoHashValue = $encoded->getGeohash();//比对相同的value $geoHashValue = $encoded->getGeohash();//比对相同的value
Log::debug($geoHashValue);
$sites = SiteModel::where('geo_hash', $geoHashValue)->get(['longitude', 'latitude', 'img', 'status', 'name', 'id', 'province', 'city', 'area', 'area_name', 'address', 'service_time'])->toArray(); $sites = SiteModel::where('geo_hash', $geoHashValue)->get(['longitude', 'latitude', 'img', 'status', 'name', 'id', 'province', 'city', 'area', 'area_name', 'address', 'service_time'])->toArray();
$neighbourSite = []; $neighbourSite = [];
foreach ($sites as $site) { foreach ($sites as $site) {
$point = new Coordinate([$site['latitude'], $site['longitude']]); $point = new Coordinate([$site['latitude'], $site['longitude']]);
$distance = $geoTools->distance()->setFrom($centerPoint)->setTo($point); $distance = $geoTools->distance()->setFrom($centerPoint)->setTo($point);
...@@ -71,6 +102,7 @@ class NeighbourController extends Controller ...@@ -71,6 +102,7 @@ class NeighbourController extends Controller
$data = []; $data = [];
$data['site_count'] = $sitesCount; $data['site_count'] = $sitesCount;
if ($sitesCount !== 0) { if ($sitesCount !== 0) {
$data['img_domain'] = '.com';//fixme
$data['site'] = $neighbourSite; $data['site'] = $neighbourSite;
} }
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/26
* Time: 下午2:23
*/
namespace App\model;
use Illuminate\Database\Eloquent\Model;
class ServiceModel extends Model
{
protected $table = 'services';
}
\ No newline at end of file
...@@ -70,6 +70,10 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/ ...@@ -70,6 +70,10 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/
$api->get('sites/neighbour', 'station\NeighbourController@get'); $api->get('sites/neighbour', 'station\NeighbourController@get');
$api->get('site{id}', 'station\NeighbourController@siteDetail');
$api->get('service/neighbour', 'service\NeighbourController@get');
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment