From 289f86c73d471dcf7ade32475963d7ceeed0689c Mon Sep 17 00:00:00 2001 From: weigang Date: Wed, 21 Oct 2020 15:44:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E9=94=AE=E8=AF=8D=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/v3/LocationController.php | 34 +++++++++++++++ .../v3/Implementations/LocationService.php | 41 +++++++++++++++++++ .../Interfaces/LocationServiceInterface.php | 1 + config/routes.php | 1 + 4 files changed, 77 insertions(+) diff --git a/app/Controller/v3/LocationController.php b/app/Controller/v3/LocationController.php index c1748c7..e01d386 100644 --- a/app/Controller/v3/LocationController.php +++ b/app/Controller/v3/LocationController.php @@ -2,10 +2,15 @@ namespace App\Controller\v3; +use App\Constants\v3\ErrorCode; use App\Controller\BaseController; +use App\Exception\ErrorCodeException; use App\Model\v3\Market; use App\Service\v3\Interfaces\LocationServiceInterface; use Hyperf\Di\Annotation\Inject; +use Hyperf\Validation\Contract\ValidatorFactoryInterface; +use Hyperf\Validation\ValidationException; + /** * 定位相关 * Class LocationController @@ -19,6 +24,12 @@ class LocationController extends BaseController */ protected $locationService; + /** + * @Inject() + * @var ValidatorFactoryInterface + */ + protected $validationFactory; + /** * 获取当前用户定位最近的市场 * 1.用户上报经纬度数据 @@ -47,4 +58,27 @@ class LocationController extends BaseController return $this->success($ret); } + + public function addrSuggestion() + { + + $validator = $this->validationFactory->make( + $this->request->all(), + ['keyword' => 'required|nonempty'], + ['keyword.*' => '关键字不能为空或undefined等'] + ); + + if ($validator->fails()){ + throw new ValidationException($validator); + } + + $keyword = $this->request->input('keyword'); + $marketId = $this->request->input('market_id'); + $page = $this->request->input('page'); + $pageSize = $this->request->input('pagesize'); + + $res = $this->locationService->placeSuggestion($keyword, $marketId, $page, $pageSize); + + return $this->success($res); + } } \ No newline at end of file diff --git a/app/Service/v3/Implementations/LocationService.php b/app/Service/v3/Implementations/LocationService.php index 561536f..01aed8a 100644 --- a/app/Service/v3/Implementations/LocationService.php +++ b/app/Service/v3/Implementations/LocationService.php @@ -9,6 +9,9 @@ use App\Service\v3\Interfaces\LocationServiceInterface; use App\Model\v3\Area; use App\Model\v3\Market; use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; +use Hyperf\Guzzle\ClientFactory; +use Hyperf\Utils\ApplicationContext; class LocationService implements LocationServiceInterface { @@ -168,4 +171,42 @@ class LocationService implements LocationServiceInterface $distance = $resultArr['result']['rows'][0]['elements'][0]['distance']; return $distance; } + + /** + * 根据关键词进行地址提示 + * 返回地址数据 + * @param $keyword + * @param string $marketId + * @param int $page + * @param int $pageSize + * @return string + * @throws GuzzleException + */ + public function placeSuggestion($keyword, $marketId = '', $page = 1, $pageSize = 10) + { + $apiPath = 'https://apis.map.qq.com/ws/place/v1/suggestion'; + + $queryString = 'page_index='.urlencode($page).'&page_size='.urlencode($pageSize).'&policy=1&keyword='.urlencode($keyword).'&key='.config('map.tencent'); + + // 获取当前市场所在城市 + if ($marketId) { + $cityId = Market::query()->where(['id' => $marketId])->value('city_id'); + $cityName = Area::query()->where(['id' => $cityId])->value('name'); + if ($cityName) { + $queryString .= '®ion='.urlencode($cityName); + } + } + + $client = ApplicationContext::getContainer()->get(ClientFactory::class)->create(); + $response = $client->request('GET', $apiPath.'?'.$queryString); + + $res = $response->getBody()->getContents(); + $res = json_decode($res, true); + + if ($res['status'] != 0) { + throw new ErrorCodeException($res['status'], $res['message']); + } + + return $res; + } } \ No newline at end of file diff --git a/app/Service/v3/Interfaces/LocationServiceInterface.php b/app/Service/v3/Interfaces/LocationServiceInterface.php index 9b7b6a1..e37bb26 100644 --- a/app/Service/v3/Interfaces/LocationServiceInterface.php +++ b/app/Service/v3/Interfaces/LocationServiceInterface.php @@ -15,4 +15,5 @@ interface LocationServiceInterface public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2); public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2); public function getNearestMarket($lng,$lat); + public function placeSuggestion($keyword, $marketId, $page = 1, $pageSize = 10); } \ No newline at end of file diff --git a/config/routes.php b/config/routes.php index 3a80997..617dcb7 100644 --- a/config/routes.php +++ b/config/routes.php @@ -98,6 +98,7 @@ Router::addGroup('/v3/', function () { Router::post('paramsToken/analyze', 'App\Controller\v3\ParamsTokenController@analyze'); Router::post('goods/getTags', 'App\Controller\v3\GoodsController@getTags'); Router::post('home/market', 'App\Controller\v3\HomeController@marketInfo'); + Router::post('location/addrSuggestion', 'App\Controller\v3\LocationController@addrSuggestion'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]); // 需要登录的路由