add tools request libs, migrate for hyperf framework

This commit is contained in:
2025-11-10 16:48:19 +08:00
parent 5b3b0c70f2
commit 08bb2af2c3
11 changed files with 749 additions and 0 deletions
@@ -0,0 +1,60 @@
<?php
namespace App\Platform\Tools;
use App\Utils\Log;
use GuzzleHttp\RetryMiddleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class GuzzleRetryConfig
{
const MAX_RETRIES = 3;
public static function retryPolicy() : callable
{
return function (int $retries, RequestInterface $request, ResponseInterface $response, ?\Throwable $exception):bool
{
return
// @TODO add host check when response is redirect may cause error
$retries < self::MAX_RETRIES
&& in_array($response->getStatusCode(), [429, 500]);
};
}
public static function retryDelay() : callable
{
return function (int $retries, ResponseInterface $response): int
{
$delay = 0;
if (!$response->hasHeader('Retry-After')) {
$delay = RetryMiddleware::exponentialDelay($retries);
} else {
$retryAfter = $response->getHeaderLine('Retry-After');
$delay = $retryAfter;
if (!is_numeric($retryAfter)) {
$delay = (new \DateTime($retryAfter))->getTimestamp() - time();
}
$delay = (int)$delay * 1000;
}
$code = $response->getStatusCode();
$seconds = $delay / 1000;
$message = "请求失败, 远程服务器响应码为 $code, 此为第 $retries 次尝试,{$seconds} 秒之后重试";
dump($message);
Log::warning($message);
return $delay;
};
}
}
+205
View File
@@ -0,0 +1,205 @@
<?php
namespace App\Platform\Tools;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\RetryMiddleware;
use App\Utils\Log;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Guzzle\CoroutineHandler;
/**
* Tools client - send request to wpic tools
*/
class HttpClient
{
const MAX_RETRIES = 3;
use RequestTrait;
static function create(): ClientInterface
{
$stack = HandlerStack::create(new CoroutineHandler());
// 添加自动重试中间件
$stack->push(Middleware::retry(self::retryPolicy(), self::retryDelay()));
$config = [
'headers' => [
'Accept' => 'application/json; charset=utf-8',
'Content-Type' => 'application/json; charset=utf-8',
'Hash' => self::getHash()
],
'base_uri' => self::getHost(),
'connect_timeout' => 10,
'timeout' => 60,
'handler' => $stack,
'debug' => false
];
return new Client($config);
}
/**
* 重试策略
*
* @return callable
*/
public static function retryPolicy(): callable
{
return function (int $retries, RequestInterface $request, ResponseInterface $response = null, \Throwable $exception = null): bool {
// 记录异常信息
if ($exception) {
dump($exception->getMessage());
Log::error("重试异常: " . $exception->getMessage());
}
// 检查是否为连接异常或请求异常(包括超时)
$should_retry = false;
if ($exception) {
// 处理连接异常(包括连接超时)
if ($exception instanceof ConnectException) {
$should_retry = true;
}
// 处理请求异常(包括请求超时)
elseif ($exception instanceof RequestException) {
// 如果是超时导致的请求异常
if (strpos($exception->getMessage(), 'timed out') !== false) {
$should_retry = true;
}
// 如果有响应,检查状态码
elseif ($exception->hasResponse()) {
$status_code = $exception->getResponse()->getStatusCode();
$should_retry = in_array($status_code, [429, 500, 502, 503, 504]);
}
}
}
// 首先检查重试次数
if ($retries >= self::MAX_RETRIES) {
return false;
}
// 检查状态码 (需要先确保 $response 不为 null)
$status_code_error = $response && in_array($response->getStatusCode(), [429, 500, 502, 503, 504]);
// 如果是应该重试的异常、状态码错误或响应为 null,则重试
return $should_retry || $status_code_error || is_null($response);
};
}
/**
* 重试延迟策略
*
* @return callable
*/
public static function retryDelay(): callable
{
return function (int $retries, ResponseInterface $response = null): int {
$delay = 0;
if (is_null($response) || !$response->hasHeader('Retry-After')) {
$delay = RetryMiddleware::exponentialDelay($retries) * 5;
} else {
$retryAfter = $response->getHeaderLine('Retry-After');
$delay = $retryAfter;
if (!is_numeric($retryAfter)) {
$delay = (new \DateTime($retryAfter))->getTimestamp() - time();
}
$delay = (int)$delay * 1000;
}
$seconds = $delay / 1000;
$message = "请求失败, 此为第 $retries 次尝试,{$seconds} 秒之后重试";
// 如果有响应,添加状态码信息
if ($response) {
$code = $response->getStatusCode();
dump($response->getBody()->getContents());
$message = "请求失败, 远程服务器响应码为 $code, 此为第 $retries 次尝试,{$seconds} 秒之后重试";
}
dump($message);
Log::error($message);
return $delay;
};
}
static function get(string $path, array $query = []): array
{
try{
$response = static::create()->request('GET', $path, ['query' => $query]);
// dump($response);
return static::parse($response);
}catch(\Throwable $exception) {
dump($exception->getMessage());
dump($exception->getTraceAsString());
exit(1);
}
}
static function post(string $path, array $query = [], array $data = []): array
{
try{
$response = static::create()->request('POST', $path, ['query' => $query, 'body' => json_encode($data)]);
// dump($response);
// dump(static::parse($response));
return static::parse($response);
}catch(\Throwable $exception){
dump($exception->getMessage());
dump($exception->getTraceAsString());
exit(1);
}
}
static function put(string $path, array $query = [], array $data = [])
{
try{
$response = static::create()->request('PUT', $path, ['query' => $query, 'body' => json_encode($data)]);
// dump($response);
return static::parse($response);
}catch(\Throwable $exception){
dump($exception->getMessage());
dump($exception->getTraceAsString());
exit(1);
}
}
static function delete(string $path)
{
try{
$response = static::create()->request('DELETE', $path);
// dump($response);
return static::parse($response);
}catch(\Throwable $exception){
dump($exception->getMessage());
dump($exception->getTraceAsString());
exit(1);
}
}
protected static function parse(ResponseInterface $response): array
{
$http_code = $response->getStatusCode();
$body = $response->getBody();
// when deal with stream object, need to move to pointer to the ram head
$body->rewind();
$content = $body->getContents();
$payload = \json_decode($content);
if ($http_code == 200) {
return $payload;
}
}
}
@@ -0,0 +1,75 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use Hyperf\Collection\LazyCollection;
class CompanyRequest extends HttpClient
{
public static function one(string $id): array
{
return parent::get("api/ext/companies/$id");
}
public static function find(string $name)
{
$stores = self::all();
$stores = $stores->filter(function($el) use ($name){
return strtolower($el['label']) == $name ? $el : null;
});
if($stores->isEmpty()){
throw new \InvalidArgumentException("tools 中未找到 name 为 $name 的公司信息,请确认 tools 系统中已创建该公司的记录!");
}
return $stores->first();
}
public static function all(int $page = 1, int $size = 500, $query = []): LazyCollection
{
$path = 'api/ext/companies';
$collection = new LazyCollection();
static::fetch($collection, $path, $page, $size, $query);
if($collection->isEmpty()){
throw new \InvalidArgumentException("获取的远程 tools 公司列表为空,请检查 token 配置");
}
return $collection;
}
protected static function fetch(LazyCollection &$collection, $path, $page, $size, $query)
{
$init_query = [
'page' => $page,
'size' => $size
];
$query = !empty($query) ? array_merge($init_query, $query) : $init_query;
$response = static::get($path, $query);
if($response['data']){
$collection = $collection->merge($response['data']);
}
if($response['page'] < $response['total_page']){
self::fetch($collection, $path, $page + 1, $size, $query);
}
}
public static function update(int $id, array $data): array
{
$path = "/api/ext/companies/$id";
return static::put($path, [], $data);
}
}
@@ -0,0 +1,32 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use GuzzleHttp\Client;
use Hyperf\Collection\LazyCollection;
class KpiRequest extends HttpClient
{
const BATCH_SIZE = 20;
public static function find(int $store_id, string $date): mixed
{
$path = "/api/ext/kpi";
$response = static::get($path, ['store' => $store_id, 'from' => $date, 'to' => $date]);
return isset($response['data']) && !empty($response['data']) ? $response['data']['0'] : null;
}
public static function add(array $data): array
{
$path = "/api/ext/kpi";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/kpi";
return static::put($path, [], $data);
}
}
@@ -0,0 +1,61 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use GuzzleHttp\Client;
use Hyperf\Collection\LazyCollection;
class OrderRequest extends HttpClient
{
const BATCH_SIZE = 50;
public static function find(int $id): array
{
$path = "/api/ext/orders/$id";
return static::get($path);
}
public static function findByPlatformOrderId(int $t_store_id, string $platform_order_id): array|bool
{
$path = "/api/ext/orders";
$query = [
'store' => $t_store_id,
'platformOrderId' => $platform_order_id,
];
$response = static::get($path, $query);
if(isset($response['data']) && is_array($response['data']) && count($response['data']) == 1) {
return $response['data']['0'];
}
return false;
}
public static function all(array $query):array
{
$path = "/api/ext/orders";
return static::get($path, $query);
}
public static function add(array $data): array
{
$path = "/api/ext/orders";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/orders";
return static::put($path, [], $data);
}
public static function queryByStorePlatformOrderIds(int $t_store_id, array $ids) : array
{
$path = "/api/ext/orders/batch-query-by-platform-order-ids";
return static::post($path, [], ['store' => $t_store_id, 'platformOrderIds' => $ids]);
}
}
@@ -0,0 +1,52 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
class ProductRequest extends HttpClient
{
const BATCH_SIZE = 50;
public static function find(int $id): array
{
$path = "/api/ext/products/$id";
return static::get($path);
}
public static function findByPlatformSkuId(int $tools_store_id, string $platformSkuId): array | null
{
$path = "/api/ext/products";
$response = static::get($path, ['store' => $tools_store_id, 'platformSkuId' => $platformSkuId]);
if(0 === $response['total'] || '0' === $response['total'] ){
return null;
}
if(1 < $response['total']){
throw new \Exception('参数有误,产品匹配到多个结果!');
}
return $response['data']['0'];
}
public static function add(array $data): array
{
$path = "/api/ext/products";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/products/batch";
return static::put($path, [], $data);
}
public static function list(array $query) : array
{
$path = "/api/ext/products";
return static::get($path, $query);
}
}
@@ -0,0 +1,52 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use Hyperf\Collection\LazyCollection;
class RefundRequest extends HttpClient
{
const BATCH_SIZE = 50;
public static function find(int $id): array
{
$path = "/api/ext/refunds/$id";
return static::get($path);
}
public static function findByOrder(int $t_store_id, int $t_order_id): array|bool
{
$path = "/api/ext/refunds";
$query = [
'store' => $t_store_id,
'order' => $t_order_id,
];
$response = static::get($path, $query);
if(isset($response['data']) && is_array($response['data']) && count($response['data']) == 1) {
return $response['data']['0'];
}
return false;
}
public static function add(array $data): array
{
$path = "/api/ext/refunds";
return static::post($path, [], $data);
}
public static function update(array $data): array
{
$path = "/api/ext/refunds";
return static::put($path, [], $data);
}
public static function queryByStorePlatformOrderIds(int $t_store_id, array $ids) : array
{
$path = "/api/ext/refunds/batch-query-by-ref-order-ids";
return static::post($path, [], ['store' => $t_store_id, 'orderIds' => $ids]);
}
}
@@ -0,0 +1,78 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
use Hyperf\Collection\LazyCollection;
class StoreRequest extends HttpClient
{
public static function one(string $id): array
{
return parent::get("api/ext/stores/$id");
}
public static function find(string $name, string $platform)
{
$stores = self::all();
$stores = $stores->filter(function($el) use ($name, $platform){
return $el['platform']['name'] == $platform && strtolower($el['label']) == $name ? $el : null;
});
if($stores->isEmpty()){
throw new \InvalidArgumentException("tools 中未找到平台为 $platform, name 为 $name 的店铺信息,请确认 tools 系统中已创建该平台和店铺的记录!");
}
return $stores->first();
}
public static function all(int $page = 1, int $size = 500, $query = []): LazyCollection
{
$path = 'api/ext/stores';
$collection = new LazyCollection();
static::fetch($collection, $path, $page, $size, $query);
if($collection->isEmpty()){
throw new \InvalidArgumentException("获取的远程 tools 商店列表为空,请检查 token 配置");
}
return $collection;
}
public static function orderNone(array $query) {
$path = 'api/ext/stores/order-none';
return static::get($path, $query);
}
protected static function fetch(LazyCollection &$collection, $path, $page, $size, $query)
{
$init_query = [
'page' => $page,
'size' => $size
];
$query = !empty($query) ? array_merge($init_query, $query) : $init_query;
$response = static::get($path, $query);
if($response['data']){
$collection = $collection->merge($response['data']);
}
if($response['page'] < $response['total_page']){
self::fetch($collection, $path, $page + 1, $size, $query);
}
}
public static function update(int $id, array $data): array
{
$path = "/api/ext/stores/$id";
return static::put($path, [], $data);
}
}
@@ -0,0 +1,86 @@
<?php
namespace App\Platform\Tools\Request;
use App\Platform\Tools\HttpClient;
class WarehouseInventoryRequest extends HttpClient
{
public static function find(int $id): array
{
$path = "/api/ext/warehouse-inventory/$id";
return static::get($path);
}
/**
* 根据仓库物品条件查询库存记录
*
* @param string $sku SKU编号
* @param string $barcode 条形码
* @param int $companyId 公司ID
* @param int $warehouseId 仓库ID
* @param string $warehouseSubId 子仓库ID
* @param int $inventoryType 库存类型,默认为1(良品库存)
* @return array|null 返回库存记录或null
*/
public static function findByWarehouseItem(
string $sku,
string $barcode,
int $companyId,
int $warehouseId,
string $warehouseSubId,
int $inventoryType = 1
): array|null {
$path = "/api/ext/warehouse-inventory";
$query = [
'sku' => $sku,
'barcode' => $barcode,
'company_id' => $companyId,
'warehouse_id' => $warehouseId,
'warehouse_sub_id' => $warehouseSubId,
'inventory_type' => $inventoryType,
];
$response = static::get($path, $query);
if (isset($response['data']) && is_array($response['data']) && count($response['data']) == 1) {
return $response['data'][0];
}
return null;
}
public static function add(array $data): array
{
$path = "/api/ext/warehouse-inventory";
return static::post($path, [], $data);
}
public static function update(int $id, array $data): array
{
$path = "/api/ext/warehouse-inventory/$id";
return static::put($path, [], $data);
}
public static function list(array $query) : array
{
$path = "/api/ext/warehouse-inventory";
return static::get($path, $query);
}
public static function remove(int $id): array
{
$path = "/api/ext/warehouse-inventory/$id";
return static::delete($path);
}
public static function updateHistory(array $data): array
{
$path = "/api/ext/warehouse-inventory-history";
return static::post($path, [], $data);
}
}
@@ -0,0 +1,31 @@
<?php
namespace App\Platform\Tools;
use function Hyperf\Config\config;
Trait RequestTrait
{
static function getHost() : string
{
// is debug mode
if(config('tools.debug', false)){
// use tools test host
return config('tools.host_test');
}
return config('tools.host');
}
static function getHash() : string
{
// is debug mode
if(config('tools.debug', false)){
// use tools test host
return config('tools.token_test');
}
return config('tools.token');
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Platform\Tools;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use GuzzleHttp\Psr7\Uri;
class ToolsRequestMatcher implements RequestMatcherInterface
{
public function matches(Request $request): bool
{
$limit = (new Uri(HttpClient::getHost()))->getHost();
return $limit === $request->getHost();
}
}