57 lines
1.4 KiB
PHP
57 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Iot\MoJing;
|
|
|
|
use App\Exceptions\MoJingException;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
/**
|
|
* 电信魔镜
|
|
*/
|
|
class HttpClient
|
|
{
|
|
const ENDPOINT_URL = 'https://mojing.sctel.com.cn';
|
|
|
|
public function __construct(
|
|
protected readonly string $appId,
|
|
protected readonly string $appSecret,
|
|
) {
|
|
}
|
|
|
|
public function get(string $url, array $query = []): array
|
|
{
|
|
return $this->request('GET', $url, [
|
|
'query' => $query,
|
|
]);
|
|
}
|
|
|
|
protected function request(string $method, string $url, array $options = []): array
|
|
{
|
|
$timestamp = now()->getTimestampMs();
|
|
|
|
$sign = md5($this->appId.$this->appSecret.$timestamp);
|
|
|
|
/** @var \Illuminate\Http\Client\Response */
|
|
$response = Http::baseUrl(static::ENDPOINT_URL)->withHeaders([
|
|
'Authorization' => base64_encode(
|
|
json_encode([
|
|
'appId' => $this->appId,
|
|
'time' => $timestamp,
|
|
'sign' => $sign,
|
|
])
|
|
),
|
|
])->send($method, $url, $options);
|
|
|
|
$result = $response->throw()->json();
|
|
|
|
$code = data_get($result, 'code', '-1');
|
|
if ($code === 200) {
|
|
return $result;
|
|
}
|
|
|
|
throw new MoJingException(
|
|
data_get($result, 'message', '请求失败').', 错误码: '.$code,
|
|
);
|
|
}
|
|
}
|