mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 07:15:06 +08:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0705d1bef3 | ||
|
|
922c780c49 | ||
|
|
cdb193778f | ||
|
|
9633fa6b8d | ||
|
|
08c1f6609c | ||
|
|
3a1f2695fe | ||
|
|
50885c57e2 | ||
|
|
9f9ff31a83 | ||
|
|
990c247cf6 | ||
|
|
3989f5913e | ||
|
|
ab44630db8 | ||
|
|
3bc4e275a7 | ||
|
|
82e257f7b4 | ||
|
|
480f406ccc | ||
|
|
91db3c731f | ||
|
|
9fedbb40f4 |
6
.idea/HDK-Core.iml
generated
6
.idea/HDK-Core.iml
generated
@@ -64,6 +64,8 @@
|
||||
<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" />
|
||||
@@ -133,12 +135,14 @@
|
||||
<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/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/facade/ignition-contracts" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/coordinator" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
||||
8
.idea/php.xml
generated
8
.idea/php.xml
generated
@@ -79,6 +79,8 @@
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/http-message" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/exception-handler" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/macroable" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/resource" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/paginator" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/engine" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/pool" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/event" />
|
||||
@@ -148,15 +150,17 @@
|
||||
<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" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/options-resolver" />
|
||||
<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" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="7.4" />
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
|
||||
<component name="PhpStan">
|
||||
<PhpStan_settings>
|
||||
<PhpStanConfiguration tool_path="$PROJECT_DIR$/vendor/bin/phpstan" />
|
||||
|
||||
@@ -30,6 +30,10 @@
|
||||
"type": "perf",
|
||||
"section": "⚡ Performance Improvements | 性能优化"
|
||||
},
|
||||
{
|
||||
"type": "tests",
|
||||
"section": "✅ Tests | 测试"
|
||||
},
|
||||
{
|
||||
"type": "test",
|
||||
"section": "✅ Tests | 测试"
|
||||
@@ -53,7 +57,7 @@
|
||||
],
|
||||
"bumpFiles": [
|
||||
{
|
||||
"filename": "MY_VERSION_TRACKER.txt",
|
||||
"filename": "VERSION_TRACKER.txt",
|
||||
"type": "plain-text"
|
||||
},
|
||||
{
|
||||
|
||||
80
CHANGELOG.md
80
CHANGELOG.md
@@ -1,12 +1,36 @@
|
||||
# 版本更新日志
|
||||
### [0.1.2](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.1.1...v0.1.2) (2023-01-28)
|
||||
### [0.2.4](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v0.2.3...v0.2.4) (2023-01-13)
|
||||
|
||||
|
||||
### 📦 Build System | 打包构建
|
||||
### ⏪ Revert | 回退
|
||||
|
||||
* **composer:** 修复 ConfigProvider 命名空间 ([1f0ef04](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/1f0ef04c7a092e2d7df1cafb94b3a50a04ee66cd))
|
||||
* **constant:** 移除冗余的http头authentication的枚举类型 ([cdb1937](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/cdb193778fb888851682bb1671bc55dd8f0e3423))
|
||||
|
||||
### 0.1.1 (2023-01-28)
|
||||
|
||||
### 🐛 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 | 初始化
|
||||
@@ -15,15 +39,35 @@
|
||||
* 初始化项目 ([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))
|
||||
|
||||
|
||||
### 🚀 Chore | 构建/工程依赖/工具
|
||||
### ♻️ Code Refactoring | 代码重构
|
||||
|
||||
* 移除调试用的打印语句 ([37ed1ce](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/37ed1cef77d094f6cd78dd1c97f5d2e3cf6376a3))
|
||||
* **tests:** 移除了示例测试文件 ([e705bed](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/e705bedd5d3e72887e9e5fac35926237db844116))
|
||||
* **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 | 打包构建
|
||||
@@ -34,23 +78,17 @@
|
||||
* **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))
|
||||
|
||||
|
||||
### ✏️ Documentation | 文档
|
||||
### 🚀 Chore | 构建/工程依赖/工具
|
||||
|
||||
* **core:** 添加作者信息 ([096d4e5](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/096d4e56ed30c23de1338bb6f057a91e629c6ea5))
|
||||
* **README:** 修复一直报错的文件名 ([135db1b](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/135db1bf6aeeedbb0d59405567bb4837f3221e49))
|
||||
* **README:** 增加说明文件 ([4cf16ae](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/4cf16ae0cdcf6f7a1eb99fde95a6c3ef01bd7b2b))
|
||||
* **README:** 提示版本过期,需要更新 ([3e2d340](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/3e2d3400b45edf4b503a91c078aaa3ef0fff553e))
|
||||
* **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))
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
### 🐛 Bug Fixes | Bug 修复
|
||||
|
||||
* 增加了inArray、hasExtends方法 ([c83728c](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/c83728cad28438c3bcef9d946680fe1637a6814f))
|
||||
* **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))
|
||||
|
||||
|
||||
### ♻️ Code Refactoring | 代码重构
|
||||
|
||||
* 向后兼容到7.4 ([0fa3c23](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/0fa3c23a3e249cd2473a468ee64d7efc8b6eafe3))
|
||||
* **composer:** 修复ConfigProvider命名空间 ([3989f59](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/3989f5913ea56cba8751afc225c19b8f6730d2c3))
|
||||
|
||||
11
README.md
11
README.md
@@ -2,15 +2,6 @@
|
||||
|
||||
The Common HDK for Any project by Hyperf.
|
||||
|
||||
> DEPRECATED!!!
|
||||
>
|
||||
> This Branch is outdated,
|
||||
> only used for support old versions.
|
||||
> Such as Hyperf 2.2 or less.
|
||||
>
|
||||
> Please UPGRADE your version to Hyperf 3.0
|
||||
> and HDK-Core 0.2.*
|
||||
|
||||
## PHP Versions
|
||||
|
||||
This version will work on Hyperf version 2.2 and above.
|
||||
@@ -31,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.php vendor:publish singularity/hdk-core
|
||||
&& php bin/hyperf 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.4
|
||||
@@ -15,44 +15,45 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.4 || ^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/translation": "^2.2",
|
||||
"hyperf/config": "3.0.*",
|
||||
"hyperf/constants": "3.0.*",
|
||||
"hyperf/context": "3.0.*",
|
||||
"hyperf/database": "3.0.*",
|
||||
"hyperf/di": "3.0.*",
|
||||
"hyperf/framework": "3.0.*",
|
||||
"hyperf/http-server": "3.0.*",
|
||||
"hyperf/logger": "3.0.*",
|
||||
"hyperf/redis": "3.0.*",
|
||||
"hyperf/resource": "3.0.*",
|
||||
"hyperf/translation": "3.0.*",
|
||||
"jetbrains/phpstorm-attributes": "^1.0",
|
||||
"lmc/http-constants": "^1.2",
|
||||
"myclabs/php-enum": "^1.8.4",
|
||||
"roave/dont": "^1.1",
|
||||
"symfony/polyfill-php81": "^1.27",
|
||||
"myclabs/php-enum": "^1.8",
|
||||
"roave/dont": "^1.5",
|
||||
"symfony/polyfill-php81": "^1.26",
|
||||
"teapot/status-code": "^1.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"alibabacloud/dysmsapi-20170525": "^2.0",
|
||||
"firebase/php-jwt": "^6.3",
|
||||
"firebase/php-jwt": "^6.1",
|
||||
"friendsofphp/php-cs-fixer": "^3.13",
|
||||
"guzzlehttp/guzzle": "^7.5",
|
||||
"hyperf/session": "^2.2",
|
||||
"hyperf/validation": "^2.2",
|
||||
"hyperf/session": "3.0.*",
|
||||
"hyperf/validation": "3.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": "^5.4"
|
||||
"symfony/mailer": "^6.0"
|
||||
},
|
||||
"suggest": {
|
||||
"hyperf/resource": "If you need",
|
||||
"firebase/php-jwt": "JWT 鉴权必需",
|
||||
"hyperf/session": "Session 鉴权必需",
|
||||
"symfony/mailer": "用于发送电子邮件",
|
||||
@@ -62,10 +63,7 @@
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true,
|
||||
"secure-http": false,
|
||||
"allow-plugins": {
|
||||
"pestphp/pest-plugin": true
|
||||
}
|
||||
"secure-http": false
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@@ -109,5 +107,5 @@
|
||||
"url": "https://repo.huaweicloud.com/repository/php/"
|
||||
}
|
||||
},
|
||||
"version": "0.1.2"
|
||||
"version": "0.2.4"
|
||||
}
|
||||
|
||||
1650
composer.lock
generated
Normal file → Executable file
1650
composer.lock
generated
Normal file → Executable file
File diff suppressed because it is too large
Load Diff
@@ -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'), // 可选
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
8
scripts/build-image.sh
Executable file
8
scripts/build-image.sh
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/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,4 @@ docker run \
|
||||
-w "/srv/www" \
|
||||
-v "$(pwd)":/srv/www \
|
||||
-v ~/.ssh:/root/.ssh \
|
||||
harbor.luxcreo.cn/library/hyperf:7.4-swoole /bin/ash
|
||||
harbor.luxcreo.cn/library/hyperf:8.0-swoole /bin/ash
|
||||
|
||||
@@ -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
|
||||
*
|
||||
@@ -16,6 +17,7 @@ use Dont\DontDeserialise;
|
||||
use Dont\DontGet;
|
||||
use Dont\DontSerialise;
|
||||
use Dont\DontSet;
|
||||
use Dont\DontToString;
|
||||
use Hyperf\Contract\ContainerInterface;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
@@ -39,28 +41,25 @@ abstract class AbstractController
|
||||
use DontClone;
|
||||
use DontSerialise;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -12,17 +12,14 @@ use Throwable;
|
||||
*/
|
||||
class DbException extends HttpException
|
||||
{
|
||||
public string $sql;
|
||||
|
||||
public function __construct(
|
||||
int $code = CommonErrorCode::PROGRAM_SQL_ERROR,
|
||||
?string $message = null,
|
||||
string $sql = '',
|
||||
public string $sql = '',
|
||||
Throwable $previous = null
|
||||
) {
|
||||
$this->sql = $sql;
|
||||
if ($code == CommonErrorCode::FORBIDDEN) {
|
||||
$previous_code = empty($previous) ? null : $previous->getCode();
|
||||
$previous_code = $previous?->getCode();
|
||||
$code = empty($previous_code) ? $code : $previous_code;
|
||||
}
|
||||
parent::__construct(
|
||||
|
||||
@@ -18,7 +18,7 @@ class Forbidden extends HttpException
|
||||
Throwable $previous = null
|
||||
) {
|
||||
if ($code == CommonErrorCode::FORBIDDEN) {
|
||||
$previous_code = empty($previous) ? null : $previous->getCode();
|
||||
$previous_code = $previous?->getCode();
|
||||
$code = empty($previous_code) ? $code : $previous_code;
|
||||
}
|
||||
parent::__construct(
|
||||
|
||||
@@ -45,154 +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;
|
||||
|
||||
$error_type = get_class($throwable);
|
||||
$this->request?->url();
|
||||
$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');
|
||||
|
||||
if (!empty($this->request)) {
|
||||
$this->request->url();
|
||||
|
||||
$is_debug = $this->request->hasHeader('Postman-Token')
|
||||
|| str_starts_with($this->request->header('User-Agent'), 'apifox');
|
||||
|
||||
$request_data = Json::encode($this->request->getParsedBody());
|
||||
$request_headers = Json::encode($this->request->getHeaders());
|
||||
} else {
|
||||
$is_debug = false;
|
||||
$request_data = null;
|
||||
$request_headers = null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
$request_data = Json::encode($this->request?->getParsedBody());
|
||||
$request_headers = Json::encode($this->request?->getHeaders());
|
||||
// 901 程序语法错误
|
||||
|
||||
// 902 SQL 语法错误
|
||||
if ($throwable instanceof QueryException) {
|
||||
if ($throwable->getCode() === '42S22') {
|
||||
$code = CommonErrorCode::PROGRAM_SQL_COLUMN_NOT_FOUND;
|
||||
} else {
|
||||
$code = CommonErrorCode::PROGRAM_SQL_ERROR;
|
||||
}
|
||||
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage(
|
||||
$is_testing
|
||||
? $code
|
||||
: CommonErrorCode::PROGRAM_SQL_ERROR
|
||||
),
|
||||
];
|
||||
$code = match ($throwable->getCode()) {
|
||||
'42S22' => CommonErrorCode::PROGRAM_SQL_COLUMN_NOT_FOUND,
|
||||
default => 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' => empty($this->request) ? null : $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) {
|
||||
@@ -200,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) {
|
||||
@@ -217,75 +141,46 @@ 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());
|
||||
$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()}
|
||||
{$this->request?->getMethod()}
|
||||
-------------------------------
|
||||
REQUEST_URL:
|
||||
{$this->request->getUri()}
|
||||
{$this->request?->getUri()}
|
||||
-------------------------------
|
||||
REQUEST_QUERY:
|
||||
{$this->request->getQueryString()}
|
||||
{$this->request?->getQueryString()}
|
||||
-------------------------------
|
||||
REQUEST_DATA:
|
||||
$request_data
|
||||
{$request_data}
|
||||
-------------------------------
|
||||
TRACE:
|
||||
{$throwable->getTraceAsString()}
|
||||
@@ -294,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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断该异常处理器是否要对该异常进行处理.
|
||||
*/
|
||||
|
||||
@@ -13,17 +13,13 @@ use Throwable;
|
||||
*/
|
||||
class Unauthorized extends HttpException
|
||||
{
|
||||
/** @var string $authenticationType */
|
||||
private string $authenticationType;
|
||||
|
||||
public function __construct(
|
||||
int $code = CommonErrorCode::UNAUTHORIZED,
|
||||
Throwable $previous = null,
|
||||
string $authenticationType = RFC7616::DIGEST
|
||||
private string $authenticationType = RFC7616::DIGEST
|
||||
) {
|
||||
$this->authenticationType = $authenticationType;
|
||||
if ($code == CommonErrorCode::UNAUTHORIZED) {
|
||||
$previous_code = empty($previous) ? null : $previous->getCode();
|
||||
$previous_code = $previous?->getCode();
|
||||
$code = empty($previous_code) ? $code : $previous_code;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,15 +12,6 @@ use Throwable;
|
||||
*/
|
||||
class ValidateException extends HttpException
|
||||
{
|
||||
/** @var string $field */
|
||||
private $field;
|
||||
|
||||
/** @var mixed $currentValue */
|
||||
private $currentValue;
|
||||
|
||||
/** @var array<int, mixed> $availableValue */
|
||||
private $availableValue;
|
||||
|
||||
/**
|
||||
* @param int $code
|
||||
* @param string|null $message
|
||||
@@ -32,16 +23,13 @@ class ValidateException extends HttpException
|
||||
public function __construct(
|
||||
int $code = CommonErrorCode::FORMATTER_ERROR,
|
||||
?string $message = null,
|
||||
string $field = '',
|
||||
$currentValue = null,
|
||||
array $availableValue = [],
|
||||
private string $field = '',
|
||||
private mixed $currentValue = null,
|
||||
private array $availableValue = [],
|
||||
Throwable $previous = null
|
||||
) {
|
||||
$this->availableValue = $availableValue;
|
||||
$this->currentValue = $currentValue;
|
||||
$this->field = $field;
|
||||
if ($code == CommonErrorCode::FORMATTER_ERROR) {
|
||||
$previous_code = empty($previous) ? null : $previous->getCode();
|
||||
$previous_code = $previous?->getCode();
|
||||
$code = empty($previous_code) ? $code : $previous_code;
|
||||
}
|
||||
parent::__construct(
|
||||
@@ -66,7 +54,7 @@ class ValidateException extends HttpException
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCurrentValue()
|
||||
public function getCurrentValue(): mixed
|
||||
{
|
||||
return $this->currentValue;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ClassicCoreMiddleware.php@hyperf-development-kit
|
||||
*
|
||||
@@ -31,89 +32,41 @@ use Singularity\HDK\Core\Service\UtilsService;
|
||||
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
|
||||
* @param null|array<string, mixed>|Arrayable|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,
|
||||
$paginator->url($paginator->lastPage()),
|
||||
'last'
|
||||
);
|
||||
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->url($paginator->lastPage()), 'last');
|
||||
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->url(1), 'first');
|
||||
$fact_response = $this->utilsService->extendLinkToHeader(
|
||||
$fact_response,
|
||||
$paginator->previousPageUrl(),
|
||||
'prev'
|
||||
);
|
||||
return $fact_response
|
||||
->withAddedHeader(Header::CONTENT_TYPE, 'application/json')
|
||||
->withBody(
|
||||
new SwooleStream(
|
||||
Json::encode([
|
||||
$code_name => 200,
|
||||
$message_name => 'ok',
|
||||
$data_name => $response->items(),
|
||||
'meta' => [
|
||||
'currentPage' => $paginator->currentPage(),
|
||||
'lastPage' => $paginator->lastPage(),
|
||||
'perPage' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
],
|
||||
])
|
||||
)
|
||||
);
|
||||
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->previousPageUrl(), 'prev');
|
||||
return $fact_response->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(new SwooleStream(Json::encode([$code_name => 200, $message_name => 'ok', $data_name => $response->items(), 'meta' => ['currentPage' => $paginator->currentPage(), 'lastPage' => $paginator->lastPage(), 'perPage' => $paginator->perPage(), 'total' => $paginator->total()]])));
|
||||
}
|
||||
// 可 Json 化的数据结构
|
||||
if ($response instanceof Jsonable && $response instanceof Arrayable) {
|
||||
$response = [
|
||||
$code_name => 200,
|
||||
$message_name => 'ok',
|
||||
$data_name => $response->toArray(),
|
||||
];
|
||||
return $this->response()
|
||||
->withAddedHeader(Header::CONTENT_TYPE, 'application/json')
|
||||
->withBody(new SwooleStream(Json::encode($response)));
|
||||
$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)));
|
||||
$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)
|
||||
);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,11 +22,10 @@ use Psr\Http\Server\RequestHandlerInterface;
|
||||
class InternationalizationMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -35,23 +34,15 @@ class InternationalizationMiddleware implements MiddlewareInterface
|
||||
$response = Context::get(ResponseInterface::class);
|
||||
$language = $request->getHeaderLine('Accept-Language');
|
||||
if (!empty($language)) {
|
||||
switch (true) {
|
||||
case in_array($language, ['en', 'en_us', 'en-us', 'en-uk', 'en_uk']):
|
||||
$language = 'en';
|
||||
break;
|
||||
case in_array($language, ['zh', 'zh_cn', 'zh-cn']):
|
||||
$language = 'zh_CN';
|
||||
break;
|
||||
default:
|
||||
$language = config('translation.locale') ;
|
||||
break;
|
||||
}
|
||||
$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'),
|
||||
};
|
||||
$this->translator->setLocale($language);
|
||||
|
||||
$response = $response->withAddedHeader('Content-Language', strtr($this->translator->getLocale(), '_', '-'));
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
}
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Singularity\HDK\Core\Service;
|
||||
|
||||
use JetBrains\PhpStorm\Deprecated;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\Mailer;
|
||||
use Symfony\Component\Mailer\Transport;
|
||||
@@ -53,9 +54,8 @@ class EmailService
|
||||
* @throws TransportExceptionInterface
|
||||
* @see EmailService::sendText
|
||||
* @see EmailService::sendHtml
|
||||
* @deprecated
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
#[Deprecated]
|
||||
public function sendCode(
|
||||
string $target,
|
||||
string $subject,
|
||||
@@ -74,7 +74,7 @@ class EmailService
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public function sendText(
|
||||
$target,
|
||||
string|array $target,
|
||||
string $subject,
|
||||
string $text,
|
||||
array $cc = []
|
||||
@@ -103,7 +103,7 @@ class EmailService
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public function sendHtml(
|
||||
$target,
|
||||
string|array $target,
|
||||
string $subject,
|
||||
string $html,
|
||||
array $cc = []
|
||||
|
||||
@@ -10,14 +10,6 @@ use Psr\Http\Message\ServerRequestInterface;
|
||||
*/
|
||||
class ExtendService
|
||||
{
|
||||
/** @var UtilsService $utils */
|
||||
private $utils;
|
||||
|
||||
public function __construct(UtilsService $utils)
|
||||
{
|
||||
$this->utils = $utils;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface|null $request
|
||||
* @param array<string, string>|null $params
|
||||
@@ -27,13 +19,11 @@ class ExtendService
|
||||
?ServerRequestInterface $request,
|
||||
?array $params = null
|
||||
): array {
|
||||
$params = $params ?? $request->getQueryParams();
|
||||
$params ??= $request->getQueryParams();
|
||||
$extends = $params['extends'] ?? null;
|
||||
if (!empty($extends)) {
|
||||
$extends = explode(',', $extends);
|
||||
$extends = array_map('trim', $extends);
|
||||
Context::set(self::class, $extends);
|
||||
return $extends;
|
||||
return Context::set(self::class, array_map('trim', $extends));
|
||||
}
|
||||
|
||||
return [];
|
||||
@@ -44,13 +34,7 @@ class ExtendService
|
||||
*/
|
||||
public function getExtends(): array
|
||||
{
|
||||
return Context::get(self::class);
|
||||
}
|
||||
|
||||
public function hasExtends(): bool
|
||||
{
|
||||
$extends = Context::get(self::class);
|
||||
return is_array($extends) && count($extends) > 0;
|
||||
return Context::get(self::class) ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,8 +44,8 @@ class ExtendService
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasExtend(string $field): bool
|
||||
public function hasExtends(string $field): bool
|
||||
{
|
||||
return $this->utils->inArray($field, Context::get(self::class));
|
||||
return Context::has(self::class) && isset(array_flip(Context::get(self::class))[$field]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,12 +13,6 @@ use Hyperf\Utils\Codec\Json;
|
||||
|
||||
class OssService
|
||||
{
|
||||
public ?string $accessKeyId = null;
|
||||
public ?string $accessKeySecret = null;
|
||||
public ?string $host = null;
|
||||
public ?string $callbackUrl = null;
|
||||
public ?int $expiration = 30;
|
||||
|
||||
/**
|
||||
* @param string|null $accessKeyId 不传则取配置文件
|
||||
* @param string|null $accessKeySecret 不传则取配置文件
|
||||
@@ -26,17 +20,16 @@ class OssService
|
||||
* @param int|null $expiration 过期时间,默认30s
|
||||
*/
|
||||
public function __construct(
|
||||
?string $accessKeyId = null,
|
||||
?string $accessKeySecret = null,
|
||||
?string $host = null,
|
||||
?string $callbackUrl = null,
|
||||
?int $expiration = 30
|
||||
public ?string $accessKeyId = null,
|
||||
public ?string $accessKeySecret = null,
|
||||
public ?string $host = null,
|
||||
public ?string $callbackUrl = null,
|
||||
public ?int $expiration = 30
|
||||
) {
|
||||
$this->expiration = $expiration;
|
||||
$this->callbackUrl = $callbackUrl?? config('oss_callback');
|
||||
$this->host = $host?? config('common.third_party.storage.oss.oss_host');
|
||||
$this->accessKeySecret = $accessKeySecret ?? config('common.third_party.storage.oss.access_key_secret');
|
||||
$this->accessKeyId = $accessKeyId ?? config('common.third_party.storage.oss.access_key_id');
|
||||
$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('common.third_party.storage.oss.oss_callback') ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,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'
|
||||
{
|
||||
@@ -109,7 +102,7 @@ class OssService
|
||||
"name": ${x:name},
|
||||
"hash": ${x:hash}
|
||||
}
|
||||
callbackBody : <<<'callbackBody'
|
||||
callbackBody: <<<'callbackBody'
|
||||
{
|
||||
"bucket": ${bucket},
|
||||
"object": ${object},
|
||||
@@ -122,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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,7 +10,6 @@ use Hyperf\HttpMessage\Exception\HttpException;
|
||||
use Hyperf\Utils\Codec\Json;
|
||||
use Singularity\HDK\Core\Constants\CommonErrorCode;
|
||||
use Singularity\HDK\Core\Exceptions\ValidateException;
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* 短信服务
|
||||
@@ -18,9 +17,9 @@ use UnexpectedValueException;
|
||||
class SmsService
|
||||
{
|
||||
/**
|
||||
* @var Dysmsapi
|
||||
* @var \AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi
|
||||
*/
|
||||
private $client;
|
||||
private Dysmsapi $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -30,7 +29,7 @@ class SmsService
|
||||
/**
|
||||
* 初始化实例
|
||||
*
|
||||
* @return Dysmsapi
|
||||
* @return \AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi
|
||||
*/
|
||||
private function createClient(): Dysmsapi
|
||||
{
|
||||
@@ -45,23 +44,22 @@ 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
|
||||
* @link https://help.aliyun.com/document_detail/419273.htm
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function sendSmsCountryside(
|
||||
string $phone,
|
||||
?string $templateCode = null,
|
||||
?array $templateParam = null,
|
||||
?string $signName = null
|
||||
?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, ['#' => '']);
|
||||
|
||||
@@ -91,20 +89,12 @@ class SmsService
|
||||
|
||||
// 频率太高
|
||||
if ($code === 'isv.BUSINESS_LIMIT_CONTROL') {
|
||||
switch (mb_substr($message, 2, 2)) {
|
||||
case '天级':
|
||||
$error_code = CommonErrorCode::REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_DAY;
|
||||
break;
|
||||
case '小时':
|
||||
$error_code = CommonErrorCode::REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_HOUR;
|
||||
break;
|
||||
case '分钟':
|
||||
$error_code = CommonErrorCode::REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_MINUTE;
|
||||
break;
|
||||
default:
|
||||
$error_code = null;
|
||||
break;
|
||||
}
|
||||
$error_code = match (mb_substr($message, 2, 2)) {
|
||||
'天级' => CommonErrorCode::REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_DAY,
|
||||
'小时' => CommonErrorCode::REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_HOUR,
|
||||
'分钟' => CommonErrorCode::REQUEST_PARAMS_ERROR_CODE_MAX_TIMES_MINUTE,
|
||||
default => null
|
||||
};
|
||||
|
||||
if (isset($error_code)) {
|
||||
throw new ValidateException($error_code);
|
||||
|
||||
@@ -8,6 +8,8 @@ use Closure;
|
||||
use Exception;
|
||||
use Generator;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
@@ -99,6 +101,7 @@ class UtilsService
|
||||
* @param bool $anchorQuery 开启后,query 参数将被尽量组合到锚点中,以支持 spa 路由
|
||||
* @return string
|
||||
*/
|
||||
#[Pure]
|
||||
public function buildUrl(string $url, array $moreQueries = [], bool $anchorQuery = false): string
|
||||
{
|
||||
if (count($moreQueries) === 0) {
|
||||
@@ -233,6 +236,7 @@ class UtilsService
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[NoReturn]
|
||||
public function unlimitedSubCategoriesQuicklyWithLevel(
|
||||
array &$list,
|
||||
int $level = 1,
|
||||
@@ -271,8 +275,9 @@ class UtilsService
|
||||
*
|
||||
* @return array{'size': float, 'unit': string}
|
||||
*/
|
||||
#[Pure]
|
||||
public function convertStorageSize(
|
||||
$size,
|
||||
float|int|string $size,
|
||||
bool $unitToUpper = false,
|
||||
int $precision = 2
|
||||
): array {
|
||||
@@ -300,8 +305,9 @@ class UtilsService
|
||||
*
|
||||
* @return array{'duration': float, 'unit': string}
|
||||
*/
|
||||
#[Pure]
|
||||
public function convertDuration(
|
||||
$duration,
|
||||
int|float $duration,
|
||||
bool $unitToUpper = false,
|
||||
bool $format = true,
|
||||
int $precision = 3
|
||||
@@ -322,16 +328,4 @@ class UtilsService
|
||||
'unit' => $unitToUpper ? strtoupper($unit[$i]) : $unit[$i],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 更快的判断数组元素方法
|
||||
* @param string|int|float $needle
|
||||
* @param array<int, mixed> $haystack
|
||||
* @return bool
|
||||
*/
|
||||
public function inArray($needle, array $haystack): bool
|
||||
{
|
||||
$list = array_flip($haystack);
|
||||
return isset($list[$needle]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,31 +9,24 @@
|
||||
|
||||
namespace Singularity\HDK\Test\Core\Unit;
|
||||
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Singularity\HDK\Core\Service\ExtendService;
|
||||
use Singularity\HDK\Core\Service\UtilsService;
|
||||
|
||||
/** @var ExtendService $service */
|
||||
$service = make(ExtendService::class, ['utils' => new UtilsService()]);
|
||||
$service = new ExtendService();
|
||||
|
||||
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())->toBeTrue();
|
||||
});
|
||||
|
||||
it('asserts has specify extend', function () use ($service) {
|
||||
expect($service->hasExtend('a'))->toBeTrue()
|
||||
->and($service->hasExtend('b'))->toBeTrue()
|
||||
->and($service->hasExtend('c'))->toBeFalse();
|
||||
expect($service->hasExtends('a'))->toBeTrue()
|
||||
->and($service->hasExtends('b'))->toBeTrue()
|
||||
->and($service->hasExtends('c'))->toBeFalse();
|
||||
})->depends('it asserts query parameters can be parsed.');
|
||||
|
||||
it('asserts parsed extends', function () use ($service) {
|
||||
|
||||
@@ -10,11 +10,10 @@
|
||||
use Hyperf\Utils\Codec\Json;
|
||||
use Singularity\HDK\Core\Service\OssService;
|
||||
|
||||
// use function Spatie\PestPluginTestTime\testTime;
|
||||
use function Spatie\PestPluginTestTime\testTime;
|
||||
|
||||
it('asserts oss policy can be generated', function ($setDir, $isImage, $maxSize) {
|
||||
$time = time();
|
||||
// testTime()->freeze();
|
||||
testTime()->freeze();
|
||||
$oss = new OssService(
|
||||
'{MOCK_ACCESS_KEY_ID}',
|
||||
'{MOCK_ACCESS_KEY_SECRET}',
|
||||
@@ -41,8 +40,8 @@ it('asserts oss policy can be generated', function ($setDir, $isImage, $maxSize)
|
||||
->and($host)->toBe('{MOCK_HOST}')
|
||||
->and($dir)->toBe($setDir)
|
||||
->and($setDir)->toBe($policy['conditions'][1][2])
|
||||
->and($expire)->toBe($time + 30)
|
||||
// ->and($policy['expiration'])->toBe(testTime()->setMilli(0)->toIso8601ZuluString('m'))
|
||||
->and($expire)->toBe(testTime()->addSeconds(30)->unix())
|
||||
->and($policy['expiration'])->toBe(testTime()->setMilli(0)->toIso8601ZuluString('m'))
|
||||
->and($maxSize)->toBe($policy['conditions'][0][2])
|
||||
->and($callback['callbackUrl'])->toBe('{MOCK_CALLBACK_URL}')
|
||||
->and($signature)->toBe($sign);
|
||||
|
||||
@@ -29,7 +29,7 @@ test('断言验证码可以正常生成指定长度', function (int $length) use
|
||||
})->with($length_data)->group('pure', 'utils');
|
||||
|
||||
test('断言可以根据参数构建 URL', function (string $url, array $params, bool $anchorQuery, string $expect) use ($utils) {
|
||||
$url = $utils->buildUrl($url, $params, $anchorQuery);
|
||||
$url = $utils->buildUrl(url: $url, moreQueries: $params, anchorQuery: $anchorQuery);
|
||||
expect($url)->toBe($expect);
|
||||
})->with([
|
||||
['baidu.com/list', ['a' => 'b'], false, 'baidu.com/list?a=b'],
|
||||
@@ -58,24 +58,5 @@ test('断言可以根据参数构建 URL', function (string $url, array $params,
|
||||
['c' => 'd'],
|
||||
true,
|
||||
'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');
|
||||
|
||||
Reference in New Issue
Block a user