Browse Source

计算距离

master
Lemon 5 years ago
parent
commit
47f5a1cedf
  1. 76
      app/Commons/TencentMap.php
  2. 19
      app/Service/v3/Implementations/LocationService.php
  3. 3
      config/config.php

76
app/Commons/TencentMap.php

@ -1,76 +0,0 @@
<?php
namespace App\Commons;
use Hyperf\Guzzle\ClientFactory;
class TencentMap
{
/**
* @var \Hyperf\Guzzle\ClientFactory
*/
private $clientFactory;
public function __construct(ClientFactory $clientFactory)
{
$this->clientFactory = $clientFactory;
}
public function getClient()
{
// $options 等同于 GuzzleHttp\Client 构造函数的 $config 参数
$options = [
'timeout' => 2.0,
];
// $client 为协程化的 GuzzleHttp\Client 对象
$client = $this->clientFactory->create($options);
return $client;
}
public function event($labels=null,$datas){
co(function () use ($labels,$datas){
$client = $this->getClient();
$kv = [];
foreach ($datas as $key => $value) {
$kv[] = $key."=".$value;
}
$pushLabels = [];
$event_name = 'event_'.env('APP_ENV');
if(!empty($labels)) $pushLabels[$event_name] = $labels;
/*
* data format:
curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \
'{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}'
*/
$ts = $this->getMsecTime() . '000000';
$datas = implode("&",$kv);
$values = [[$ts,$datas]];
$app_name = env('APP_NAME').'_'.env('APP_ENV');
$pushLabels['app']= $app_name;
$pushDatas = [
'streams'=>[
[
'stream'=>$pushLabels,
'values'=>$values,
]
]
];
$client->post(
env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push',
[
'headers'=>[
'Content-Type'=>'application/json'
],
'body' => json_encode($pushDatas)
]
);
//var_dump(json_encode($pushDatas) );
});
}
}

19
app/Service/v3/Implementations/LocationService.php

@ -8,6 +8,8 @@ use App\Model\v3\UserCollection;
use App\Service\v3\Interfaces\LocationServiceInterface;
use App\Model\v3\Area;
use App\Model\v3\Market;
use GuzzleHttp\Client;
class LocationService implements LocationServiceInterface
{
@ -146,13 +148,20 @@ class LocationService implements LocationServiceInterface
* $tyep driving 驾车 walking 步行 bicycling 骑行 transit 公交
*/
$type = 'bicycling';
$key = 'GB3BZ-7W2CU-LW3VH-B7ZIF-XRKSF-D3FOL';
$result = file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
$resultArr = json_decode($result,true);
$key = config('map.tencent');
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://apis.map.qq.com/ws/direction/v1/bicycling/',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('GET', $url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key);
$response = $response->getBody()->getContents();
$resultArr = json_decode($response,true);
if($resultArr['status'] > 0)
{
return 1000;
//throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
//return 1000;
throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']);
}
$distance = $resultArr['result']['routes'][0]['distance'];
return $distance;

3
config/config.php

@ -55,4 +55,7 @@ return [
'lgoin' => [
'authkey' => env('STORE_LOGIN_AUTHKEY', ''),
],
'map' => [
'tencent' => env('TENCENT_MAP_KEY', ''),
],
];
Loading…
Cancel
Save