mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 07:15:06 +08:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf0dc896e4 | ||
|
|
a729270fac | ||
|
|
099ab60103 | ||
|
|
e41847a234 | ||
|
|
9026a3e0f0 | ||
|
|
135db1bf6a | ||
|
|
5151787982 | ||
|
|
b65818bfb6 | ||
|
|
1dbad8e5a6 | ||
|
|
5a1a2a89bd | ||
|
|
8757908f3f | ||
|
|
73401d7cb1 | ||
|
|
d1bae3e623 | ||
|
|
3d092074b3 | ||
|
|
427ba4b322 | ||
|
|
aeddb55988 | ||
|
|
0705d1bef3 | ||
|
|
922c780c49 | ||
|
|
cdb193778f | ||
|
|
9633fa6b8d | ||
|
|
08c1f6609c | ||
|
|
3a1f2695fe | ||
|
|
50885c57e2 | ||
|
|
9f9ff31a83 |
1
.idea/inspectionProfiles/Project_Default.xml
generated
1
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -2,6 +2,7 @@
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PhpCSFixerValidationInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="PhpDocSignatureInspection" enabled="true" level="INFORMATION" enabled_by_default="true" />
|
||||
<inspection_tool class="PhpPropertyOnlyWrittenInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="PhpStanGlobal" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
|
||||
85
CHANGELOG.md
85
CHANGELOG.md
@@ -1,4 +1,89 @@
|
||||
# 版本更新日志
|
||||
### [0.2.7](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.6...v0.2.7) (2023-02-02)
|
||||
|
||||
|
||||
### ⚡ Performance Improvements | 性能优化
|
||||
|
||||
* **Sms:** 修改抛出的异常类型,追加方法注释 ([5a1a2a8](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/5a1a2a89bd7d706a6e8d1ef567c59969d8d63554))
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
|
||||
* **Email:** 实现了邮件发送的事件机制 ([8757908](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/8757908f3f8a6dff5516bd6e449c8dd3952ec2cd))
|
||||
* **Listener:** 添加了一个抽象监听器类,预先注入了一些属性 ([1dbad8e](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/1dbad8e5a65d299b9a56b6a5c4759be5c3736707))
|
||||
* **Sms:** 实现了短信发送的事件机制 ([5151787](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/51517879820de832395efc40bb308a736f33f131))
|
||||
|
||||
|
||||
### ✏️ Documentation | 文档
|
||||
|
||||
* **README:** 修复一直报错的文件名 ([135db1b](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/135db1bf6aeeedbb0d59405567bb4837f3221e49))
|
||||
* **Sms:** 补充所抛出异常的注释 ([b65818b](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/b65818bfb6d8368514d517c31dd6b6696b2d08be))
|
||||
|
||||
|
||||
### ✅ Tests | 测试
|
||||
|
||||
* **extend:** 补充测试用例 ([e41847a](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/e41847a2340916de24726f8266dca8b514b4edd3))
|
||||
|
||||
|
||||
### 🐛 Bug Fixes | Bug 修复
|
||||
|
||||
* **extend:** 修复了解析不到为空不为数组的问题 ([099ab60](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/099ab601030caef52806f5b9ec4d62fb27b08e72))
|
||||
|
||||
|
||||
### 💄 Styles | 风格
|
||||
|
||||
* **cs-fixer:** prettier ([a729270](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/a729270fac57cd617e8f166bb59f9ccffe33358b))
|
||||
|
||||
### [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)
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ After cloning this repository, change into the newly created directory and run
|
||||
composer config secure-http false \
|
||||
&& composer config repo.lux-map composer https://satis.luxcreo.cn/ \
|
||||
&& composer require singularity/hdk-core \
|
||||
&& php bin/hyperf vendor:publish singularity/hdk-core
|
||||
&& php bin/hyperf.php vendor:publish singularity/hdk-core
|
||||
```
|
||||
|
||||
This will install all dependencies needed for the project.
|
||||
|
||||
@@ -1 +1 @@
|
||||
0.2.1
|
||||
0.2.7
|
||||
@@ -63,7 +63,10 @@
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true,
|
||||
"secure-http": false
|
||||
"secure-http": false,
|
||||
"allow-plugins": {
|
||||
"pestphp/pest-plugin": true
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@@ -107,5 +110,5 @@
|
||||
"url": "https://repo.huaweicloud.com/repository/php/"
|
||||
}
|
||||
},
|
||||
"version": "0.2.1"
|
||||
"version": "0.2.7"
|
||||
}
|
||||
|
||||
76
docs/Email.md
Normal file
76
docs/Email.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# Email | 邮件
|
||||
提供一套通用 Service 用于定制需求,和一套现成的事件/监听器用于异步发送。
|
||||
|
||||
## 服务类
|
||||
[\Singularity\HDK\Core\Service\EmailService](../src/Service/EmailService.php)
|
||||
* 发送纯文本邮件的 `\Singularity\HDK\Core\Service\EmailService::sendText()`
|
||||
* 发送 Html 富文本邮件的 `\Singularity\HDK\Core\Service\EmailService::sendHtml()`
|
||||
|
||||
## 事件机制
|
||||
> 这种方式无法监听邮件发送的结果!
|
||||
>
|
||||
> 如果你需要根据邮件发送的结果进行处理,
|
||||
> 请使用服务类进行自定义
|
||||
|
||||
更好的方法可能是在事件发生后触发异步队列,
|
||||
但如果将异步队列封装在本包中,
|
||||
相当于强制引用本库的项目使用异步队列机制,
|
||||
这是我们不愿意看到的。
|
||||
所以这里的监听器只是集成了同步发送的操作。
|
||||
|
||||
如果你想要定义更好的设计,
|
||||
你可以直接使用提供的 Service 类进行定制。
|
||||
> 或许这里还有优化空间😄
|
||||
|
||||
### 事件
|
||||
[\Singularity\HDK\Core\Events\EmailWillSent](../src/Events/EmailWillSent.php)
|
||||
### 监听器
|
||||
[\Singularity\HDK\Core\Listener\EmailWillSentListener](../src/Listener/EmailWillSentListener.php)
|
||||
### 使用方法
|
||||
监听器已经通过 [ConfigProvider](../src/ConfigProvider.php) 机制注册,
|
||||
所以你只需要在要发送邮件的地方调度这个事件即可。
|
||||
> 这也就意味着,你无法调用这个事件,但不触发这个监听器。
|
||||
> 如果你有这样做的必要,请创建一个自己的事件。
|
||||
> 这也是为了保证功能的完整性和原子性。
|
||||
|
||||
具体使用方法,如下代码所示:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace App\Service;
|
||||
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use App\Event\UserRegistered;use Singularity\HDK\Core\Events\EmailWillSent;
|
||||
|
||||
class UserService
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $eventDispatcher;
|
||||
|
||||
public function register()
|
||||
{
|
||||
// 我们假设存在 User 这个实体
|
||||
$user = new User();
|
||||
$result = $user->save();
|
||||
|
||||
// ↓
|
||||
// 这里 dispatch(object $event) 会逐个运行监听该事件的监听器
|
||||
$this->eventDispatcher->dispatch(new EmailWillSent(
|
||||
target: $user->mobile,
|
||||
subject: '注册成功通知',
|
||||
type: 'html', // 默认 text 可以不传
|
||||
content: <<<HTML
|
||||
<h1>恭喜你,注册成功!</h1>
|
||||
HTML,
|
||||
));
|
||||
// ↑
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
75
docs/Sms.md
Normal file
75
docs/Sms.md
Normal file
@@ -0,0 +1,75 @@
|
||||
# SMS | 短信
|
||||
提供一套通用 Service 用于定制需求,和一套现成的事件/监听器用于异步发送。
|
||||
|
||||
## 服务类
|
||||
[\Singularity\HDK\Core\Service\SmsService](../src/Service/SmsService.php)
|
||||
* 发送国内短信的 `\Singularity\HDK\Core\Service\SmsService::sendSmsCountryside()`
|
||||
|
||||
## 事件机制
|
||||
> 这种方式无法监听短信发送的结果!
|
||||
>
|
||||
> 如果你需要根据短信发送的结果进行处理,
|
||||
> 请使用服务类进行自定义
|
||||
|
||||
更好的方法可能是在事件发生后触发异步队列,
|
||||
但如果将异步队列封装在本包中,
|
||||
相当于强制引用本库的项目使用异步队列机制,
|
||||
这是我们不愿意看到的。
|
||||
所以这里的监听器只是集成了同步发送的操作。
|
||||
|
||||
如果你想要定义更好的设计,
|
||||
你可以直接使用提供的 Service 类进行定制。
|
||||
> 或许这里还有优化空间😄
|
||||
|
||||
### 事件
|
||||
[\Singularity\HDK\Core\Events\SmsWillSent](../src/Events/SmsWillSent.php)
|
||||
### 监听器
|
||||
[\Singularity\HDK\Core\Listener\SmsWillSentListener](../src/Listener/SmsWillSentListener.php)
|
||||
### 使用方法
|
||||
监听器已经通过 [ConfigProvider](../src/ConfigProvider.php) 机制注册,
|
||||
所以你只需要在要发送短信的地方调度这个事件即可。
|
||||
> 这也就意味着,你无法调用这个事件,但不触发这个监听器。
|
||||
> 如果你有这样做的必要,请创建一个自己的事件。
|
||||
> 这也是为了保证功能的完整性和原子性。
|
||||
|
||||
具体使用方法,如下代码所示:
|
||||
|
||||
```php
|
||||
<?php
|
||||
namespace App\Service;
|
||||
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use App\Event\UserRegistered;use Singularity\HDK\Core\Events\EmailWillSent;
|
||||
|
||||
class UserService
|
||||
{
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $eventDispatcher;
|
||||
|
||||
public function register()
|
||||
{
|
||||
// 我们假设存在 User 这个实体
|
||||
$user = new User();
|
||||
$result = $user->save();
|
||||
|
||||
// ↓
|
||||
// 这里 dispatch(object $event) 会逐个运行监听该事件的监听器
|
||||
$this->eventDispatcher->dispatch(new EmailWillSent(
|
||||
target: $user->mobile,
|
||||
subject: '注册成功通知',
|
||||
type: 'html', // 默认 text 可以不传
|
||||
content: <<<HTML
|
||||
<h1>恭喜你,注册成功!</h1>
|
||||
HTML,
|
||||
));
|
||||
// ↑
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
@@ -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'), // 可选
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace Singularity\HDK\Core;
|
||||
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Framework\Logger\StdoutLogger;
|
||||
use Singularity\HDK\Core\Listener\EmailWillSentListener;
|
||||
use Singularity\HDK\Core\Listener\SmsWillSentListener;
|
||||
|
||||
class ConfigProvider
|
||||
{
|
||||
@@ -38,7 +40,10 @@ class ConfigProvider
|
||||
'commands' => [
|
||||
],
|
||||
// 与 commands 类似
|
||||
'listeners' => [],
|
||||
'listeners' => [
|
||||
EmailWillSentListener::class,
|
||||
SmsWillSentListener::class
|
||||
],
|
||||
// 组件默认配置文件,即执行命令后会把 source 的对应的文件复制为 destination 对应的的文件
|
||||
'publish' => [
|
||||
[
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
50
src/Events/EmailWillSent.php
Normal file
50
src/Events/EmailWillSent.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* EmailWillSent.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Events;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Events\EmailWillSent@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*
|
||||
* @link ../../docs/Email.md
|
||||
*/
|
||||
class EmailWillSent
|
||||
{
|
||||
public function __construct(
|
||||
/**
|
||||
* @var string|string[] $target
|
||||
*/
|
||||
public string|array $target,
|
||||
|
||||
/**
|
||||
* @var non-empty-string $subject
|
||||
*/
|
||||
public string $subject,
|
||||
|
||||
/**
|
||||
* @var non-empty-string $content
|
||||
*/
|
||||
public string $content,
|
||||
|
||||
/**
|
||||
* @var string[] $cc
|
||||
*/
|
||||
public array $cc = [],
|
||||
|
||||
/**
|
||||
* @var 'text'|'html' $type
|
||||
*/
|
||||
public string $type = 'text'
|
||||
) {
|
||||
}
|
||||
}
|
||||
48
src/Events/SmsWillSent.php
Normal file
48
src/Events/SmsWillSent.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* SmsWillSent.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Events;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Events\SmsWillSent@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*
|
||||
* @link ../../docs/SMS.md
|
||||
*/
|
||||
class SmsWillSent
|
||||
{
|
||||
public function __construct(
|
||||
/**
|
||||
* @var non-empty-string $phone 目标手机号
|
||||
* @example +8613700000001
|
||||
* @example 8613700000001
|
||||
* @example 13700000001
|
||||
*/
|
||||
public string $phone,
|
||||
|
||||
/**
|
||||
* @var array<literal-string, literal-string|int>|null $templateParam 模板变量
|
||||
*/
|
||||
public ?array $templateParam = null,
|
||||
|
||||
/**
|
||||
* @var literal-string|null $templateCode 模板代码
|
||||
*/
|
||||
public ?string $templateCode = null,
|
||||
|
||||
/**
|
||||
* @var literal-string|null $signName 模板签名
|
||||
*/
|
||||
public ?string $signName = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
34
src/Listener/AbstractListener.php
Normal file
34
src/Listener/AbstractListener.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* AbstractListener.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Listener;
|
||||
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
abstract class AbstractListener implements ListenerInterface
|
||||
{
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
protected StdoutLoggerInterface $stdoutLogger;
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->stdoutLogger = $this->container->get(StdoutLoggerInterface::class);
|
||||
}
|
||||
}
|
||||
94
src/Listener/EmailWillSentListener.php
Normal file
94
src/Listener/EmailWillSentListener.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* EmailWillSentListener.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Listener;
|
||||
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Singularity\HDK\Core\Constants\CommonErrorCode;
|
||||
use Singularity\HDK\Core\Events\EmailWillSent;
|
||||
use Singularity\HDK\Core\Service\EmailService;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Listener\EmailWillSentListener@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*
|
||||
* @link ../../docs/Email.md
|
||||
*/
|
||||
class EmailWillSentListener implements ListenerInterface
|
||||
{
|
||||
#[Inject]
|
||||
private ContainerInterface $container;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
EmailWillSent::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EmailWillSent $event
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
#[NoReturn]
|
||||
public function process(object $event): void
|
||||
{
|
||||
$stdoutLogger = $this->container->get(StdoutLoggerInterface::class);
|
||||
$emailService = $this->container->get(EmailService::class);
|
||||
try {
|
||||
$event->type === 'html'
|
||||
? $emailService->sendHtml(
|
||||
target: $event->target,
|
||||
subject: $event->subject,
|
||||
html: $event->content,
|
||||
cc: $event->cc
|
||||
)
|
||||
: $emailService->sendText(
|
||||
target: $event->target,
|
||||
subject: $event->subject,
|
||||
text: $event->content,
|
||||
cc: $event->cc
|
||||
);
|
||||
|
||||
$stdoutLogger->info('邮件发送成功!');
|
||||
$stdoutLogger->info("To: $event->target");
|
||||
$stdoutLogger->info("Subject: $event->subject");
|
||||
$stdoutLogger->debug('Content: ');
|
||||
$stdoutLogger->debug($event->content);
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$code = CommonErrorCode::SERVER_MESSAGE_EMAIL_ERROR;
|
||||
$msg = $e->getMessage();
|
||||
if (strpos($msg, '500 Error: bad syntax')) {
|
||||
$code = CommonErrorCode::SERVER_MESSAGE_EMAIL_NOT_FOUND;
|
||||
$msg = CommonErrorCode::getMessage($code);
|
||||
}
|
||||
$stdoutLogger->alert('邮件发送失败!');
|
||||
$stdoutLogger->error(
|
||||
<<<ERROR_LOG
|
||||
[$code] $msg
|
||||
ERROR_LOG
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
src/Listener/SmsWillSentListener.php
Normal file
60
src/Listener/SmsWillSentListener.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* SmsWillSentListener.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Listener;
|
||||
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Singularity\HDK\Core\Events\SmsWillSent;
|
||||
use Singularity\HDK\Core\Service\SmsService;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Listener\SmsWillSentListener@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/1/16
|
||||
*/
|
||||
class SmsWillSentListener extends AbstractListener
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
SmsWillSent::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SmsWillSent $event
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function process(object $event): void
|
||||
{
|
||||
$smsService = $this->container->get(SmsService::class);
|
||||
try {
|
||||
$smsService->sendSmsCountryside(
|
||||
phone: $event->phone,
|
||||
templateCode: $event->templateCode,
|
||||
templateParam: $event->templateParam,
|
||||
signName: $event->signName
|
||||
);
|
||||
} catch (Throwable $throwable) {
|
||||
$code = $throwable->getCode();
|
||||
$message = $throwable->getMessage();
|
||||
$this->stdoutLogger->alert('短信发送失败!');
|
||||
$this->stdoutLogger->error(sprintf("[%u] %s", $code, $message));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
65
src/Middleware/RestfulCoreMiddleware.php
Normal file
65
src/Middleware/RestfulCoreMiddleware.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,9 @@ class ExtendService
|
||||
*/
|
||||
public function parse(
|
||||
?ServerRequestInterface $request,
|
||||
?array $params = null
|
||||
array|string|null $params = null
|
||||
): array {
|
||||
$params ??= $request->getQueryParams();
|
||||
$params ??= $request?->getQueryParams();
|
||||
$extends = $params['extends'] ?? null;
|
||||
if (!empty($extends)) {
|
||||
$extends = explode(',', $extends);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,10 +6,11 @@ use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
|
||||
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
|
||||
use AlibabaCloud\Tea\Exception\TeaError;
|
||||
use Darabonba\OpenApi\Models\Config;
|
||||
use Hyperf\HttpMessage\Exception\HttpException;
|
||||
use Hyperf\HttpMessage\Exception\ServerErrorHttpException;
|
||||
use Hyperf\Utils\Codec\Json;
|
||||
use Singularity\HDK\Core\Constants\CommonErrorCode;
|
||||
use Singularity\HDK\Core\Exceptions\ValidateException;
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* 短信服务
|
||||
@@ -17,7 +18,7 @@ use Singularity\HDK\Core\Exceptions\ValidateException;
|
||||
class SmsService
|
||||
{
|
||||
/**
|
||||
* @var \AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi
|
||||
* @var Dysmsapi
|
||||
*/
|
||||
private Dysmsapi $client;
|
||||
|
||||
@@ -29,7 +30,7 @@ class SmsService
|
||||
/**
|
||||
* 初始化实例
|
||||
*
|
||||
* @return \AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi
|
||||
* @return Dysmsapi
|
||||
*/
|
||||
private function createClient(): Dysmsapi
|
||||
{
|
||||
@@ -44,13 +45,17 @@ class SmsService
|
||||
/**
|
||||
* 国内短信
|
||||
*
|
||||
* @param string $phone 接收短信的手机号码
|
||||
* @param string|null $templateCode 短信模板CODE
|
||||
* @param array<string, string>|null $templateParam 短信模板变量对应的实际值(不支持空数组)
|
||||
* @param string|null $signName 短信签名名称
|
||||
* @param string $phone 接收短信的手机号码
|
||||
* @param string|null $templateCode 短信模板CODE
|
||||
* @param array<string, string>|null $templateParam 短信模板变量对应的实际值(不支持空数组)
|
||||
* @param string|null $signName 短信签名名称
|
||||
*
|
||||
* @return bool
|
||||
* @throws UnexpectedValueException 参数校验
|
||||
* @throws ValidateException 频率太高
|
||||
* @throws ServerErrorHttpException 阿里云短信服务报错
|
||||
* @link https://help.aliyun.com/document_detail/419273.htm
|
||||
*
|
||||
*/
|
||||
public function sendSmsCountryside(
|
||||
string $phone,
|
||||
@@ -59,7 +64,7 @@ class SmsService
|
||||
?string $signName = null,
|
||||
): bool {
|
||||
if (is_array($templateParam) && count($templateParam) <= 0) {
|
||||
throw new \UnexpectedValueException('不支持空数组,请用 null 代替或不传', CommonErrorCode::FORMATTER_ERROR);
|
||||
throw new UnexpectedValueException('不支持空数组,请用 null 代替或不传', CommonErrorCode::FORMATTER_ERROR);
|
||||
}
|
||||
$phone_number = strtr($phone, ['#' => '']);
|
||||
|
||||
@@ -101,11 +106,10 @@ class SmsService
|
||||
}
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
500,
|
||||
$message,
|
||||
CommonErrorCode::SERVER_DEPENDENCY_SMS_ERROR,
|
||||
$error
|
||||
throw new ServerErrorHttpException(
|
||||
message: $message,
|
||||
code: CommonErrorCode::SERVER_DEPENDENCY_SMS_ERROR,
|
||||
previous: $error
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ class UtilsService
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Pure]
|
||||
public function generateSecureCode(int $length = 4): string
|
||||
{
|
||||
$code = '';
|
||||
|
||||
@@ -9,10 +9,26 @@
|
||||
|
||||
namespace Singularity\HDK\Test\Core\Unit;
|
||||
|
||||
use Darabonba\GatewaySpi\Models\InterceptorContext\request;
|
||||
use Singularity\HDK\Core\Service\ExtendService;
|
||||
|
||||
$service = new ExtendService();
|
||||
|
||||
it('asserts no parameters can be parsed.', function (ExtendService $service, $params) {
|
||||
$service->parse(
|
||||
request: null,
|
||||
params: $params
|
||||
);
|
||||
$result = $service->getExtends();
|
||||
expect($result)->toBeArray()->toHaveCount(0)->toBe([]);
|
||||
})->with([
|
||||
[$service, null],
|
||||
[$service, ''],
|
||||
[$service, []],
|
||||
[$service, ['extends' => '']],
|
||||
[$service, ['extends' => null]],
|
||||
]);
|
||||
|
||||
it('asserts query parameters can be parsed.', function () use ($service) {
|
||||
$result = $service->parse(
|
||||
null,
|
||||
|
||||
Reference in New Issue
Block a user