goodsnotices/app/helpers/apiRequest.php

152 lines
4.3 KiB
PHP
Raw Permalink Normal View History

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;
}
2024-05-27 04:13:29 +00:00
public static function SetClient(Client $client): void
{
self::$client = $client;
}
2024-05-25 14:06:58 +00:00
/**
* @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
*/
2024-05-27 09:52:02 +00:00
public static function getToken(): string
2024-05-27 03:45:15 +00:00
{
$token = Cache::get('token');
if ($token) {
return $token;
}
$appId = env('APP_ID');
2024-05-27 09:52:02 +00:00
if (!$appId) {
throw new Exception('app_id不能为空');
}
2024-05-27 03:45:15 +00:00
$appKey = env('APP_KEYX');
2024-05-27 09:52:02 +00:00
if (!$appKey) {
throw new Exception('app_keyx不能为空');
}
$url = rtrim(env('API_DOMAIN'), '/') . '/Index/getToken';
$now = date('Y-m-d H:i:s');
2024-05-27 03:45:15 +00:00
$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'];
2024-05-27 04:13:29 +00:00
Cache::set('token', $token, strtotime($res['data']['deadline']) - time() - 300);
2024-05-27 03:45:15 +00:00
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
2024-05-27 09:52:02 +00:00
* @throws Exception
2024-05-27 03:45:15 +00:00
*/
public static function apis($path, Client $client, mixed $param = [], int $timeout = 5, array $extern = []): mixed
{
2024-05-27 09:52:02 +00:00
$domain = env('API_DOMAIN');
if (!$domain) {
throw new Exception('接口域名不能为空');
}
$url = rtrim($domain, '/') . $path;
$token = self::getToken();
2024-05-27 03:45:15 +00:00
$param['token'] = $token;
return self::requests($client, $url, $param, FormType::json, true, $timeout, $extern);
}
2024-05-25 14:06:58 +00:00
}