-
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
0 parents
commit 9f49e9f
Showing
12 changed files
with
220 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,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,9 @@ | ||
.idea | ||
*.DS_Store | ||
/vendor | ||
/coverage | ||
sftp-config.json | ||
composer.lock | ||
.subsplit | ||
.php_cs.cache | ||
.editorconfig |
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 madou1217/weather. | ||
(c) Model <madou1217@126.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,28 @@ | ||
<h1 align="center"> weather </h1> | ||
|
||
<p align="center"> 高德天气.</p> | ||
|
||
|
||
## Installing | ||
|
||
```shell | ||
$ composer require madou1217/weather -vvv | ||
``` | ||
|
||
## Usage | ||
|
||
TODO | ||
|
||
## Contributing | ||
|
||
You can contribute in one of three ways: | ||
|
||
1. File bug reports using the [issue tracker](https://github.com/madou1217/weather/issues). | ||
2. Answer questions or fix bugs on the [issue tracker](https://github.com/madou1217/weather/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,30 @@ | ||
{ | ||
"name": "madou1217\/weather", | ||
"description": "高德天气", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Model", | ||
"email": "madou1217@126.com" | ||
} | ||
], | ||
"require": { | ||
"guzzlehttp/guzzle": "^7.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Madou1217\\Weather\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Madou1217\\Weather\\ServiceProvider" | ||
] | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5", | ||
"mockery/mockery": "^1.5" | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Madou1217\Weather\Exceptions; | ||
|
||
class HttpException extends \Exception | ||
{ | ||
|
||
} |
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 @@ | ||
<?php | ||
|
||
namespace Madou1217\Weather\Exceptions; | ||
|
||
class InvalidArgumentException extends \Exception | ||
{ | ||
|
||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Madou1217\Weather; | ||
|
||
class ServiceProvider extends \Illuminate\Support\ServiceProvider | ||
{ | ||
protected $defer = true; | ||
|
||
public function register() | ||
{ | ||
$this->app->singleton(Weather::class, function () { | ||
return new Weather(config('weathers.amap.key')); | ||
}); | ||
|
||
$this->app->alias(Weather::class, 'weather'); | ||
} | ||
|
||
public function provides() | ||
{ | ||
return [Weather::class, 'weather']; | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
namespace Madou1217\Weather; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use InvalidArgumentException; | ||
use Madou1217\Weather\Exceptions\HttpException; | ||
|
||
class Weather | ||
{ | ||
private string $base_url; | ||
private Client $client; | ||
/** | ||
* 高德开放平台 应用 Key | ||
* @var string | ||
*/ | ||
private string $key; | ||
|
||
public function __construct(string $key) | ||
{ | ||
$this->base_url = "https://restapi.amap.com"; | ||
$this->client = new Client(); | ||
$this->key = $key; | ||
} | ||
|
||
|
||
/** | ||
* @param string $city city_name | city_code | ||
* @param int $type | ||
* @return array | ||
* @throws GuzzleException | ||
* @throws HttpException | ||
*/ | ||
public function weatherInfo(string $city, int $type = 1) | ||
{ | ||
$url = sprintf("%s/v3/weather/weatherInfo", $this->base_url); | ||
|
||
if (!\in_array(\strtolower($type), [1, 2])) { | ||
throw new InvalidArgumentException('Invalid type value(1:base/ 2:all),you are : '.$type); | ||
} | ||
try { | ||
$response = $this->client->get($url, [ | ||
'key' => $this->key, | ||
'city' => $city, | ||
'output' => 'json', | ||
'extensions' => $type == 1 ? 'base' : 'all', | ||
]); | ||
return json_decode($response->getBody()->getContents(), true) ?: []; | ||
} catch (\Exception $e) { | ||
throw new HttpException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
|
||
} |
Empty file.