Compare commits

..

13 Commits

Author SHA1 Message Date
ch4o5
73401d7cb1 chore(release): 0.2.6 2023-01-16 07:15:52 +00:00
李东云
d1bae3e623 fix(configure): 修复配置文件和实际应用的字段名不统一 2023-01-16 10:52:00 +08:00
李东云
3d092074b3 style: 移除冗余的注释 2023-01-14 05:24:10 +08:00
ch4o5
427ba4b322 chore(release): 0.2.5 2023-01-13 20:56:46 +00:00
李东云
aeddb55988 feat(CoreMiddleware): 增加restful场景下的分页响应处理
优化弃用代码
2023-01-14 04:56:34 +08:00
ch4o5
0705d1bef3 chore(release): 0.2.4 2023-01-13 20:20:00 +00:00
李东云
922c780c49 fix(oss): 修复了oss的配置缺失和错误 2023-01-14 04:19:46 +08:00
李东云
cdb193778f revert(constant): 移除冗余的http头authentication的枚举类型 2023-01-14 04:18:48 +08:00
ch4o5
9633fa6b8d chore(release): 0.2.3 2023-01-13 18:54:16 +00:00
李东云
08c1f6609c feat(constant): 增加http头authentication的枚举类型 2023-01-14 02:52:52 +08:00
李东云
3a1f2695fe docs(link): 增加错误码对应的语雀文档 2023-01-14 02:52:10 +08:00
ch4o5
50885c57e2 chore(release): 0.2.2 2023-01-13 18:17:21 +00:00
李东云
9f9ff31a83 perf(exceptions.handler): 优化trace显示格式 2023-01-14 02:16:37 +08:00
11 changed files with 188 additions and 40 deletions

View File

@@ -1,4 +1,54 @@
# 版本更新日志
### [0.2.6](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.5...v0.2.6) (2023-01-16)
### 💄 Styles | 风格
* 移除冗余的注释 ([3d09207](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/3d092074b3784b120cabb1561a9d6b9af5c3c428))
### 🐛 Bug Fixes | Bug 修复
* **configure:** 修复配置文件和实际应用的字段名不统一 ([d1bae3e](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/d1bae3e623dde3839acb44d4a7905c7dab78e8ce))
### [0.2.5](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.4...v0.2.5) (2023-01-13)
### ✨ Features | 新功能
* **CoreMiddleware:** 增加restful场景下的分页响应处理 ([aeddb55](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/aeddb55988e714e53b7458ead8668dee6ebec63e))
### [0.2.4](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.3...v0.2.4) (2023-01-13)
### ⏪ Revert | 回退
* **constant:** 移除冗余的http头authentication的枚举类型 ([cdb1937](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/cdb193778fb888851682bb1671bc55dd8f0e3423))
### 🐛 Bug Fixes | Bug 修复
* **oss:** 修复了oss的配置缺失和错误 ([922c780](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/922c780c49edc2cddbce3e66e6b9dfde7be364f8))
### [0.2.3](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.2...v0.2.3) (2023-01-13)
### ✏️ Documentation | 文档
* **link:** 增加错误码对应的语雀文档 ([3a1f269](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/3a1f2695fef03840d9afe3578f3e7ed33da45926))
### ✨ Features | 新功能
* **constant:** 增加http头authentication的枚举类型 ([08c1f66](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/08c1f6609c3035dd7d7f39e8389b66a8ba2e34a5))
### [0.2.2](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.1...v0.2.2) (2023-01-13)
### ⚡ Performance Improvements | 性能优化
* **exceptions.handler:** 优化trace显示格式 ([9f9ff31](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/9f9ff31a83ea5662500c688471b78f02ff67eb15))
### 0.2.1 (2023-01-13)

View File

@@ -1 +1 @@
0.2.1
0.2.6

View File

@@ -107,5 +107,5 @@
"url": "https://repo.huaweicloud.com/repository/php/"
}
},
"version": "0.2.1"
"version": "0.2.6"
}

View File

@@ -68,7 +68,7 @@ return [
'aliyun' => [
'access_key_id' => env('ACCESS_KEY_ID', ''),
'access_key_secret' => env('ACCESS_KEY_SECRET', ''),
'sign' => '', // 短信签名名称
'sign_name' => '', // 短信签名名称
'template_code' => '', // 短信模板CODE
],
],
@@ -76,6 +76,8 @@ return [
'oss' => [
'access_key_id' => env('ACCESS_KEY_ID', ''),
'access_key_secret' => env('ACCESS_KEY_SECRET', ''),
'oss_host' => env('OSS_HOST', ''),
'oss_callback' => env('OSS_CALLBACK'), // 可选
],
],
],

View File

@@ -19,6 +19,8 @@ use Hyperf\Constants\Annotation\Constants;
* @author 李东云<dongyun.li@luxcreo.cn>
* Powered by PhpStorm
* Created on 2022/4/25
*
* @see https://lux-software.yuque.com/htnx76/vcm2oc/pkzgpv
*/
#[Constants]
class CommonErrorCode extends AbstractConstants

View File

@@ -42,24 +42,16 @@ abstract class AbstractController
use DontSerialise;
use DontDeserialise;
use DontToString;
/**
* @var ContainerInterface
*/
#[Inject]
protected ContainerInterface $container;
/**
* @var RequestInterface
*/
#[Inject]
protected RequestInterface $request;
/**
* @var ResponseInterface
*/
#[Inject]
protected ResponseInterface $response;
/**
* @var \Hyperf\Contract\StdoutLoggerInterface
*/
#[Inject]
protected StdoutLoggerInterface $stdoutLogger;
}

View File

@@ -153,7 +153,7 @@ class CommonHandler extends ExceptionHandler
}
$response = $response->withHeader(Header::CONTENT_TYPE, 'application/json; charset=utf-8');
if ($is_debug && $is_testing) {
$data['trace'] = ['errorType' => $error_type, 'errorTrack' => $throwable->getTrace()];
$data += ['errorType' => $error_type, 'errorTrack' => $throwable->getTrace()];
}
$cookies = json_encode($this->request->getCookieParams(), JSON_UNESCAPED_UNICODE);
$this->logger->error(

View File

@@ -10,13 +10,13 @@
namespace Singularity\HDK\Core\Middleware;
use Hyperf\Contract\Arrayable;
use Hyperf\Contract\Jsonable;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\CoreMiddleware;
use Hyperf\Utils\Codec\Json;
use Hyperf\Utils\Contracts\Arrayable;
use Hyperf\Utils\Contracts\Jsonable;
use Lmc\HttpConstants\Header;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@@ -31,15 +31,14 @@ use Singularity\HDK\Core\Service\UtilsService;
*/
class ClassicCoreMiddleware extends CoreMiddleware
{
/**
* @var \Singularity\HDK\Core\Service\UtilsService
*/
#[Inject]
private UtilsService $utilsService;
/**
* Transfer the non-standard response content to a standard response object.
*
* @param null|array<string, mixed>|Arrayable|Jsonable|string $response
* @template TKey of array-key
* @template TValue
* @param null|array<TKey, TValue>|Arrayable<TKey, TValue>|Jsonable|string $response
*/
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
{
@@ -49,24 +48,60 @@ class ClassicCoreMiddleware extends CoreMiddleware
// 分页数据
if ($response instanceof LengthAwarePaginatorInterface) {
$paginator = $response;
$fact_response = $this->response()->withHeader('Per-Page', (string) $paginator->perPage())->withHeader('Total', (string) $paginator->total())->withHeader('Current-Page', (string) $paginator->currentPage())->withHeader('Total-Pages', (string) $paginator->hasPages());
$fact_response = $this->response()->withHeader('Per-Page', (string)$paginator->perPage())->withHeader(
'Total',
(string)$paginator->total()
)->withHeader('Current-Page', (string)$paginator->currentPage())->withHeader(
'Total-Pages',
(string)$paginator->hasPages()
);
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->nextPageUrl(), 'next');
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->url($paginator->lastPage()), 'last');
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
$paginator->url($paginator->lastPage()),
'last'
);
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->url(1), 'first');
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->previousPageUrl(), 'prev');
return $fact_response->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(new SwooleStream(Json::encode([$code_name => 200, $message_name => 'ok', $data_name => $response->items(), 'meta' => ['currentPage' => $paginator->currentPage(), 'lastPage' => $paginator->lastPage(), 'perPage' => $paginator->perPage(), 'total' => $paginator->total()]])));
}
// 可 Json 化的数据结构
if ($response instanceof Jsonable && $response instanceof Arrayable) {
$response = [$code_name => 200, $message_name => 'ok', $data_name => $response->toArray()];
return $this->response()->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(new SwooleStream(Json::encode($response)));
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
$paginator->previousPageUrl(),
'prev'
);
return $fact_response->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(
new SwooleStream(
Json::encode(
[
$code_name => 200,
$message_name => 'ok',
$data_name => $response->items(),
'meta' => [
'currentPage' => $paginator->currentPage(),
'lastPage' => $paginator->lastPage(),
'perPage' => $paginator->perPage(),
'total' => $paginator->total(),
],
]
)
)
);
}
// 普通数组
if (is_array($response) || $response instanceof Arrayable) {
$response = [$code_name => 200, $message_name => 'ok', $data_name => $response];
return $this->response()->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(new SwooleStream(Json::encode($response)));
return $this->response()->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(
new SwooleStream(Json::encode($response))
);
}
// 可 Json 化的数据结构
if ($response instanceof Jsonable) {
$response = [$code_name => 200, $message_name => 'ok', $data_name => Json::decode((string)$response)];
return $this->response()->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(
new SwooleStream(Json::encode($response))
);
}
// 其他默认按字符串处理
return $this->response()->withAddedHeader(Header::CONTENT_TYPE, 'text/plain')->withBody(new SwooleStream((string) $response));
return $this->response()->withAddedHeader(Header::CONTENT_TYPE, 'text/plain')->withBody(
new SwooleStream((string)$response)
);
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* RestfulCoreMiddleware.php@HDK-Core
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2023/1/14
*/
namespace Singularity\HDK\Core\Middleware;
use Hyperf\Contract\Arrayable;
use Hyperf\Contract\Jsonable;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\CoreMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Singularity\HDK\Core\Service\UtilsService;
/**
* Singularity\HDK\Core\Middleware\RestfulCoreMiddleware@HDK-Core
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2023/1/14
*/
class RestfulCoreMiddleware extends CoreMiddleware
{
#[Inject]
private UtilsService $utilsService;
/**
* @template TKey of array-key
* @template TValue
* @param null|array<TKey, TValue>|Arrayable<TKey, TValue>|Jsonable|string $response
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
{
// 分页数据
if ($response instanceof LengthAwarePaginatorInterface) {
$fact_response = $this->response()
->withHeader('Per-Page', (string)$response->perPage())
->withHeader('Total', (string)$response->total())
->withHeader('Current-Page', (string)$response->currentPage())
->withHeader('Total-Pages', (string)$response->hasPages());
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->nextPageUrl(), 'next');
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
$response->url($response->lastPage()),
'last'
);
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->url(1), 'first');
return $this->utilsService->extendLinkToHeader(
$fact_response,
$response->previousPageUrl(),
'prev'
);
}
return parent::transferToResponse($response, $request);
}
}

View File

@@ -29,7 +29,7 @@ class OssService
$this->accessKeyId ??= config('common.third_party.storage.oss.access_key_id');
$this->accessKeySecret ??= config('common.third_party.storage.oss.access_key_secret');
$this->host ??= config('common.third_party.storage.oss.oss_host');
$this->callbackUrl ??= config('oss_callback');
$this->callbackUrl ??= config('common.third_party.storage.oss.oss_callback') ?? null;
}
/**
@@ -84,7 +84,7 @@ class OssService
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true));
$callback = Json::encode([
$callback = isset($this->callbackUrl) ? Json::encode([
'callbackUrl' => $this->callbackUrl,
'callbackBody' => $isImage ? <<<'callbackBody'
{
@@ -115,17 +115,20 @@ callbackBody : <<<'callbackBody'
}
callbackBody,
'callbackBodyType' => 'application/json',
]);
]) : null;
return [
$result = [
'accessid' => $this->accessKeyId,
'host' => $this->host,
'policy' => $base64_policy,
'signature' => $signature,
'expire' => $expire_time,
'dir' => $dir,
'callback' => base64_encode($callback),
];
if (isset($callback)) {
$result += ['callback' => base64_encode($callback),];
}
return $result;
}
/**

View File

@@ -25,7 +25,6 @@ class UtilsService
* @return string
* @throws Exception
*/
#[Pure]
public function generateSecureCode(int $length = 4): string
{
$code = '';