mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKit.git
synced 2026-01-15 00:35:08 +08:00
@@ -24,6 +24,7 @@
|
||||
"hyperf/database": "^2.2",
|
||||
"hyperf/di": "^2.2",
|
||||
"hyperf/framework": "^2.2",
|
||||
"hyperf/guzzle": "^2.2",
|
||||
"hyperf/http-server": "^2.2",
|
||||
"hyperf/logger": "^2.2",
|
||||
"hyperf/redis": "^2.2",
|
||||
|
||||
1214
composer.lock
generated
1214
composer.lock
generated
File diff suppressed because it is too large
Load Diff
34
src/Utils/Middleware/ExtendsMiddleware.php
Normal file
34
src/Utils/Middleware/ExtendsMiddleware.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Singularity\HDK\Utils\Middleware;
|
||||
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Singularity\HDK\Utils\Service\ExtendService;
|
||||
|
||||
/**
|
||||
* 扩展资源中间件
|
||||
*/
|
||||
class ExtendsMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var \Singularity\HDK\Utils\Service\ExtendService
|
||||
*/
|
||||
private ExtendService $service;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function process(
|
||||
ServerRequestInterface $request,
|
||||
RequestHandlerInterface $handler
|
||||
): ResponseInterface {
|
||||
$this->service->parse($request);
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
23
src/Utils/Service/Base64Wrapper.php
Normal file
23
src/Utils/Service/Base64Wrapper.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Singularity\HDK\Utils\Service;
|
||||
|
||||
/**
|
||||
* Base64处理器
|
||||
*/
|
||||
class Base64Wrapper
|
||||
{
|
||||
public function encode(string $base64String): string
|
||||
{
|
||||
$base64String = strtr($base64String, '==', '');
|
||||
$base64String = trim($base64String, '=');
|
||||
$base64String = strtr($base64String, '+', '-');
|
||||
return strtr($base64String, '/', '_');
|
||||
}
|
||||
|
||||
public function decode(string $decoder): string
|
||||
{
|
||||
$origin_string = strtr($decoder, '_', '/');
|
||||
return strtr($origin_string, '-', '+');
|
||||
}
|
||||
}
|
||||
51
src/Utils/Service/ExtendService.php
Normal file
51
src/Utils/Service/ExtendService.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Singularity\HDK\Utils\Service;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* 资源扩展
|
||||
*/
|
||||
class ExtendService
|
||||
{
|
||||
private array $extends = [];
|
||||
|
||||
/**
|
||||
* @param \Psr\Http\Message\ServerRequestInterface $request
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function parse(ServerRequestInterface $request): array
|
||||
{
|
||||
$params = $request->getQueryParams();
|
||||
$extends = $params['extends'] ?? null;
|
||||
if (!empty($extends)) {
|
||||
$extends = explode(',', $extends);
|
||||
$this->extends = array_map('trim', $extends);
|
||||
return $this->extends;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getExtends(): array
|
||||
{
|
||||
return $this->extends;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否传入了此扩展
|
||||
*
|
||||
* @param string $field
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasExtends(string $field): bool
|
||||
{
|
||||
return isset($this->extends[$field]);
|
||||
}
|
||||
}
|
||||
38
src/Utils/Service/HttpRequestService.php
Normal file
38
src/Utils/Service/HttpRequestService.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Singularity\HDK\Utils\Service;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* 发起 Http 请求的类
|
||||
*/
|
||||
class HttpRequestService
|
||||
{
|
||||
public const TIMEOUT = 20;
|
||||
|
||||
private Client $client;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var \Singularity\HDK\Utils\Service\UtilsService
|
||||
*/
|
||||
private UtilsService $utilsService;
|
||||
|
||||
/**
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function get(string $url, array $params = []): ResponseInterface
|
||||
{
|
||||
$url = $this->utilsService->buildUrl($url, $params);
|
||||
|
||||
return $this->getClient()->get($url);
|
||||
}
|
||||
|
||||
public function getClient(): Client
|
||||
{
|
||||
return $this->client ?? new Client(['timeout' => self::TIMEOUT]);
|
||||
}
|
||||
}
|
||||
166
src/Utils/Service/UtilsService.php
Normal file
166
src/Utils/Service/UtilsService.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace Singularity\HDK\Utils\Service;
|
||||
|
||||
use Exception;
|
||||
use Generator;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* 工具类
|
||||
*/
|
||||
class UtilsService
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var \Hyperf\HttpServer\Contract\RequestInterface
|
||||
*/
|
||||
private RequestInterface $request;
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*
|
||||
* @param int $length
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generateSecureCode(int $length = 4): string
|
||||
{
|
||||
$code = '';
|
||||
for ($times = 0; $times < $length; $times++) {
|
||||
$code .= rand(1, 9);
|
||||
}
|
||||
return $code;
|
||||
}
|
||||
|
||||
public function getRealIpAddress(): string
|
||||
{
|
||||
$ip = $this->request->server('remote_addr');
|
||||
if (empty($ip) or in_array(
|
||||
$ip,
|
||||
[
|
||||
'localhost',
|
||||
'127.0.0.1',
|
||||
]
|
||||
)) {
|
||||
$ip = $this->request->header('x-real-ip');
|
||||
}
|
||||
return $ip ?? 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* 在响应头中添加 Link 字段
|
||||
*
|
||||
* @param \Psr\Http\Message\ResponseInterface $response
|
||||
* @param string|null $urlReference
|
||||
* @param string $rel
|
||||
* @param array $params
|
||||
* @param bool $trimQuote
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function extendLinkToHeader(
|
||||
ResponseInterface $response,
|
||||
?string $urlReference,
|
||||
string $rel = '',
|
||||
array $params = [],
|
||||
bool $trimQuote = true
|
||||
): ResponseInterface {
|
||||
if (is_null($urlReference)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$params['rel'] = $rel;
|
||||
if (!$trimQuote) {
|
||||
foreach ($params as &$value) {
|
||||
$value = '"' . trim($value, '"') . '"';
|
||||
}
|
||||
}
|
||||
|
||||
$link_list = [];
|
||||
if ($response->hasHeader('Link')) {
|
||||
$link = $response->getHeaderLine('Link');
|
||||
$link_list = explode(',', $link);
|
||||
$link_list = array_map('trim', $link_list);
|
||||
}
|
||||
array_push($link_list, "<$urlReference>; " . http_build_query($params, '', '; '));
|
||||
|
||||
return $response->withHeader(
|
||||
'Link',
|
||||
join(',', $link_list)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 URL
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $params
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function buildUrl(string $url, array $params = []): string
|
||||
{
|
||||
$url_info = parse_url($url);
|
||||
$base_url = str_replace(('?' . $url_info['query'] ?? ''), '', $url);
|
||||
parse_str($url_info['query'], $origin_params);
|
||||
$origin_params += $params;
|
||||
|
||||
return $base_url . '?' . http_build_query($origin_params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以迭代器的形式响应
|
||||
*
|
||||
* @param string $fullName
|
||||
*
|
||||
* @return \Generator
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function csvReaderGenerator(string $fullName): Generator
|
||||
{
|
||||
if (!is_file($fullName)) {
|
||||
throw new Exception('指定的 csv 文件不存在');
|
||||
}
|
||||
|
||||
$file = fopen($fullName, 'r');
|
||||
while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
|
||||
yield $data;
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以数组形式响应
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function csvReader(string $fullName, array $headers = [], ?int $batchNumber = null): array
|
||||
{
|
||||
if (!is_file($fullName)) {
|
||||
throw new Exception($fullName . ' 不存在');
|
||||
}
|
||||
|
||||
$file = fopen($fullName, 'r');
|
||||
$result = [];
|
||||
$row_number = 0;
|
||||
while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
|
||||
$row_number++;
|
||||
$data = empty($headers) ? $data : array_combine($headers, $data);
|
||||
$data = array_map(function ($row) {
|
||||
return is_string($row) ? trim($row) : $row;
|
||||
}, $data);
|
||||
if (!is_null($batchNumber)) {
|
||||
$result[intval($row_number / $batchNumber)][] = $data;
|
||||
} else {
|
||||
$result[] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user