goodsnotices/app/helpers/apiRequest.php
zhouchangcheng 3da00000d6 完善
2024-05-27 17:52:02 +08:00

152 lines
4.3 KiB
PHP

<?php
namespace App\helpers;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;
class apiRequest
{
public static Client|null $client = null;
public static function getClient(): Client
{
if (!self::$client) {
self::$client = new Client();
}
return self::$client;
}
public static function SetClient(Client $client): void
{
self::$client = $client;
}
/**
* @throws GuzzleException
*/
public static function request(string $url, mixed $param = [], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []): mixed
{
return self::requests(self::getClient(), $url, $param, $type, $isJson, $timeout, $extern);
}
/**
* 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
*/
public static function requests(Client $client, string $url, mixed $param = [], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []): mixed
{
$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;
}
/**
* @throws GuzzleException
* @throws Exception
*/
public static function getToken(): string
{
$token = Cache::get('token');
if ($token) {
return $token;
}
$appId = env('APP_ID');
if (!$appId) {
throw new Exception('app_id不能为空');
}
$appKey = env('APP_KEYX');
if (!$appKey) {
throw new Exception('app_keyx不能为空');
}
$url = rtrim(env('API_DOMAIN'), '/') . '/Index/getToken';
$now = date('Y-m-d H:i:s');
$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() - 300);
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
* @throws Exception
*/
public static function apis($path, Client $client, mixed $param = [], int $timeout = 5, array $extern = []): mixed
{
$domain = env('API_DOMAIN');
if (!$domain) {
throw new Exception('接口域名不能为空');
}
$url = rtrim($domain, '/') . $path;
$token = self::getToken();
$param['token'] = $token;
return self::requests($client, $url, $param, FormType::json, true, $timeout, $extern);
}
}