2024-05-25 14:06:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\helpers;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2024-05-27 03:45:15 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-05-25 14:06:58 +00:00
|
|
|
|
|
|
|
class apiRequest
|
|
|
|
{
|
|
|
|
|
2024-05-27 03:45:15 +00:00
|
|
|
public static Client|null $client = null;
|
2024-05-25 14:06:58 +00:00
|
|
|
|
|
|
|
public static function getClient(): Client
|
|
|
|
{
|
|
|
|
if (!self::$client) {
|
|
|
|
self::$client = new Client();
|
|
|
|
}
|
|
|
|
return self::$client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws GuzzleException
|
|
|
|
*/
|
2024-05-27 03:45:15 +00:00
|
|
|
public static function request(string $url, mixed $param = [], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []): mixed
|
2024-05-25 14:06:58 +00:00
|
|
|
{
|
2024-05-27 03:45:15 +00:00
|
|
|
return self::requests(self::getClient(), $url, $param, $type, $isJson, $timeout, $extern);
|
2024-05-25 14:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* json请求api
|
|
|
|
* @param Client $client
|
|
|
|
* @param string $url
|
|
|
|
* @param mixed $param
|
|
|
|
* @param FormType $type 1 x-www-form-urlencoded,2 json, 3 multipart/form-data 4 get query
|
|
|
|
* @param bool $isJson
|
|
|
|
* @param int $timeout
|
|
|
|
* @param int[] $extern
|
|
|
|
* @return mixed
|
|
|
|
* @throws GuzzleException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2024-05-27 03:45:15 +00:00
|
|
|
public static function requests(Client $client, string $url, mixed $param = [], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []): mixed
|
2024-05-25 14:06:58 +00:00
|
|
|
{
|
|
|
|
$params = [
|
|
|
|
'timeout' => $timeout
|
|
|
|
];
|
|
|
|
switch ($type) {
|
|
|
|
case FormType::urlencoded:
|
|
|
|
$params['form_params'] = $param;
|
|
|
|
$type = 'POST';
|
|
|
|
break;
|
|
|
|
case FormType::json:
|
|
|
|
$params['json'] = $param;
|
|
|
|
$type = 'POST';
|
|
|
|
break;
|
|
|
|
case FormType::formData:
|
|
|
|
$params['multipart'] = $param;
|
|
|
|
$type = 'POST';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$params['query'] = $param;
|
|
|
|
$type = 'GET';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($extern) {
|
|
|
|
$params = array_merge($params, $extern);
|
|
|
|
}
|
|
|
|
$r = $client->request($type, $url, $params);
|
|
|
|
$res = $r->getBody()->getContents();
|
|
|
|
if ($isJson) {
|
|
|
|
$data = json_decode($res, true);
|
|
|
|
if ($data === false) {
|
|
|
|
throw new Exception($res, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = $res;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2024-05-27 03:45:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws GuzzleException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function GetToken(): string
|
|
|
|
{
|
|
|
|
$token = Cache::get('token');
|
|
|
|
if ($token) {
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
$url = rtrim(env('API_DOMAIN'), '/') . '/Index/getToken';
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
|
|
$appId = env('APP_ID');
|
|
|
|
$appKey = env('APP_KEYX');
|
|
|
|
$sign = sprintf("app_id=%s:app_key=%s:tamptimes=%s", $appId, $appKey, $now);
|
|
|
|
$sign = strtoupper(md5($sign));
|
|
|
|
$form = [
|
|
|
|
'app_id' => $appId,
|
|
|
|
'app_key' => $appKey,
|
|
|
|
'tamptimes' => $now,
|
|
|
|
'sign' => $sign
|
|
|
|
];
|
|
|
|
$res = self::request($url, $form, FormType::json);
|
|
|
|
if (!$res['data']['token']) {
|
|
|
|
throw new Exception('获取token失败:' . $res['desc'] ?? '');
|
|
|
|
}
|
|
|
|
$token = $res['data']['token'];
|
|
|
|
Cache::set('token', $token, strtotime($res['data']['deadline']) - time());
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws GuzzleException
|
|
|
|
*/
|
|
|
|
public static function api($path, mixed $param = [], int $timeout = 5, array $extern = []): mixed
|
|
|
|
{
|
|
|
|
|
|
|
|
return self::apis($path, self::getClient(), $param, $timeout, $extern);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws GuzzleException
|
|
|
|
*/
|
|
|
|
public static function apis($path, Client $client, mixed $param = [], int $timeout = 5, array $extern = []): mixed
|
|
|
|
{
|
|
|
|
$url = rtrim(env('API_DOMAIN'), '/') . $path;
|
|
|
|
$token = self::GetToken();
|
|
|
|
$param['token'] = $token;
|
|
|
|
return self::requests($client, $url, $param, FormType::json, true, $timeout, $extern);
|
|
|
|
}
|
2024-05-25 14:06:58 +00:00
|
|
|
}
|