From 3d1d58ac61f1b1045ed680a2de8247ac09ca60aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 28 Apr 2023 11:16:23 +0800 Subject: [PATCH] =?UTF-8?q?LinkOS=20Http=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Iot/Linkos/HttpClient.php | 114 +++++++++++++++++++++++++++ app/Providers/AppServiceProvider.php | 17 +++- config/services.php | 5 ++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 app/Iot/Linkos/HttpClient.php diff --git a/app/Iot/Linkos/HttpClient.php b/app/Iot/Linkos/HttpClient.php new file mode 100644 index 0000000..9b7265a --- /dev/null +++ b/app/Iot/Linkos/HttpClient.php @@ -0,0 +1,114 @@ +post('/api/deviceFlow/v1/list', [ + 'device_id' => $deviceId, + 'start_time' => $start->unix() * 1000, + 'end_time' => $end->unix() * 1000, + 'pageable' => array_merge([ + 'page' => 0, + 'size' => 20, + ], $pageable), + ]); + + return $result['data']; + } + + /** + * @param string $url + * @param array $data + * @return array + */ + public function post(string $url, array $data = []): array + { + return $this->request('POST', $url, [ + 'json' => $data, + ]); + } + + /** + * @param string $method + * @param string $url + * @param array $options + * @return array + * + * @throws \Illuminate\Http\Client\RequestException + * @throws \RuntimeException + */ + public function request(string $method, string $url, array $options = []): array + { + $nonce = $this->nonce(); + + $timestamp = now()->getTimestampMs(); + + /** @var \Illuminate\Http\Client\Response */ + $response = Http::withHeaders([ + 'Content-Type' => 'application/json', + 'api-key' => $this->apiKey, + 'Nonce' => $nonce, + 'Timestamp' => $timestamp, + 'Signature' => $this->sign(compact('nonce', 'timestamp')), + ])->baseUrl(self::ENDPOINT_URL)->send($method, $url, $options); + + $result = $response->throw()->json(); + + if (data_get($result, 'success') !== true) { + throw new RuntimeException(data_get($result, 'msg', '出错啦!')); + } + + return $result; + } + + /** + * @param array $data + * @return string + */ + protected function sign(array $data): string + { + return sha1( + sprintf( + '%s%s%s', + $data['nonce'] ?? '', + $data['timestamp'] ?? '', + $this->apiSecret + ) + ); + } + + protected function nonce(): string + { + $nonce = ''; + + for ($i = 0; $i < 8; $i++) { + $nonce .= mt_rand(0, 9); + } + + return $nonce; + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4053365..9d6ce46 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Iot\Linkos\HttpClient as LinkosHttpClient; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -13,7 +14,7 @@ class AppServiceProvider extends ServiceProvider */ public function register() { - // + $this->registerLinkos(); } /** @@ -23,7 +24,19 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - // \Schema::defaultStringLength(191); } + + /** + * 注册 Linkos 服务 + */ + protected function registerLinkos(): void + { + $this->app->singleton(LinkosHttpClient::class, function ($app) { + return new LinkosHttpClient( + (string) $app['config']->get('services.linkos.key'), + (string) $app['config']->get('services.linkos.secret') + ); + }); + } } diff --git a/config/services.php b/config/services.php index 0ace530..40b812a 100644 --- a/config/services.php +++ b/config/services.php @@ -31,4 +31,9 @@ return [ 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], + 'linkos' => [ + 'key' => env('LINKOS_API_KEY'), + 'secret' => env('LINKOS_API_SECRET'), + ], + ];