-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jackven
committed
Apr 6, 2021
0 parents
commit d5282ab
Showing
22 changed files
with
954 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = false | ||
|
||
[*.{vue,js,scss}] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
* text=auto | ||
|
||
/tests export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.scrutinizer.yml export-ignore | ||
.travis.yml export-ignore | ||
phpunit.php export-ignore | ||
phpunit.xml.dist export-ignore | ||
phpunit.xml export-ignore | ||
.php_cs export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.idea | ||
*.DS_Store | ||
/vendor | ||
/coverage | ||
sftp-config.json | ||
composer.lock | ||
.subsplit | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
$header = <<<EOF | ||
This file is part of the jackven/amap. | ||
(c) jackven <jackven@qq.com> | ||
This source file is subject to the MIT license that is bundled. | ||
EOF; | ||
|
||
return PhpCsFixer\Config::create() | ||
->setRiskyAllowed(true) | ||
->setRules(array( | ||
'@Symfony' => true, | ||
'header_comment' => array('header' => $header), | ||
'array_syntax' => array('syntax' => 'short'), | ||
'ordered_imports' => true, | ||
'no_useless_else' => true, | ||
'no_useless_return' => true, | ||
'php_unit_construct' => true, | ||
'php_unit_strict' => true, | ||
)) | ||
->setFinder( | ||
PhpCsFixer\Finder::create() | ||
->exclude('vendor') | ||
->in(__DIR__) | ||
) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 jackven | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<h1 align="center">VenAmap</h1> | ||
|
||
<p align="center">📦 It may be the best SDK for developing Amap App.</p> | ||
|
||
## Requirement | ||
|
||
1. PHP >= 7.2 | ||
2. **[Composer](https://getcomposer.org/)** | ||
|
||
## Installing | ||
|
||
|
||
## Usage | ||
|
||
基本使用 | ||
|
||
```php | ||
<?php | ||
|
||
use VenAmap\AmapClient; | ||
|
||
$config = [ | ||
'key' => 'xxxxxxxxxxxxxxxxxxxxxxx', | ||
'private_key' => 'xxxxxxxxxxxxxxx', // 未启用数字签名不用设置该项 | ||
]; | ||
|
||
$app = AmapClient::getInstance($config); | ||
$respone = $app->driver('ReGeo')->setLocation('116.481488,39.990464')->send(); | ||
|
||
``` | ||
|
||
也可以这样使用 | ||
|
||
```php | ||
<?php | ||
use VenAmap\AmapClient; | ||
use VenAmap\Request\ReGeo; | ||
|
||
$config = [ | ||
'key' => 'xxxxxxxxxxxxxxxxxxxxxxx', | ||
'private_key' => 'xxxxxxxxxxxxxxx', // 未启用数字签名不用设置该项 | ||
]; | ||
|
||
AmapClient::getInstance($config); | ||
|
||
$respone = (new ReGeo())->setLocation('116.481488,39.990464')->send(); | ||
|
||
``` | ||
|
||
## Contributing | ||
|
||
You can contribute in one of three ways: | ||
|
||
1. File bug reports using the [issue tracker](https://github.com/cnjackven/venamap/issues). | ||
2. Answer questions or fix bugs on the [issue tracker](https://github.com/cnjackven/venamap/issues). | ||
3. Contribute new features or update the wiki. | ||
|
||
_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._ | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "jackven\/amap", | ||
"description": "\uD83D\uDCE6 It may be the best SDK for developing Amap App.", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "jackven", | ||
"email": "jackven@qq.com" | ||
} | ||
], | ||
"require": { | ||
"guzzlehttp/guzzle": "^7.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"VenAmap\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Application Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
/** | ||
* --------------------------------------------------------------------------------------------------------------------- | ||
* FileName: AmapClient.php | ||
* Description: | ||
* --------------------------------------------------------------------------------------------------------------------- | ||
* Author: jackven <jackven@qq.com> | ||
* Date: 2021/4/2 | ||
* Version: 1.0 | ||
*/ | ||
|
||
namespace VenAmap; | ||
|
||
use VenAmap\Contracts\FactoryInterface; | ||
use InvalidArgumentException; | ||
|
||
class AmapClient implements FactoryInterface | ||
{ | ||
protected $output = "JSON"; | ||
protected $version = 'v3'; | ||
protected $gateway_uri = 'http://restapi.amap.com/'; | ||
protected $app_key = ''; | ||
protected $app_private_key = ''; | ||
private static $instance; | ||
|
||
/** | ||
* The array of created "drivers". | ||
*/ | ||
protected $requests = []; | ||
|
||
/** | ||
* 默认封装提供的接口封装 | ||
* @var string[] | ||
*/ | ||
protected $defaultDriver = [ | ||
'geo' => 'Geo', | ||
'ip' => 'IP', | ||
'regeo' => 'ReGeo', | ||
'walking' => 'Walking', | ||
'distance' => 'Distance', | ||
'convert' => 'Convert', | ||
'weatherinfo' => 'WeatherInfo', | ||
'district' => 'District', | ||
'poitext' => 'PoiText', | ||
]; | ||
|
||
private function __construct(array $config = []){ | ||
$this->setConfig($config); | ||
} | ||
|
||
/** | ||
* 设置接口请求配置信息 | ||
* @param $config | ||
*/ | ||
private function setConfig($config){ | ||
if(!empty($config) && isset($config['key'])){ | ||
$this->app_key = $config['key']; | ||
} | ||
|
||
if(!empty($config) && isset($config['private_key'])){ | ||
$this->app_private_key = $config['private_key']; | ||
} | ||
|
||
if(!empty($config) && isset($config['output'])){ | ||
$this->output = $config['output']; | ||
} | ||
} | ||
|
||
/** | ||
* 获取请求对象 | ||
* @param $driver | ||
* @return mixed | ||
*/ | ||
public function driver($driver){ | ||
$driver = strtolower($driver); | ||
if (!isset($this->requests[$driver])) { | ||
$this->requests[$driver] = $this->createRequest($driver); | ||
} | ||
return $this->requests[$driver]; | ||
} | ||
|
||
/** | ||
* send interface request | ||
* @param $uri | ||
* @param $params | ||
* @param string $method | ||
* @return mixed | ||
* @throws \HttpException | ||
*/ | ||
public function send($uri,$params,$method='GET'){ | ||
$params['key'] = $this->app_key; | ||
$params['output'] = $this->output; | ||
if(!is_null($this->app_private_key) && !empty($this->app_private_key)){ | ||
$params['sig'] = $this->getSign($params); | ||
} | ||
return $this->requestGateway($this->gateway_uri.$uri,$params,$method); | ||
} | ||
|
||
/** | ||
* Instantiate object | ||
* @param array $config | ||
* @return AmapClient | ||
*/ | ||
public static function getInstance( $config = []){ | ||
if(is_null(self::$instance)){ | ||
self::$instance = new self($config); | ||
} | ||
return self::$instance; | ||
} | ||
|
||
/** | ||
* 创建请求对象 | ||
* @param $driver | ||
* @return mixed | ||
*/ | ||
private function createRequest($driver){ | ||
|
||
if(!isset($this->defaultDriver[$driver])){ | ||
throw new InvalidArgumentException("Driver [$driver] not supported."); | ||
} | ||
|
||
$request = $this->defaultDriver[$driver]; | ||
$request = __NAMESPACE__.'\\Request\\'.$request; | ||
return new $request(); | ||
} | ||
|
||
/** | ||
* build request sign | ||
* @param $params | ||
* @return string | ||
*/ | ||
private function getSign($params){ | ||
ksort($params); | ||
$strArr = []; | ||
foreach($params as $k=>$v) $strArr[] = $k.'='.$v; | ||
return md5(implode('&',$strArr).$this->app_private_key); | ||
} | ||
|
||
/** | ||
* Encapsulate requests based on GuzzleHttp | ||
* @param $uri | ||
* @param $params | ||
* @param string $method | ||
* @return mixed | ||
* @throws \GuzzleHttp\Exception\GuzzleException | ||
* @throws \HttpException | ||
*/ | ||
private function requestGateway($uri,$params,$method='GET'){ | ||
|
||
$client = new \GuzzleHttp\Client(); | ||
switch ($method){ | ||
case 'GET': | ||
$respone = $client->get($uri,['query'=>$params]); | ||
break; | ||
case 'POST': | ||
$respone = $client->post($uri,['form_params'=>$params]); | ||
break; | ||
default: | ||
$respone = $client->post($uri,['form_params'=>$params]); | ||
} | ||
|
||
try { | ||
$respone = $respone->getBody(); | ||
return json_decode($respone,true); | ||
}catch (\Exception $e){ | ||
throw new \HttpException($e->getMessage(),$e->getCode()); | ||
} | ||
} | ||
|
||
private function __clone() | ||
{ | ||
// TODO: Implement __clone() method. | ||
} | ||
} |
Oops, something went wrong.