mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 07:15:06 +08:00
Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11315aa109 | ||
|
|
90a521f90a | ||
|
|
db31689f66 | ||
|
|
7e5cb89dfe | ||
|
|
4ba0cb83f5 | ||
|
|
c25e6eed52 | ||
|
|
2ee20984ce | ||
|
|
859a1a1f2a | ||
|
|
78b56a6aa9 | ||
|
|
c991c31260 | ||
|
|
1e4158639f | ||
|
|
70ad3078b1 | ||
|
|
afec642454 | ||
|
|
b2debf9c13 | ||
|
|
d7c902d697 | ||
|
|
de96e288c3 | ||
|
|
7c1efe1a1f | ||
|
|
e33c115938 | ||
|
|
b8c3d6d068 | ||
|
|
2d2148f3ec | ||
|
|
2d2a535fd2 | ||
|
|
a618af5ab3 | ||
|
|
28e0ceed11 | ||
|
|
c8f9b793ee | ||
|
|
bf0dc896e4 | ||
|
|
a729270fac | ||
|
|
099ab60103 | ||
|
|
e41847a234 | ||
|
|
9026a3e0f0 | ||
|
|
135db1bf6a | ||
|
|
5151787982 | ||
|
|
b65818bfb6 | ||
|
|
1dbad8e5a6 | ||
|
|
5a1a2a89bd | ||
|
|
8757908f3f | ||
|
|
73401d7cb1 | ||
|
|
d1bae3e623 | ||
|
|
3d092074b3 | ||
|
|
427ba4b322 | ||
|
|
aeddb55988 | ||
|
|
0705d1bef3 | ||
|
|
922c780c49 | ||
|
|
cdb193778f | ||
|
|
9633fa6b8d | ||
|
|
08c1f6609c | ||
|
|
3a1f2695fe | ||
|
|
50885c57e2 | ||
|
|
9f9ff31a83 | ||
|
|
990c247cf6 | ||
|
|
3989f5913e | ||
|
|
ab44630db8 | ||
|
|
3bc4e275a7 | ||
|
|
82e257f7b4 | ||
|
|
4deffe055e | ||
|
|
480f406ccc | ||
|
|
91db3c731f | ||
|
|
9fedbb40f4 |
22
.idea/HDK-Core.iml
generated
22
.idea/HDK-Core.iml
generated
@@ -64,9 +64,6 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/http-message" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/exception-handler" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/macroable" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/paginator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/engine" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/pool" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/event" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/validation" />
|
||||
@@ -85,7 +82,6 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phar-io/manifest" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phar-io/version" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-invoker" />
|
||||
@@ -135,14 +131,26 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/nunomaduro/collision" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/pestphp/pest" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpstan/phpstan" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/pestphp/pest-plugin" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/facade/ignition-contracts" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/pest-plugin-test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/filp/whoops" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/deprecations" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/engine" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/pest-plugin-test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/coordinator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/engine-contract" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/paginator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/guzzle" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/http-foundation" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/collection" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/db" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/pipeline" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/tappable" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/serializer" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
||||
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>
|
||||
|
||||
11
.idea/php.xml
generated
11
.idea/php.xml
generated
@@ -150,7 +150,6 @@
|
||||
<path value="$PROJECT_DIR$/vendor/pestphp/pest-plugin" />
|
||||
<path value="$PROJECT_DIR$/vendor/filp/whoops" />
|
||||
<path value="$PROJECT_DIR$/vendor/nunomaduro/collision" />
|
||||
<path value="$PROJECT_DIR$/vendor/facade/ignition-contracts" />
|
||||
<path value="$PROJECT_DIR$/vendor/spatie/test-time" />
|
||||
<path value="$PROJECT_DIR$/vendor/spatie/pest-plugin-test-time" />
|
||||
<path value="$PROJECT_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||
@@ -158,6 +157,16 @@
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/stopwatch" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpstan/phpstan" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/deprecations" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/coordinator" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/guzzle" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/engine-contract" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/crontab" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/http-foundation" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/pipeline" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/collection" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/tappable" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/db" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/serializer" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
|
||||
|
||||
68
.versionrc
Normal file
68
.versionrc
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"header": "# 版本更新日志",
|
||||
"preMajor": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "feat",
|
||||
"section": "✨ Features | 新功能"
|
||||
},
|
||||
{
|
||||
"type": "fix",
|
||||
"section": "🐛 Bug Fixes | Bug 修复"
|
||||
},
|
||||
{
|
||||
"type": "init",
|
||||
"section": "🎉 Init | 初始化"
|
||||
},
|
||||
{
|
||||
"type": "docs",
|
||||
"section": "✏️ Documentation | 文档"
|
||||
},
|
||||
{
|
||||
"type": "style",
|
||||
"section": "💄 Styles | 风格"
|
||||
},
|
||||
{
|
||||
"type": "refactor",
|
||||
"section": "♻️ Code Refactoring | 代码重构"
|
||||
},
|
||||
{
|
||||
"type": "perf",
|
||||
"section": "⚡ Performance Improvements | 性能优化"
|
||||
},
|
||||
{
|
||||
"type": "tests",
|
||||
"section": "✅ Tests | 测试"
|
||||
},
|
||||
{
|
||||
"type": "test",
|
||||
"section": "✅ Tests | 测试"
|
||||
},
|
||||
{
|
||||
"type": "revert",
|
||||
"section": "⏪ Revert | 回退"
|
||||
},
|
||||
{
|
||||
"type": "build",
|
||||
"section": "📦 Build System | 打包构建"
|
||||
},
|
||||
{
|
||||
"type": "chore",
|
||||
"section": "🚀 Chore | 构建/工程依赖/工具"
|
||||
},
|
||||
{
|
||||
"type": "ci",
|
||||
"section": "👷 Continuous Integration | CI 配置"
|
||||
}
|
||||
],
|
||||
"bumpFiles": [
|
||||
{
|
||||
"filename": "VERSION_TRACKER.txt",
|
||||
"type": "plain-text"
|
||||
},
|
||||
{
|
||||
"filename": "composer.json",
|
||||
"type": "json"
|
||||
}
|
||||
]
|
||||
}
|
||||
210
CHANGELOG.md
Normal file
210
CHANGELOG.md
Normal file
@@ -0,0 +1,210 @@
|
||||
# 版本更新日志
|
||||
### [0.2.13](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.12...v0.2.13) (2023-04-11)
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
|
||||
* **xml:** 增加了 xml 生成的方法及测试用例 ([90a521f](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/90a521f90acee7fd6efe6f0a554e749c16fadc1c))
|
||||
|
||||
### [0.2.12](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.11...v0.2.12) (2023-04-10)
|
||||
|
||||
|
||||
### 📦 Build System | 打包构建
|
||||
|
||||
* **composer:** 更新依赖以兼容 nano ([7e5cb89](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/7e5cb89dfe1c09390e78cdd527c60151f2cd0418))
|
||||
* **phpstan:** fix ([c25e6ee](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/c25e6eed52e83d2ca62fc3d0cf83f508a1045c5a))
|
||||
* **scripts:** 更新脚本 ([4ba0cb8](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/4ba0cb83f5dc88817260cb2b2dfde491c6b5ba75))
|
||||
|
||||
### [0.2.11](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.10...v0.2.11) (2023-04-03)
|
||||
|
||||
|
||||
### 📦 Build System | 打包构建
|
||||
|
||||
* **composer:** 更新依赖 ([78b56a6](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/78b56a6aa9038743be7df9a17787b94771e226c3))
|
||||
* **composer:** 更新依赖 ([c991c31](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/c991c312609e7501c7adad28ec8229efad717db0))
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
|
||||
* **i18n:** 引入了 symfony 的 Accept-Language 机制 ([859a1a1](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/859a1a1f2aa683f9f026652952d1a03ba5d7be02))
|
||||
|
||||
### [0.2.10](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.9...v0.2.10) (2023-03-21)
|
||||
|
||||
|
||||
### 📦 Build System | 打包构建
|
||||
|
||||
* **composer:** 只留下了腾讯源,更新依赖 ([afec642](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/afec6424545daa3b3f2a4b91e97e359b4a840a19))
|
||||
* **composer:** 只留下了腾讯源,更新依赖 ([b2debf9](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/b2debf9c135554a15639c299f183c43f3682545b))
|
||||
* **composer:** 更新依赖 ([70ad307](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/70ad3078b1ddeeeb0179dd44770e959ecf072fed))
|
||||
|
||||
### [0.2.9](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.8...v0.2.9) (2023-03-15)
|
||||
|
||||
|
||||
### 🐛 Bug Fixes | Bug 修复
|
||||
|
||||
* 修复了一处大小写错误 ([2d2a535](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/2d2a535fd2c1ae13195f13498864540b503edd1e))
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
|
||||
* 增加了inArray、hasExtends方法 ([de96e28](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/de96e288c3429c58deed15ea667a5e15a34fb81f))
|
||||
* 增加了对 session 中间件配置 cookie 方式的补充 ([a618af5](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/a618af5ab353f478dd4bcce18a7b84f611fd0c7a))
|
||||
* 增加默认api风格参数 ([e33c115](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/e33c1159388efa3590e81f484c01a66d21f8b574))
|
||||
* 实现了通过中间件设置是否 restful 的逻辑 ([2d2148f](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/2d2148f3ecabef09801a225632430bf103dbf218))
|
||||
* 添加了 http 请求服务 ([7c1efe1](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/7c1efe1a1f18f082ed7b7b6383112034982e2893))
|
||||
* **api:** 增加了可以使用中间件定制是否 restful 的逻辑 ([b8c3d6d](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/b8c3d6d06810fb920df200a4d75746b49e2fbd07))
|
||||
|
||||
### [0.2.8](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.7...v0.2.8) (2023-02-03)
|
||||
|
||||
|
||||
### 🐛 Bug Fixes | Bug 修复
|
||||
|
||||
* **commonHandler:** 修复了一处兼容问题 ([c8f9b79](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/c8f9b793ee7c2ba4d77a2779b6825211dcf52636))
|
||||
|
||||
### [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)
|
||||
|
||||
|
||||
### 🎉 Init | 初始化
|
||||
|
||||
* **core:** 初始化代码 ([f564d01](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/f564d01766f52fc57f07aa05bd995a5662c2ae45))
|
||||
* 初始化项目 ([a13779a](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/a13779ab3fb617189227b9d2c0462588be8ceee5))
|
||||
|
||||
|
||||
### ✅ Tests | 测试
|
||||
|
||||
* **oss:** 增加 oss 票据生成的测试文件 ([b5970d5](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/b5970d543435495706502c5b67897ff1cfd5fb85))
|
||||
* **service:** 添加了base64Wrapper的测试 ([69ce665](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/69ce66561f19e44d35b134c6d032ff69f48201a3))
|
||||
* **service:** 添加了发送邮件的测试 ([65e3485](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/65e34857e2978a9952ee89df71b1e0c9101ec69c))
|
||||
* **service:** 添加了解析 extends 的测试 ([da1d6c6](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/da1d6c68d21b2739c8bb1701243e6419eae14975))
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
|
||||
* **utils:** 重写了构造URL的方法,增加对应的单元测试 ([9f79d14](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/9f79d1465ba6ed4b4c0d0c30e23ffa34bbeece65))
|
||||
* 迁移到了 Core 命名空间下 ([a8a582f](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/a8a582f3aa0948c982e31bf8938b6f3331d8dc6d))
|
||||
|
||||
|
||||
### ✏️ Documentation | 文档
|
||||
|
||||
* **core:** 添加作者信息 ([096d4e5](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/096d4e56ed30c23de1338bb6f057a91e629c6ea5))
|
||||
* **README:** 增加说明文件 ([4cf16ae](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/4cf16ae0cdcf6f7a1eb99fde95a6c3ef01bd7b2b))
|
||||
|
||||
|
||||
### ⚡ Performance Improvements | 性能优化
|
||||
|
||||
* **utils:** 获取真实IP方法,增加apache的处理 ([47e522c](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/47e522c67bf595282df7671f1d045e44593022bf))
|
||||
|
||||
|
||||
### ♻️ Code Refactoring | 代码重构
|
||||
|
||||
* **migrate:** 迁移注解以支持hyperf3 ([480f406](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/480f406cccde21f5fb39ba431b6f633c562e5f96))
|
||||
* **migrate:** 迁移注解以支持hyperf3 ([91db3c7](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/91db3c731f651e178f830953bf97958c5ddc973d))
|
||||
|
||||
|
||||
### 📦 Build System | 打包构建
|
||||
|
||||
* **ci:** 完善 style-fix/syntax-check/unit-test ([52ff5cc](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/52ff5ccb048d41d003b7e044c446c2f04a013bdb))
|
||||
* **ci:** 完善 style-fix/syntax-check/unit-test ([4b7f118](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/4b7f1189d33e8871091d05d330ed16fa98048dd9))
|
||||
* **composer:** update dependencies ([e004ab4](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/e004ab402df9185ead225a3398077f3e39dffe92))
|
||||
* **composer:** 更新依赖 ([1e4fbe5](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/1e4fbe59276ed2913aa21973f9502505115d2628))
|
||||
* **composer:** 添加依赖到开发环境 ([9f5699f](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/9f5699f39cd7a89c37e9365322c6082a97a973f4))
|
||||
* **version:** 添加changelog配置 ([4deffe0](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/4deffe055ebe8586a183103d84dcd5b97969ca7d))
|
||||
* **version:** 添加changelog配置及脚本 ([3bc4e27](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/3bc4e275a761bf71bb0ca227770b82c3d5ec76bb))
|
||||
* 迁移到hyperf3 ([9fedbb4](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/9fedbb40f4c9f3221a97986dc7063d049c914728))
|
||||
|
||||
|
||||
### 🚀 Chore | 构建/工程依赖/工具
|
||||
|
||||
* **release:** 0.2.1-0 ([ab44630](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/ab44630db8c2d05c21bccdf0899569f911b01473))
|
||||
* 移除调试用的打印语句 ([37ed1ce](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/37ed1cef77d094f6cd78dd1c97f5d2e3cf6376a3))
|
||||
* **tests:** 移除了示例测试文件 ([e705bed](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/e705bedd5d3e72887e9e5fac35926237db844116))
|
||||
|
||||
|
||||
### 🐛 Bug Fixes | Bug 修复
|
||||
|
||||
* **composer:** 修复ConfigProvider命名空间 ([3989f59](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/3989f5913ea56cba8751afc225c19b8f6730d2c3))
|
||||
@@ -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
VERSION_TRACKER.txt
Normal file
1
VERSION_TRACKER.txt
Normal file
@@ -0,0 +1 @@
|
||||
0.2.13
|
||||
@@ -5,7 +5,7 @@
|
||||
"description": "Common Hyperf Development Kit",
|
||||
"extra": {
|
||||
"hyperf": {
|
||||
"config": "Singularity\\HDK\\ConfigProvider"
|
||||
"config": "Singularity\\HDK\\Core\\ConfigProvider"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
@@ -15,55 +15,67 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "~8.0",
|
||||
"php": "^8.0",
|
||||
"ext-redis": "^5.3",
|
||||
"ext-swoole": "*",
|
||||
"composer/composer": ">=2.0",
|
||||
"ergebnis/http-method": "^2.2",
|
||||
"hyperf/config": "^2.2",
|
||||
"hyperf/constants": "^2.2",
|
||||
"hyperf/context": "^2.2",
|
||||
"hyperf/database": "^2.2",
|
||||
"hyperf/di": "^2.2",
|
||||
"hyperf/framework": "^2.2",
|
||||
"hyperf/http-server": "^2.2",
|
||||
"hyperf/logger": "^2.2",
|
||||
"hyperf/redis": "^2.2",
|
||||
"hyperf/resource": "^2.2",
|
||||
"hyperf/translation": "^2.2",
|
||||
"hyperf/config": "^3.0.0",
|
||||
"hyperf/constants": "^3.0.0",
|
||||
"hyperf/context": "^3.0.0",
|
||||
"hyperf/di": "^3.0.0",
|
||||
"hyperf/framework": "^3.0.0",
|
||||
"hyperf/logger": "^3.0.0",
|
||||
"hyperf/redis": "^3.0.0",
|
||||
"hyperf/resource": "^3.0.0",
|
||||
"hyperf/translation": "^3.0.0",
|
||||
"jetbrains/phpstorm-attributes": "^1.0",
|
||||
"lmc/http-constants": "^1.2",
|
||||
"myclabs/php-enum": "^1.8",
|
||||
"roave/dont": "^1.5",
|
||||
"symfony/http-foundation": "^6.0",
|
||||
"symfony/polyfill-php81": "^1.26",
|
||||
"teapot/status-code": "^1.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"ext-dom": "*",
|
||||
"ext-libxml": "*",
|
||||
"alibabacloud/dysmsapi-20170525": "^2.0",
|
||||
"firebase/php-jwt": "^6.1",
|
||||
"friendsofphp/php-cs-fixer": "^3.13",
|
||||
"guzzlehttp/guzzle": "^7.5",
|
||||
"hyperf/session": "^2.2",
|
||||
"hyperf/validation": "^2.2",
|
||||
"hyperf/crontab": "^3.0.0",
|
||||
"hyperf/database": "^3.0.0",
|
||||
"hyperf/db": "^3.0.0",
|
||||
"hyperf/guzzle": "^3.0.0",
|
||||
"hyperf/session": "^3.0.0",
|
||||
"hyperf/validation": "^3.0.0",
|
||||
"pestphp/pest": "^1.22",
|
||||
"phpstan/phpstan": "^1.9",
|
||||
"phpunit/phpunit": "^9.5",
|
||||
"roave/security-advisories": "dev-latest",
|
||||
"spatie/pest-plugin-test-time": "^1.1",
|
||||
"swoole/ide-helper": "*",
|
||||
"symfony/mailer": "^6.0"
|
||||
"symfony/mailer": "^6.0",
|
||||
"symfony/serializer": "^6.0"
|
||||
},
|
||||
"suggest": {
|
||||
"firebase/php-jwt": "JWT 鉴权必需",
|
||||
"hyperf/session": "Session 鉴权必需",
|
||||
"hyperf/session": "^3.0.0",
|
||||
"symfony/mailer": "用于发送电子邮件",
|
||||
"guzzlehttp/guzzle": "需要发起 http 请求时必需",
|
||||
"symfony/serializer": "处理 XML 必需",
|
||||
"ext-dom": "处理 XML 必需",
|
||||
"ext-libxml": "处理 XML 必需",
|
||||
"hyperf/guzzle": "^3.0.0",
|
||||
"alibabacloud/dysmsapi-20170525": "阿里云短信服务必需"
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true,
|
||||
"secure-http": false
|
||||
"secure-http": false,
|
||||
"allow-plugins": {
|
||||
"pestphp/pest-plugin": true
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@@ -95,16 +107,9 @@
|
||||
"url": "https://satis.luxcreo.cn/"
|
||||
},
|
||||
"packagist": {
|
||||
"type": "composer",
|
||||
"url": "https://mirrors.aliyun.com/composer/"
|
||||
},
|
||||
"packagist-tx": {
|
||||
"type": "composer",
|
||||
"url": "https://mirrors.cloud.tencent.com/composer/"
|
||||
},
|
||||
"packagist-hw": {
|
||||
"type": "composer",
|
||||
"url": "https://repo.huaweicloud.com/repository/php/"
|
||||
}
|
||||
}
|
||||
},
|
||||
"version": "0.2.13"
|
||||
}
|
||||
|
||||
3001
composer.lock
generated
3001
composer.lock
generated
File diff suppressed because it is too large
Load Diff
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;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
@@ -1,9 +1,13 @@
|
||||
parameters:
|
||||
level: 6
|
||||
reportUnmatchedIgnoredErrors: false
|
||||
checkGenericClassInNonGenericObjectType: false
|
||||
paths:
|
||||
- publish
|
||||
- src
|
||||
- tests
|
||||
ignoreErrors:
|
||||
- '#Constant BASE_PATH not found#'
|
||||
- '#Property [a-zA-Z0-9\\_]+::\$[a-zA-Z0-9]+ is never written, only read.#'
|
||||
- '#Property [a-zA-Z0-9\\_]+::\$[a-zA-Z0-9]+ is never written, only read\.#'
|
||||
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9]+\(\) is unused\.#'
|
||||
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9]+\(\) has parameter \$response with no value type specified in iterable type array\.#'
|
||||
|
||||
@@ -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'), // 可选
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/usr/bin/env sh
|
||||
docker login harbor.luxcreo.cn -u php -p fTr6oVvqqdzVlYgnZhRPPAP54u7SmqGA
|
||||
|
||||
image_name=harbor.luxcreo.cn/php/composer-template
|
||||
docker build --tag $image_name .
|
||||
|
||||
#docker push $image_name
|
||||
#docker push ${image_name}-dev
|
||||
@@ -6,4 +6,5 @@ docker run \
|
||||
-w "/srv/www" \
|
||||
-v "$(pwd)":/srv/www \
|
||||
-v ~/.ssh:/root/.ssh \
|
||||
-v ~/.gitconfig:/root/.gitconfig \
|
||||
harbor.luxcreo.cn/library/hyperf:8.0-swoole /bin/ash
|
||||
|
||||
5
scripts/release.sh
Executable file
5
scripts/release.sh
Executable file
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
docker run --rm -it \
|
||||
-v $(pwd):/app -e "GIT_AUTHOR_NAME=$(git config user.name)" -e "EMAIL=$(git config user.email)" \
|
||||
detouched/standard-version:latest $1
|
||||
@@ -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' => [
|
||||
[
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CommonErrorCode.php@hyperf-development-kit
|
||||
*
|
||||
@@ -13,382 +14,307 @@ use Hyperf\Constants\AbstractConstants;
|
||||
use Hyperf\Constants\Annotation\Constants;
|
||||
|
||||
/**
|
||||
* @Constants
|
||||
* Singularity\HyperfDevelopmentKit\Utils\Constants\CommonErrorCode@hyperf-development-kit
|
||||
*
|
||||
* @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
|
||||
{
|
||||
// =============== 9 服务器异常 =================
|
||||
|
||||
/**
|
||||
* @Message("common_error.server_common_error")
|
||||
*/
|
||||
public const SERVER_ERROR = 9000000;
|
||||
|
||||
/**
|
||||
* @Message("common_error.program.default")
|
||||
*/
|
||||
public const PROGRAM_ERROR = 9000000;
|
||||
|
||||
// 901 逻辑错误
|
||||
|
||||
/**
|
||||
* @Message("common_error.program.syntax.default")
|
||||
*/
|
||||
public const PROGRAM_SYNTAX_ERROR = 9010001;
|
||||
|
||||
// 902 SQL 错误
|
||||
|
||||
/**
|
||||
* @Message("common_error.program.sql.default")
|
||||
*/
|
||||
public const PROGRAM_SQL_ERROR = 9020001;
|
||||
|
||||
/**
|
||||
* @Message("common_error.program.sql.update")
|
||||
*/
|
||||
public const PROGRAM_SQL_UPDATE_ERROR = 9020101;
|
||||
|
||||
// 90201 字段不存在
|
||||
|
||||
/**
|
||||
* @Message("common_error.program.sql.column_not_found")
|
||||
*/
|
||||
public const PROGRAM_SQL_COLUMN_NOT_FOUND = 9020101;
|
||||
|
||||
/**
|
||||
* @Message("common_error.program.sql.insert")
|
||||
*/
|
||||
public const PROGRAM_SQL_INSERT_ERROR = 9020201;
|
||||
|
||||
// =============== 1 请求错误 =================
|
||||
// 101 请求方式有误
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.method.default")
|
||||
*/
|
||||
public const REQUEST_METHOD_ERROR = 1010001;
|
||||
|
||||
// 102 参数非法
|
||||
|
||||
/**
|
||||
* 参数非法.
|
||||
* @Message("common_error.params.format_error")
|
||||
*/
|
||||
public const FORMATTER_ERROR = 1020001;
|
||||
|
||||
/**
|
||||
* 缺少参数.
|
||||
* @Message("common_error.params.error.default")
|
||||
*/
|
||||
public const REQUEST_PARAMS_MISS = 1020002;
|
||||
|
||||
/**
|
||||
* 签名非法.
|
||||
* @Message("common_error.params.error.default")
|
||||
*/
|
||||
public const REQUEST_SIGN_ERROR = 1020003;
|
||||
|
||||
|
||||
// 103 参数错误
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.default")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR = 1030001;
|
||||
|
||||
// 10301 用户信息错误
|
||||
/**
|
||||
* @Message("common_error.params.error.user.default")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER = 1030101;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.sign_up.default")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_REGISTERED = 1030102;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.username.unique")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_USERNAME_UNIQUE = 1030111;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.sec_phone.unique")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_SEC_PHONE_UNIQUE = 1030121;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.sec_phone.required")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_SEC_PHONE_REQUIRED = 1030122;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.sec_phone.undefined")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_SEC_PHONE_UNDEFINED = 1030123;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.sec_email.unique")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_SEC_EMAIL_UNIQUE = 1030131;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.sec_email.required")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_SEC_EMAIL_REQUIRED = 1030132;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.sec_email.undefined")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_SEC_EMAIL_UNDEFINED = 1030133;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.user.action.error")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_USER_ACTION_ERROR = 1030141;
|
||||
|
||||
// 10302 验证码错误
|
||||
/**
|
||||
* @Message("common_error.params.error.code.error")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_CODE_ERROR = 1030201;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.code.timeout")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_CODE_TIMEOUT = 1030202;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.code.limit.minute")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_MINUTE = 1030301;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.code.limit.hour")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_HOUR = 1030302;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.code.limit.day")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_DAY = 1030303;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.target.format")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_TARGET_FORMAT = 1030401;
|
||||
|
||||
/**
|
||||
* @Message("common_error.params.error.target.missing")
|
||||
*/
|
||||
public const REQUEST_PARAMS_ERROR_TARGET_MISSING = 1030402;
|
||||
|
||||
// =============== 2 鉴权问题 =================
|
||||
// 201 JWT 鉴权失败
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.default")
|
||||
*/
|
||||
public const UNAUTHORIZED = 200000;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.jwt.default")
|
||||
*/
|
||||
public const AUTH_JWT_ERROR = 201000;
|
||||
|
||||
// 20101 验证
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.jwt.iat")
|
||||
*/
|
||||
public const AUTH_JWT_IAT_ERROR = 2010101;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.jwt.nbf")
|
||||
*/
|
||||
public const AUTH_JWT_NBF_ERROR = 2010102;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.jwt.exp")
|
||||
*/
|
||||
public const AUTH_JWT_EXP_TIMEOUT = 2010103;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.jwt.iss")
|
||||
*/
|
||||
public const AUTH_JWT_ISS_ERROR = 2010104;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.jwt.uid")
|
||||
*/
|
||||
public const AUTH_JWT_UID_ERROR = 2010105;
|
||||
|
||||
// 202 Session 鉴权
|
||||
/**
|
||||
* @Message("common_error.auth.session.default")
|
||||
*/
|
||||
public const AUTH_SESSION_ERROR = 202000;
|
||||
|
||||
// 20201 验证
|
||||
/**
|
||||
* @Message("common_error.auth.session.uid")
|
||||
*/
|
||||
public const AUTH_SESSION_UID_ERROR = 2020101;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.session.created_at")
|
||||
*/
|
||||
public const AUTH_SESSION_CREATED_AT_ERROR = 2020102;
|
||||
|
||||
|
||||
// 203 SAML 鉴权
|
||||
/**
|
||||
* @Message("common_error.auth.saml.default")
|
||||
*/
|
||||
public const AUTH_SAML_ERROR = 203000;
|
||||
|
||||
// 20301 验证
|
||||
/**
|
||||
* @Message("common_error.auth.saml.params.default")
|
||||
*/
|
||||
public const AUTH_SAML_REQUEST_PARAMS_ERROR = 2030100;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.saml.params.saml_request")
|
||||
*/
|
||||
public const AUTH_SAML_REQUEST_PARAMS_SAML_REQUEST = 2030101;
|
||||
|
||||
|
||||
// 204 无权访问
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.default")
|
||||
*/
|
||||
public const FORBIDDEN = 204000;
|
||||
|
||||
// 20401 无权访问
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.read.default")
|
||||
*/
|
||||
public const FORBIDDEN_READ_ERROR = 2040101;
|
||||
|
||||
// 20402 无权修改
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.update.default")
|
||||
*/
|
||||
public const FORBIDDEN_UPDATE_ERROR = 2040201;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.update.address.unique")
|
||||
*/
|
||||
public const FORBIDDEN_UPDATE_ADDRESS_DEFAULT_UNIQUE_ERROR = 2040211;
|
||||
|
||||
// 20403 无权创建
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.create.default")
|
||||
*/
|
||||
public const FORBIDDEN_CREATE_ERROR = 2040301;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.create.wechat.redundant")
|
||||
*/
|
||||
public const FORBIDDEN_CREATE_WECHAT_REDUNDANT_ERROR = 2040311;
|
||||
|
||||
// 20404 无权删除
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.delete.default")
|
||||
*/
|
||||
public const FORBIDDEN_DELETE_ERROR = 2040401;
|
||||
|
||||
/**
|
||||
* @Message("common_error.auth.forbidden.delete.wechat.only")
|
||||
*/
|
||||
public const FORBIDDEN_DELETE_ONLY_USERNAME_WITH_WECHAT = 2040411;
|
||||
|
||||
// 205 App 鉴权
|
||||
/**
|
||||
* @Message("common_error.auth.app.default")
|
||||
*/
|
||||
public const AUTH_APP_ERROR = 205000;
|
||||
|
||||
// ============== 3 依赖服务出错 ===============
|
||||
// 303 缓存异常
|
||||
// 30301 Redis 异常
|
||||
|
||||
/**
|
||||
* @Message("common_error.server.cache.redis.default")
|
||||
*/
|
||||
public const SERVER_CACHE_REDIS_ERROR = 3030101;
|
||||
|
||||
/**
|
||||
* @Message("common_error.server.cache.redis.refused")
|
||||
*/
|
||||
public const SERVER_CACHE_REDIS_REFUSED_ERROR = 3030110;
|
||||
|
||||
// 30302 SMS 异常
|
||||
/**
|
||||
* @Message("common_error.server.dependency_sms_common_error")
|
||||
*/
|
||||
public const SERVER_DEPENDENCY_SMS_ERROR = 3030201;
|
||||
|
||||
// 30303 微信错误
|
||||
/**
|
||||
* @Message("common_error.server.dependency.wechat.default")
|
||||
*/
|
||||
public const SERVER_DEPENDENCY_WECHAT_ERROR = 3030301;
|
||||
|
||||
/**
|
||||
* @Message("common_error.server.dependency.wechat.code")
|
||||
*/
|
||||
public const SERVER_DEPENDENCY_WECHAT_CODE_ERROR = 3030302;
|
||||
|
||||
/**
|
||||
* @Message("common_error.server.dependency.wechat.timeout")
|
||||
*/
|
||||
public const SERVER_DEPENDENCY_WECHAT_CODE_TIMEOUT_ERROR = 3030311;
|
||||
|
||||
// 306 消息异常
|
||||
// 30601 自建邮箱发件服务异常
|
||||
/**
|
||||
* @Message("common_error.server.message.email.default")
|
||||
*/
|
||||
public const SERVER_MESSAGE_EMAIL_ERROR = 3060101;
|
||||
|
||||
/**
|
||||
* @Message("common_error.server.message.email.not_found")
|
||||
*/
|
||||
public const SERVER_MESSAGE_EMAIL_NOT_FOUND = 3060102;
|
||||
|
||||
// =============== 4 资源不存在 ================
|
||||
// 401 路由不存在
|
||||
/**
|
||||
* @Message("common_error.not_found.route.default")
|
||||
*/
|
||||
public const ROUTE_NOT_FOUND = 4010001;
|
||||
|
||||
// 402 模型不存在
|
||||
/**
|
||||
* @Message("common_error.model.not_found")
|
||||
*/
|
||||
public const MODEL_NOT_FOUND = 4020001;
|
||||
|
||||
// 40201 用户不存在
|
||||
/**
|
||||
* @Message("common_error.model.user.default")
|
||||
* @note !!!这个码一定不要改,前端根据这个做判断了!!!!
|
||||
*/
|
||||
public const USER_NOT_FOUND = 4020101;
|
||||
|
||||
/**
|
||||
* @Message("common_error.model.user.not_valid")
|
||||
*/
|
||||
public const USER_NOT_VALID = 4020102;
|
||||
|
||||
/**
|
||||
* @Message("common_error.model.document.default")
|
||||
*/
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AbstractController.php@hyperf-development-kit
|
||||
*
|
||||
@@ -42,27 +43,15 @@ abstract class AbstractController
|
||||
use DontDeserialise;
|
||||
use DontToString;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var RequestInterface
|
||||
*/
|
||||
#[Inject]
|
||||
protected RequestInterface $request;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
#[Inject]
|
||||
protected ResponseInterface $response;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -45,142 +45,86 @@ use Throwable;
|
||||
class CommonHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* @Inject(required=false)
|
||||
*
|
||||
* @var RequestInterface|null
|
||||
*/
|
||||
#[Inject(required: false)]
|
||||
private ?RequestInterface $request;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var StdoutLogger
|
||||
*/
|
||||
#[Inject]
|
||||
private StdoutLogger $logger;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function handle(
|
||||
Throwable $throwable,
|
||||
ResponseInterface $response
|
||||
): ResponseInterface {
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
|
||||
$restful = config('common.response.restful');
|
||||
$code_name = config('common.response.code_name');
|
||||
$message_name = config('common.response.message_name');
|
||||
|
||||
$is_testing = config('app_status') === true;
|
||||
$this->request?->url();
|
||||
|
||||
$is_debug = $this->request?->hasHeader('Postman-Token')
|
||||
|| str_starts_with($this->request?->header('User-Agent'), 'apifox');
|
||||
|
||||
$is_debug = $this->request?->hasHeader('Postman-Token') || str_starts_with($this->request?->header('User-Agent', ''), 'apifox');
|
||||
$error_type = $throwable::class;
|
||||
$request_time = date('Y-m-d H:i:s');
|
||||
$request_data = Json::encode($this->request?->getParsedBody());
|
||||
$request_headers = Json::encode($this->request?->getHeaders());
|
||||
|
||||
// 901 程序语法错误
|
||||
|
||||
// 902 SQL 语法错误
|
||||
if ($throwable instanceof QueryException) {
|
||||
$code = match ($throwable->getCode()) {
|
||||
'42S22' => CommonErrorCode::PROGRAM_SQL_COLUMN_NOT_FOUND,
|
||||
default => CommonErrorCode::PROGRAM_SQL_ERROR
|
||||
default => CommonErrorCode::PROGRAM_SQL_ERROR,
|
||||
};
|
||||
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage(
|
||||
$is_testing
|
||||
? $code
|
||||
: CommonErrorCode::PROGRAM_SQL_ERROR
|
||||
),
|
||||
];
|
||||
$data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($is_testing ? $code : CommonErrorCode::PROGRAM_SQL_ERROR)];
|
||||
if ($is_testing) {
|
||||
$data['details'] = [
|
||||
'sql' => $throwable->getSql(),
|
||||
'error' => $throwable->getMessage(),
|
||||
];
|
||||
$data['details'] = ['sql' => $throwable->getSql(), 'error' => $throwable->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
// 101 请求方式错误
|
||||
if ($throwable instanceof MethodNotAllowedHttpException) {
|
||||
$message = explode(': ', $throwable->getMessage());
|
||||
$allow_method = explode(', ', $message[1]);
|
||||
$code = CommonErrorCode::REQUEST_METHOD_ERROR;
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage($code, [
|
||||
'methods' => join(', ', $allow_method),
|
||||
]),
|
||||
'currentMethod' => $this->request?->getMethod(),
|
||||
'allowedMethod' => $allow_method,
|
||||
];
|
||||
$data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($code, ['methods' => join(', ', $allow_method)]), 'currentMethod' => $this->request?->getMethod(), 'allowedMethod' => $allow_method];
|
||||
}
|
||||
|
||||
// 验证失败
|
||||
if ($throwable instanceof BadRequestHttpException) {
|
||||
$data = [
|
||||
$code_name => CommonErrorCode::REQUEST_PARAMS_ERROR,
|
||||
$message_name => $is_testing
|
||||
? $throwable->getMessage()
|
||||
: CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR),
|
||||
];
|
||||
$data = [$code_name => CommonErrorCode::REQUEST_PARAMS_ERROR, $message_name => $is_testing ? $throwable->getMessage() : CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR)];
|
||||
}
|
||||
|
||||
if ($throwable instanceof ValidationException) {
|
||||
$data = $throwable->validator->errors()->first();
|
||||
|
||||
if (is_numeric($data)) {
|
||||
$code = (int)$data;
|
||||
$code = (int) $data;
|
||||
$data = CommonErrorCode::getMessage($code);
|
||||
}
|
||||
$data = [
|
||||
$code_name => $code ?? CommonErrorCode::REQUEST_PARAMS_ERROR,
|
||||
$message_name => $data,
|
||||
];
|
||||
$data = [$code_name => $code ?? CommonErrorCode::REQUEST_PARAMS_ERROR, $message_name => $data];
|
||||
}
|
||||
if ($throwable instanceof ValidateException) {
|
||||
$code = $throwable->getCode();
|
||||
$message = $throwable->getMessage();
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => empty($message) ? CommonErrorCode::getMessage($code, [
|
||||
'param' => $throwable->getFieldName(),
|
||||
]) : $message,
|
||||
];
|
||||
$data = [$code_name => $code, $message_name => empty($message) ? CommonErrorCode::getMessage($code, ['param' => $throwable->getFieldName()]) : $message];
|
||||
if ($is_debug) {
|
||||
$data['currentValue'] = $throwable->getCurrentValue();
|
||||
$data['availableValue'] = $throwable->getAvailableValue();
|
||||
}
|
||||
}
|
||||
|
||||
// 路由不存在
|
||||
if ($throwable instanceof NotFoundHttpException) {
|
||||
$code = CommonErrorCode::ROUTE_NOT_FOUND;
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage($code),
|
||||
];
|
||||
$data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($code)];
|
||||
$status_code = 404;
|
||||
}
|
||||
// 模型不存在
|
||||
if ($throwable instanceof ModelNotFoundException) {
|
||||
$code = empty($throwable->getCode()) ? CommonErrorCode::MODEL_NOT_FOUND : $throwable->getCode();
|
||||
$message = empty($throwable->getCode()) ? CommonErrorCode::getMessage($code, [
|
||||
'resource' => '资源',
|
||||
]) : $throwable->getMessage();
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => $message,
|
||||
];
|
||||
$message = empty($throwable->getCode()) ? CommonErrorCode::getMessage($code, ['resource' => '资源']) : $throwable->getMessage();
|
||||
$data = [$code_name => $code, $message_name => $message];
|
||||
$status_code = 404;
|
||||
}
|
||||
|
||||
// 300 服务出错
|
||||
// 303 缓存异常
|
||||
if ($throwable instanceof RedisException) {
|
||||
@@ -188,16 +132,8 @@ class CommonHandler extends ExceptionHandler
|
||||
if ($throwable->getMessage() === 'Connection refused') {
|
||||
$code = CommonErrorCode::SERVER_CACHE_REDIS_REFUSED_ERROR;
|
||||
}
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage(
|
||||
$is_testing
|
||||
? $code
|
||||
: CommonErrorCode::SERVER_CACHE_REDIS_ERROR
|
||||
),
|
||||
];
|
||||
$data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($is_testing ? $code : CommonErrorCode::SERVER_CACHE_REDIS_ERROR)];
|
||||
}
|
||||
|
||||
// 306 消息异常
|
||||
// 30601 邮箱发件异常
|
||||
if ($throwable instanceof TransportException) {
|
||||
@@ -205,63 +141,34 @@ class CommonHandler extends ExceptionHandler
|
||||
if (strpos($throwable->getMessage(), '500 Error: bad syntax')) {
|
||||
$code = CommonErrorCode::SERVER_MESSAGE_EMAIL_NOT_FOUND;
|
||||
}
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage(
|
||||
$is_testing
|
||||
? $code
|
||||
: CommonErrorCode::SERVER_MESSAGE_EMAIL_ERROR
|
||||
),
|
||||
];
|
||||
$data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($is_testing ? $code : CommonErrorCode::SERVER_MESSAGE_EMAIL_ERROR)];
|
||||
}
|
||||
|
||||
if (empty($data)) {
|
||||
// 其他情况
|
||||
$data = [
|
||||
$code_name => $is_testing
|
||||
? ($throwable->getCode() == 0
|
||||
? CommonErrorCode::SERVER_ERROR
|
||||
: $throwable->getCode())
|
||||
: CommonErrorCode::SERVER_ERROR,
|
||||
$message_name => $is_testing ? $throwable->getMessage() : __(
|
||||
CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR)
|
||||
),
|
||||
];
|
||||
|
||||
$data = [$code_name => $is_testing ? $throwable->getCode() == 0 ? CommonErrorCode::SERVER_ERROR : $throwable->getCode() : CommonErrorCode::SERVER_ERROR, $message_name => $is_testing ? $throwable->getMessage() : __(CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR))];
|
||||
// 其他错误
|
||||
if ($throwable instanceof HttpException) {
|
||||
$data = [
|
||||
$code_name => $throwable->getCode() ?: $throwable->getStatusCode(),
|
||||
$message_name => $throwable->getMessage(),
|
||||
];
|
||||
$data = [$code_name => $throwable->getCode() ?: $throwable->getStatusCode(), $message_name => $throwable->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
$response = $response->withHeader(
|
||||
Header::CONTENT_TYPE,
|
||||
'application/json; charset=utf-8'
|
||||
);
|
||||
|
||||
$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(
|
||||
<<<ERROR_LOG
|
||||
TYPE: $error_type
|
||||
[$data[$code_name]] $data[$message_name]
|
||||
TYPE: {$error_type}
|
||||
[{$data[$code_name]}] {$data[$message_name]}
|
||||
{$throwable->getMessage()}
|
||||
-------------------------------
|
||||
REQUEST_TIME: $request_time
|
||||
REQUEST_TIME: {$request_time}
|
||||
-------------------------------
|
||||
REQUEST_HEADERS:
|
||||
$request_headers
|
||||
{$request_headers}
|
||||
-------------------------------
|
||||
REQUEST_COOKIES:
|
||||
$cookies
|
||||
{$cookies}
|
||||
-------------------------------
|
||||
REQUEST_METHOD:
|
||||
{$this->request?->getMethod()}
|
||||
@@ -273,7 +180,7 @@ REQUEST_QUERY:
|
||||
{$this->request?->getQueryString()}
|
||||
-------------------------------
|
||||
REQUEST_DATA:
|
||||
$request_data
|
||||
{$request_data}
|
||||
-------------------------------
|
||||
TRACE:
|
||||
{$throwable->getTraceAsString()}
|
||||
@@ -282,21 +189,11 @@ TRACE:
|
||||
ERROR_LOG
|
||||
);
|
||||
$data = Json::encode($data);
|
||||
|
||||
if ($restful) {
|
||||
$response = $response->withStatus(
|
||||
$status_code ??
|
||||
$throwable->status ??
|
||||
$throwable->statusCode ??
|
||||
RFC7231::INTERNAL_SERVER_ERROR
|
||||
);
|
||||
$response = $response->withStatus($status_code ?? $throwable->status ?? $throwable->statusCode ?? RFC7231::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return $response
|
||||
->withBody(
|
||||
new SwooleStream($data)
|
||||
);
|
||||
return $response->withBody(new SwooleStream($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断该异常处理器是否要对该异常进行处理.
|
||||
*/
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ClassicCoreMiddleware.php@hyperf-development-kit
|
||||
*
|
||||
@@ -9,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;
|
||||
@@ -27,34 +28,36 @@ use Singularity\HDK\Core\Service\UtilsService;
|
||||
* @author 李东云<dongyun.li@luxcreo.cn>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2022/4/29
|
||||
*
|
||||
* @deprecated
|
||||
* @see CommonCoreMiddleware
|
||||
*/
|
||||
class ClassicCoreMiddleware extends CoreMiddleware
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @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<string, mixed>|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
|
||||
{
|
||||
$code_name = config('common.response.code_name');
|
||||
$message_name = config('common.response.message_name');
|
||||
$data_name = config('common.response.data_name');
|
||||
|
||||
// 分页数据
|
||||
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,
|
||||
@@ -67,11 +70,10 @@ class ClassicCoreMiddleware extends CoreMiddleware
|
||||
$paginator->previousPageUrl(),
|
||||
'prev'
|
||||
);
|
||||
return $fact_response
|
||||
->withAddedHeader(Header::CONTENT_TYPE, 'application/json')
|
||||
->withBody(
|
||||
new SwooleStream(
|
||||
Json::encode([
|
||||
return $fact_response->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(
|
||||
new SwooleStream(
|
||||
Json::encode(
|
||||
[
|
||||
$code_name => 200,
|
||||
$message_name => 'ok',
|
||||
$data_name => $response->items(),
|
||||
@@ -81,39 +83,28 @@ class ClassicCoreMiddleware extends CoreMiddleware
|
||||
'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)));
|
||||
}
|
||||
|
||||
// 普通数组
|
||||
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, 'text/plain')
|
||||
->withBody(
|
||||
new SwooleStream((string)$response)
|
||||
$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))
|
||||
);
|
||||
}
|
||||
// 可 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
32
src/Middleware/ClassicStyleMiddleware.php
Normal file
32
src/Middleware/ClassicStyleMiddleware.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* ClassicStyleMiddleware.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/3/10
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Singularity\HDK\Core\Service\ApiStyleService;
|
||||
|
||||
class ClassicStyleMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(protected ApiStyleService $apiStyleService)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$this->apiStyleService->set(ApiStyleService::CLASSIC);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
157
src/Middleware/CommonCoreMiddleware.php
Normal file
157
src/Middleware/CommonCoreMiddleware.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ClassicCoreMiddleware.php@hyperf-development-kit
|
||||
*
|
||||
* @author 李东云<dongyun.li@luxcreo.cn>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2022/4/29
|
||||
*/
|
||||
|
||||
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 Lmc\HttpConstants\Header;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Singularity\HDK\Core\Service\ApiStyleService;
|
||||
use Singularity\HDK\Core\Service\UtilsService;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Middleware\CommonCoreMiddleware@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/3/10
|
||||
*/
|
||||
class CommonCoreMiddleware extends CoreMiddleware
|
||||
{
|
||||
#[Inject]
|
||||
private UtilsService $utilsService;
|
||||
|
||||
#[Inject]
|
||||
private ApiStyleService $apiStyleService;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$style = $this->apiStyleService->get();
|
||||
|
||||
if ($style === ApiStyleService::RESTFUL) {
|
||||
return $this->transferToRestfulResponse($response, $request);
|
||||
} else {
|
||||
return $this->transferToClassicResponse($response, $request);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string|Arrayable|Jsonable|null $response
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function transferToRestfulResponse(Arrayable|Jsonable|array|string|null $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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string|Arrayable|Jsonable|null $response
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function transferToClassicResponse(
|
||||
Arrayable|Jsonable|array|string|null $response,
|
||||
ServerRequestInterface $request
|
||||
): ResponseInterface {
|
||||
$code_name = config('common.response.code_name');
|
||||
$message_name = config('common.response.message_name');
|
||||
$data_name = config('common.response.data_name');
|
||||
// 分页数据
|
||||
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->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(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(),
|
||||
],
|
||||
]
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
// 普通数组
|
||||
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))
|
||||
);
|
||||
}
|
||||
// 可 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -15,20 +15,16 @@ use Singularity\HDK\Core\Service\ExtendService;
|
||||
class ExtendsMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var \Singularity\HDK\Core\Service\ExtendService
|
||||
*/
|
||||
#[Inject]
|
||||
private ExtendService $service;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function process(
|
||||
ServerRequestInterface $request,
|
||||
RequestHandlerInterface $handler
|
||||
): ResponseInterface {
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$this->service->parse($request);
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@ namespace Singularity\HDK\Core\Middleware;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Contract\TranslatorInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Lmc\HttpConstants\Header;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Utils\Middleware\InternationalizationMiddleware@HDK
|
||||
@@ -22,9 +24,9 @@ use Psr\Http\Server\RequestHandlerInterface;
|
||||
class InternationalizationMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
/**
|
||||
@@ -32,19 +34,24 @@ class InternationalizationMiddleware implements MiddlewareInterface
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$response = Context::get(ResponseInterface::class);
|
||||
$language = $request->getHeaderLine('Accept-Language');
|
||||
$req = (new Request());
|
||||
$req->headers->set(Header::ACCEPT_LANGUAGE, $request->getHeaderLine(Header::ACCEPT_LANGUAGE));
|
||||
|
||||
$language = $req->getPreferredLanguage([config('translation.locale')]);
|
||||
if (!empty($language)) {
|
||||
$language = match (strtolower($language)) {
|
||||
'en', 'en_us', 'en-us', 'en-uk', 'en_uk' => 'en',
|
||||
'zh', 'zh_cn', 'zh-cn' => 'zh_CN',
|
||||
default => config('translation.locale')
|
||||
default => config('translation.locale'),
|
||||
};
|
||||
$this->translator->setLocale($language);
|
||||
|
||||
$response = $response->withAddedHeader('Content-Language', strtr($this->translator->getLocale(), '_', '-'));
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
}
|
||||
$response = Context::get(ResponseInterface::class);
|
||||
$response = $response->withAddedHeader(
|
||||
Header::CONTENT_LANGUAGE,
|
||||
strtr($this->translator->getLocale(), '_', '-')
|
||||
);
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
32
src/Middleware/RestStyleMiddleware.php
Normal file
32
src/Middleware/RestStyleMiddleware.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* ClassicStyleMiddleware.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/3/10
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Singularity\HDK\Core\Service\ApiStyleService;
|
||||
|
||||
class RestStyleMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(protected ApiStyleService $apiStyleService)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$this->apiStyleService->set(ApiStyleService::RESTFUL);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
68
src/Middleware/RestfulCoreMiddleware.php
Normal file
68
src/Middleware/RestfulCoreMiddleware.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?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
|
||||
*
|
||||
* @deprecated
|
||||
* @see CommonCoreMiddleware
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
73
src/Middleware/SessionMiddleware.php
Normal file
73
src/Middleware/SessionMiddleware.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* SessionMiddleware.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/3/8
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Middleware;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Contract\SessionInterface;
|
||||
use Hyperf\HttpMessage\Cookie\Cookie;
|
||||
use Hyperf\HttpMessage\Server\Response;
|
||||
use Hyperf\Session\SessionManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Middleware\SessionMiddleware@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/3/8
|
||||
*/
|
||||
class SessionMiddleware extends \Hyperf\Session\Middleware\SessionMiddleware
|
||||
{
|
||||
public function __construct(private SessionManager $sessionManager, private ConfigInterface $config)
|
||||
{
|
||||
parent::__construct($this->sessionManager, $this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the session cookie to the response·.
|
||||
*/
|
||||
private function addCookieToResponse(
|
||||
ServerRequestInterface $request,
|
||||
ResponseInterface $response,
|
||||
SessionInterface $session
|
||||
): ResponseInterface {
|
||||
$cookie = new Cookie(
|
||||
name: $session->getName(),
|
||||
value: $session->getId(),
|
||||
expire: $this->getCookieExpirationDate(),
|
||||
path: $this->config->get('session.options.path', '/'),
|
||||
domain: $this->config->get('session.options.domain', $request->getUri()->getHost()),
|
||||
secure: $this->config->get('session.options.secure', strtolower($request->getUri()->getScheme()) === 'https'),
|
||||
httpOnly: true,
|
||||
sameSite: $this->config->get('session.options.samesite', Cookie::SAMESITE_LAX)
|
||||
);
|
||||
if (!method_exists($response, 'withCookie')) {
|
||||
return $response->withHeader('Set-Cookie', (string)$cookie);
|
||||
}
|
||||
/* @var Response $response */
|
||||
return $response->withCookie($cookie);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the session lifetime in seconds.
|
||||
*/
|
||||
private function getCookieExpirationDate(): int
|
||||
{
|
||||
if ($this->config->get('session.options.expire_on_close')) {
|
||||
$expirationDate = 0;
|
||||
} else {
|
||||
$expireSeconds = $this->config->get('session.options.cookie_lifetime', 5 * 60 * 60);
|
||||
$expirationDate = Carbon::now()->addSeconds($expireSeconds)->getTimestamp();
|
||||
}
|
||||
return $expirationDate;
|
||||
}
|
||||
}
|
||||
40
src/Service/ApiStyleService.php
Normal file
40
src/Service/ApiStyleService.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* ApiStyleService.php@HDK-Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/3/10
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Service;
|
||||
|
||||
use Hyperf\Context\Context;
|
||||
|
||||
class ApiStyleService
|
||||
{
|
||||
public const RESTFUL = 'restful';
|
||||
public const CLASSIC = 'classic';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Context::set(self::class, config('common.response.restful') ? self::RESTFUL : self::CLASSIC);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param literal-string $style
|
||||
* @return $this
|
||||
*/
|
||||
public function set(string $style): static
|
||||
{
|
||||
Context::set(self::class, $style === self::RESTFUL ? self::RESTFUL : self::CLASSIC);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(): string
|
||||
{
|
||||
return Context::get(self::class) ?? (config('common.response.restful')
|
||||
? ApiStyleService::RESTFUL
|
||||
: ApiStyleService::CLASSIC);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,10 @@ use Psr\Http\Message\ServerRequestInterface;
|
||||
*/
|
||||
class ExtendService
|
||||
{
|
||||
public function __construct(private UtilsService $utils)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface|null $request
|
||||
* @param array<string, string>|null $params
|
||||
@@ -17,9 +21,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,6 +33,7 @@ class ExtendService
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
@@ -37,6 +42,12 @@ class ExtendService
|
||||
return Context::get(self::class) ?? [];
|
||||
}
|
||||
|
||||
public function hasExtends(): bool
|
||||
{
|
||||
$extends = Context::get(self::class);
|
||||
return is_array($extends) && count($extends) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否传入了此扩展
|
||||
*
|
||||
@@ -44,8 +55,8 @@ class ExtendService
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasExtends(string $field): bool
|
||||
public function hasExtend(string $field): bool
|
||||
{
|
||||
return Context::has(self::class) && isset(array_flip(Context::get(self::class))[$field]);
|
||||
return $this->utils->inArray($field, Context::get(self::class));
|
||||
}
|
||||
}
|
||||
|
||||
122
src/Service/HttpRequestService.php
Normal file
122
src/Service/HttpRequestService.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Singularity\HDK\Core\Service;
|
||||
|
||||
use Ergebnis\Http\Method;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\BadResponseException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Utils;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
use Hyperf\Utils\Codec\Json;
|
||||
use Psr\Http\Client\ClientExceptionInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* 发起 Http 请求的类
|
||||
*/
|
||||
class HttpRequestService
|
||||
{
|
||||
public const TIMEOUT = 20;
|
||||
|
||||
/**
|
||||
* @Inject()
|
||||
* @var ClientFactory
|
||||
*/
|
||||
private ClientFactory $client;
|
||||
|
||||
/**
|
||||
* @var array<literal-string, mixed>
|
||||
*/
|
||||
private array $options = [];
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, string|int> $params
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestGet(string $url, array $params = []): ResponseInterface
|
||||
{
|
||||
$request = new Request(Method\Rfc\Rfc7231::GET, $url);
|
||||
return $this->getClient()->send($request, [
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
private function getClient(): Client
|
||||
{
|
||||
return $this->client->create($this->options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<string, string|int> $params
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestPost(string $url, array $data = [], array $params = []): ResponseInterface
|
||||
{
|
||||
$data = Json::encode($data);
|
||||
$request = new Request('post', $url, ['Content-Type' => 'application/json'], Utils::streamFor($data));
|
||||
return $this->getClient()->send($request, [
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<string, string|int> $params
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestPut(string $url, array $data = [], array $params = []): ResponseInterface
|
||||
{
|
||||
$data = Json::encode($data);
|
||||
$request = new Request('put', $url, ['Content-Type' => 'application/json'], Utils::streamFor($data));
|
||||
$result = $this->getClient()->send($request, [
|
||||
'params' => $params,
|
||||
]);
|
||||
$json = Json::decode($result->getBody()->getContents());
|
||||
if ($json['code'] !== 200) {
|
||||
throw new ServerException($json['message'], $request, $result);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定制 options
|
||||
* @param array<string, mixed> $options
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options): HttpRequestService
|
||||
{
|
||||
$this->options = array_replace_recursive($this->options, $options);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @param RequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
private function parseResponse(
|
||||
ResponseInterface $response,
|
||||
RequestInterface $request
|
||||
): ResponseInterface {
|
||||
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
|
||||
return $response;
|
||||
} else {
|
||||
throw new BadResponseException($response->getReasonPhrase(), $request, $response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 = '';
|
||||
@@ -329,4 +328,17 @@ class UtilsService
|
||||
'unit' => $unitToUpper ? strtoupper($unit[$i]) : $unit[$i],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更快的判断数组元素方法
|
||||
* @param float|int|string $needle
|
||||
* @param array<int, mixed> $haystack
|
||||
* @return bool
|
||||
*/
|
||||
public function inArray(float|int|string $needle, array $haystack): bool
|
||||
{
|
||||
$list = array_flip($haystack);
|
||||
return isset($list[$needle]);
|
||||
}
|
||||
}
|
||||
|
||||
72
src/Service/XmlService.php
Normal file
72
src/Service/XmlService.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* XmlService.php@LuxOP
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/4/10
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Service;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||
|
||||
class XmlService
|
||||
{
|
||||
private XmlEncoder $service;
|
||||
|
||||
public function __construct(XmlEncoder $encoder)
|
||||
{
|
||||
$this->service = $encoder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object|array<string, mixed> $data
|
||||
* @param string $xmlRootNodeName
|
||||
* @param bool $xmlFormatOutput 是否格式化
|
||||
* @param string $xmlVersion
|
||||
* @param string $xmlEncoding
|
||||
* @param bool $xmlStandalone 是否添加声明规范的标签到xml
|
||||
* @param bool $xmlTypeCastAttributes
|
||||
* @param bool $asCollection
|
||||
* @param int[] $decoderIgnoredNodeTypes
|
||||
* @param int[] $encoderIgnoredNodeTypes
|
||||
* @param int $loadOptions
|
||||
* @param bool $removeEmptyTags
|
||||
*
|
||||
* @return false|string
|
||||
* @see https://symfony.com/doc/current/components/serializer.html#the-xmlencoder
|
||||
*/
|
||||
public function encode(
|
||||
object|array $data,
|
||||
string $xmlRootNodeName = 'root',
|
||||
bool $xmlFormatOutput = true,
|
||||
string $xmlVersion = '1.0',
|
||||
string $xmlEncoding = 'utf-8',
|
||||
bool $xmlStandalone = false,
|
||||
bool $xmlTypeCastAttributes = true,
|
||||
bool $asCollection = true,
|
||||
array $decoderIgnoredNodeTypes = [XML_PI_NODE, XML_COMMENT_NODE],
|
||||
array $encoderIgnoredNodeTypes = [],
|
||||
int $loadOptions = LIBXML_NONET | LIBXML_NOBLANKS,
|
||||
bool $removeEmptyTags = false
|
||||
): false|string {
|
||||
return $this->service->encode(
|
||||
$data,
|
||||
XmlEncoder::FORMAT,
|
||||
[
|
||||
XmlEncoder::FORMAT_OUTPUT => $xmlFormatOutput,
|
||||
XmlEncoder::VERSION => $xmlVersion,
|
||||
XmlEncoder::ENCODING => $xmlEncoding,
|
||||
XmlEncoder::STANDALONE => $xmlStandalone,
|
||||
XmlEncoder::TYPE_CAST_ATTRIBUTES => $xmlTypeCastAttributes,
|
||||
XmlEncoder::ROOT_NODE_NAME => $xmlRootNodeName,
|
||||
XmlEncoder::AS_COLLECTION => $asCollection,
|
||||
XmlEncoder::DECODER_IGNORED_NODE_TYPES => $decoderIgnoredNodeTypes,
|
||||
XmlEncoder::ENCODER_IGNORED_NODE_TYPES => $encoderIgnoredNodeTypes,
|
||||
XmlEncoder::LOAD_OPTIONS => $loadOptions,
|
||||
XmlEncoder::REMOVE_EMPTY_TAGS => $removeEmptyTags,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,23 +10,47 @@
|
||||
namespace Singularity\HDK\Test\Core\Unit;
|
||||
|
||||
use Singularity\HDK\Core\Service\ExtendService;
|
||||
use Singularity\HDK\Core\Service\UtilsService;
|
||||
|
||||
$service = new ExtendService();
|
||||
$service = new ExtendService(new UtilsService());
|
||||
|
||||
|
||||
/** @var ExtendService $service */
|
||||
$service = make(ExtendService::class, ['utils' => new UtilsService()]);
|
||||
it('asserts no parameters can be parsed.', function (ExtendService $service, $params) {
|
||||
$service->parse(
|
||||
null,
|
||||
$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,
|
||||
params: [
|
||||
[
|
||||
'id' => 5,
|
||||
'extends' => 'a,b',
|
||||
]
|
||||
);
|
||||
expect($result)->toBeArray()->toHaveCount(2)->toBe(['a', 'b']);
|
||||
});
|
||||
|
||||
it('asserts has extends', function () use ($service) {
|
||||
expect($service->hasExtends('a'))->toBeTrue()
|
||||
->and($service->hasExtends('b'))->toBeTrue()
|
||||
->and($service->hasExtends('c'))->toBeFalse();
|
||||
expect($service->hasExtends())->toBeTrue();
|
||||
});
|
||||
|
||||
it('asserts has specify extend', function () use ($service) {
|
||||
expect($service->hasExtend('a'))->toBeTrue()
|
||||
->and($service->hasExtend('b'))->toBeTrue()
|
||||
->and($service->hasExtend('c'))->toBeFalse();
|
||||
})->depends('it asserts query parameters can be parsed.');
|
||||
|
||||
it('asserts parsed extends', function () use ($service) {
|
||||
|
||||
@@ -60,3 +60,22 @@ test('断言可以根据参数构建 URL', function (string $url, array $params,
|
||||
'http://username:password@127.0.0.1/git/resp?id=1#/page?a=b&c=d',
|
||||
]
|
||||
])->group('pure', 'utils');
|
||||
|
||||
test('断言可以判断是否是数组中的元素', function (
|
||||
$needle,
|
||||
array $haystack,
|
||||
bool $expect,
|
||||
bool $exceptions = false
|
||||
) use ($utils) {
|
||||
try {
|
||||
expect($utils->inArray($needle, $haystack))->toBe($expect);
|
||||
} catch (Throwable $e) {
|
||||
if ($exceptions) {
|
||||
expect(true)->toBeTrue();
|
||||
}
|
||||
}
|
||||
})->with([
|
||||
[1, ['1', 2, 3], true, false],
|
||||
['1', ['1', 2, 3], true, false],
|
||||
[[1, 2, 3], ['1', 2, 3], false, true],
|
||||
])->group('pure', 'utils');
|
||||
|
||||
101
tests/Unit/XmlServiceTest.php
Normal file
101
tests/Unit/XmlServiceTest.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* XmlServiceTest.php@LuxOP
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/4/10
|
||||
*/
|
||||
|
||||
namespace HyperfTest\Unit\Common;
|
||||
|
||||
use Singularity\HDK\Core\Service\XmlService;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||
|
||||
$service = new XmlService(new XmlEncoder());
|
||||
|
||||
test(
|
||||
'assert Xml can be encoded',
|
||||
function (array $data, string $expect, string $rootNodeName) use ($service) {
|
||||
$result = $service->encode($data, $rootNodeName);
|
||||
|
||||
expect($result)->toBe($expect);
|
||||
}
|
||||
)->with([
|
||||
'normal' => [
|
||||
'data' => [
|
||||
'@version' => '1.0',
|
||||
'@descripe' => 'Luxcreo',
|
||||
'UserName' => 'zhm',
|
||||
'Email' => 'haimei.zheng@luxcreo.ai',
|
||||
'BiosId' => '420036H32020120279',
|
||||
'validTime' => '2099/03/27',
|
||||
],
|
||||
'expect' => <<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ClientInfo version="1.0" descripe="Luxcreo">
|
||||
<UserName>zhm</UserName>
|
||||
<Email>haimei.zheng@luxcreo.ai</Email>
|
||||
<BiosId>420036H32020120279</BiosId>
|
||||
<validTime>2099/03/27</validTime>
|
||||
</ClientInfo>
|
||||
|
||||
XML
|
||||
,
|
||||
'xmlRootNodeName' => 'ClientInfo',
|
||||
],
|
||||
'normal attribute' => [
|
||||
'data' => [
|
||||
'@version' => '1.0',
|
||||
'@descripe' => 'Luxcreo',
|
||||
'UserName' => 'zhm',
|
||||
'Email' => [
|
||||
'@type' => 'Business',
|
||||
'#' => 'haimei.zheng@luxcreo.ai',
|
||||
],
|
||||
'BiosId' => '420036H32020120279',
|
||||
'validTime' => '2099/03/27',
|
||||
],
|
||||
'expect' => <<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ClientInfo version="1.0" descripe="Luxcreo">
|
||||
<UserName>zhm</UserName>
|
||||
<Email type="Business">haimei.zheng@luxcreo.ai</Email>
|
||||
<BiosId>420036H32020120279</BiosId>
|
||||
<validTime>2099/03/27</validTime>
|
||||
</ClientInfo>
|
||||
|
||||
XML
|
||||
,
|
||||
'ClientInfo',
|
||||
],
|
||||
'normal comment' => [
|
||||
'data' => [
|
||||
'@version' => '1.0',
|
||||
'@descripe' => 'Luxcreo',
|
||||
'UserName' => 'zhm',
|
||||
'Email' => [
|
||||
'@type' => 'Business',
|
||||
'#' => 'haimei.zheng@luxcreo.ai',
|
||||
],
|
||||
'BiosId' => '420036H32020120279',
|
||||
'validTime' => [
|
||||
'#comment' => '到期时间',
|
||||
],
|
||||
],
|
||||
'expect' => <<<'XML'
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ClientInfo version="1.0" descripe="Luxcreo">
|
||||
<UserName>zhm</UserName>
|
||||
<Email type="Business">haimei.zheng@luxcreo.ai</Email>
|
||||
<BiosId>420036H32020120279</BiosId>
|
||||
<validTime>
|
||||
<!--到期时间-->
|
||||
</validTime>
|
||||
</ClientInfo>
|
||||
|
||||
XML
|
||||
,
|
||||
'ClientInfo',
|
||||
],
|
||||
]);
|
||||
Reference in New Issue
Block a user