LinkOS 服务接入

dev
Jing Li 2022-10-09 10:50:41 +08:00
parent 62b78f812f
commit 9d2dfdac95
4 changed files with 126 additions and 2 deletions

View File

@ -51,6 +51,9 @@ PUSHER_PORT=443
PUSHER_SCHEME=https PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1 PUSHER_APP_CLUSTER=mt1
LINKOS_API_KEY=
LINKOS_API_SECRET=
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}" VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}" VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}" VITE_PUSHER_PORT="${PUSHER_PORT}"

View File

@ -2,8 +2,9 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\ServiceProvider; use App\Services\LinkosService;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@ -14,7 +15,12 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
// // 注册 LinkOS 服务
$this->app->singleton(LinkosService::class, function ($app) {
$config = $app['config']->get('services.linkos', []);
return new LinkosService($config['key'] ?? '', $config['secret'] ?? '');
});
} }
/** /**

View File

@ -0,0 +1,110 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class LinkosService
{
const BASE_URL = 'http://service.easylinkin.com/api';
/**
* @param string $apiKey
* @param string $apiSecret
*/
public function __construct(
protected readonly string $apiKey,
protected readonly string $apiSecret,
) {
}
/**
* 发起 GET 请求
*
* @param string $url
* @param array $query
* @return array
*/
public function get(string $url, array $query = []): array
{
return $this->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;
}
}

View File

@ -31,4 +31,9 @@ return [
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
], ],
'linkos' => [
'key' => env('LINKOS_API_KEY'),
'secret' => env('LINKOS_API_SECRET'),
],
]; ];