From 9d2dfdac95590ac59670815c3a2af97b8ad8920f Mon Sep 17 00:00:00 2001 From: Jing Li Date: Sun, 9 Oct 2022 10:50:41 +0800 Subject: [PATCH] =?UTF-8?q?LinkOS=20=E6=9C=8D=E5=8A=A1=E6=8E=A5=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 3 + app/Providers/AppServiceProvider.php | 10 ++- app/Services/LinkosService.php | 110 +++++++++++++++++++++++++++ config/services.php | 5 ++ 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 app/Services/LinkosService.php diff --git a/.env.example b/.env.example index 00b6110..e057235 100644 --- a/.env.example +++ b/.env.example @@ -51,6 +51,9 @@ PUSHER_PORT=443 PUSHER_SCHEME=https PUSHER_APP_CLUSTER=mt1 +LINKOS_API_KEY= +LINKOS_API_SECRET= + VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}" VITE_PUSHER_HOST="${PUSHER_HOST}" VITE_PUSHER_PORT="${PUSHER_PORT}" diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 2bca3df..5fca5bc 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,8 +2,9 @@ namespace App\Providers; -use Illuminate\Support\ServiceProvider; +use App\Services\LinkosService; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { @@ -14,7 +15,12 @@ class AppServiceProvider extends ServiceProvider */ public function register() { - // + // 注册 LinkOS 服务 + $this->app->singleton(LinkosService::class, function ($app) { + $config = $app['config']->get('services.linkos', []); + + return new LinkosService($config['key'] ?? '', $config['secret'] ?? ''); + }); } /** diff --git a/app/Services/LinkosService.php b/app/Services/LinkosService.php new file mode 100644 index 0000000..870e3c6 --- /dev/null +++ b/app/Services/LinkosService.php @@ -0,0 +1,110 @@ +request('GET', $url, [ + 'query' => $query, + ]); + } + + /** + * 发起 POST 请求 + * + * @param string $url + * @param array $data + * @return array + */ + public function post(string $url, array $data = []): array + { + return $this->request('POST', $url, [ + 'json' => $data, + // 'form_params' => $data, + ]); + } + + /** + * 发起 HTTP 请求 + * + * @param string $method + * @param string $url + * @param array $options + * @return array + */ + protected function request(string $method, string $url, array $options = []): array + { + // 随机字符串 + $nonce = $this->nonce(); + // 毫秒时间戳 + $timestamp = now()->getTimestampMs(); + + $options['headers'] = array_merge([ + 'Content-Type' => 'application/json', + 'api-key' => $this->apiKey, + 'Nonce' => $nonce, + 'Timestamp' => $timestamp, + 'Signature' => $this->sign($nonce, $timestamp), + ], $options['headers'] ?? []); + + $response = Http::baseUrl(static::BASE_URL)->send($method, $url, $options); + + // HTTP request returned status code 500: {"msg":"系统异常,接口调用失败","code":"default","success":false} + $response->throw(); + + return $response->json(); + } + + /** + * 生成签名 + * + * @param string $nonce + * @param int $timestamp + * @return string + */ + public function sign(string $nonce, int $timestamp): string + { + return sha1($nonce.$timestamp.$this->apiSecret); + } + + /** + * @param int $length + * @param string $charset + * @return string + */ + protected function nonce(int $length = 8, string $charset = '0123456789'): string + { + $max = strlen($charset) - 1; + + $nonce = ''; + + for ($i = 0; $i < $length; $i++) { + $nonce .= $charset[mt_rand(0, $max)]; + } + + return $nonce; + } +} 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'), + ], + ];