LinkOS 服务接入
parent
62b78f812f
commit
9d2dfdac95
|
|
@ -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}"
|
||||
|
|
|
|||
|
|
@ -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'] ?? '');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -31,4 +31,9 @@ return [
|
|||
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
|
||||
],
|
||||
|
||||
'linkos' => [
|
||||
'key' => env('LINKOS_API_KEY'),
|
||||
'secret' => env('LINKOS_API_SECRET'),
|
||||
],
|
||||
|
||||
];
|
||||
|
|
|
|||
Loading…
Reference in New Issue