From f4b812f8612ec9651137965c803c9174f2ef9ad8 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Thu, 4 Sep 2025 23:08:54 +0800 Subject: [PATCH] feat : pay Adapter --- README.md => BLADE.README.md | 0 README | 87 +++++++++++++++++++ app/Aspect/CommonReturnAspect.php | 77 ++++++++++++++++ app/Command/ServiceCommand.php | 49 +++++++++++ app/Command/stubs/service.stub | 20 +++++ app/Constants/ReturnCode.php | 30 +++++++ app/Controller/Test/AdapterTestController.php | 25 ++++++ app/Interface/Test/Adapter/PaymentGateway.php | 8 ++ app/Lib/Return/AdminReturn.php | 15 ++++ app/Lib/Return/CommonReturn.php | 53 +++++++++++ app/Lib/Return/TestReturn.php | 15 ++++ app/Middleware/CoreMiddleware.php | 23 +++++ app/Middleware/CorsMiddleware.php | 23 +++++ app/Service/BaseService.php | 20 +++++ app/Service/Test/Adapter/CacheService.php | 22 +++++ .../Test/Adapter/Pay/AlipayAdapter.php | 36 ++++++++ .../Test/Adapter/Pay/AlipayService.php | 27 ++++++ .../Test/Adapter/Pay/WechatPayAdapter.php | 36 ++++++++ .../Test/Adapter/Pay/WechatPayService.php | 28 ++++++ app/Service/Test/Adapter/PayService.php | 39 +++++++++ app/Service/Test/AdapterTestService.php | 22 +++++ app/Service/Test/TestBaseService.php | 18 ++++ config/config.php | 2 +- request/http-client.env.json | 11 +++ request/test.http | 9 ++ 25 files changed, 694 insertions(+), 1 deletion(-) rename README.md => BLADE.README.md (100%) create mode 100644 README create mode 100644 app/Aspect/CommonReturnAspect.php create mode 100644 app/Command/ServiceCommand.php create mode 100644 app/Command/stubs/service.stub create mode 100644 app/Constants/ReturnCode.php create mode 100644 app/Controller/Test/AdapterTestController.php create mode 100644 app/Interface/Test/Adapter/PaymentGateway.php create mode 100644 app/Lib/Return/AdminReturn.php create mode 100644 app/Lib/Return/CommonReturn.php create mode 100644 app/Lib/Return/TestReturn.php create mode 100644 app/Middleware/CoreMiddleware.php create mode 100644 app/Middleware/CorsMiddleware.php create mode 100644 app/Service/BaseService.php create mode 100644 app/Service/Test/Adapter/CacheService.php create mode 100644 app/Service/Test/Adapter/Pay/AlipayAdapter.php create mode 100644 app/Service/Test/Adapter/Pay/AlipayService.php create mode 100644 app/Service/Test/Adapter/Pay/WechatPayAdapter.php create mode 100644 app/Service/Test/Adapter/Pay/WechatPayService.php create mode 100644 app/Service/Test/Adapter/PayService.php create mode 100644 app/Service/Test/AdapterTestService.php create mode 100644 app/Service/Test/TestBaseService.php create mode 100644 request/http-client.env.json create mode 100644 request/test.http diff --git a/README.md b/BLADE.README.md similarity index 100% rename from README.md rename to BLADE.README.md diff --git a/README b/README new file mode 100644 index 0000000..9adc024 --- /dev/null +++ b/README @@ -0,0 +1,87 @@ +## 仓库 + +- [hhl_hyperf_service](https://codeup.aliyun.com/67039465d8d1ada68263f984/hhl/rewrite/hyperf_service.git) - git远程仓库地址 + +## 特性 + +- **最新技术栈**:使用 PHP8.3/hyperf3.1/swoole5.1.4 等后端前沿技术开发 + +## 文档 + +[文档地址 Github](https://hyperf.wiki/3.1/) + +## 前序准备 + +- [php8.3](https://www.php.net/) 和 [git](https://git-scm.com/) - 项目开发环境 +- [swoole](https://www.swoole.com/) - 熟悉 swoole 特性 +- [php8.3](https://www.php.net/) - 熟悉 php 基础语法 +- [hyperf](https://hyperf.wiki/3.1/) - 熟悉 `hyperf` 基本语法 + +## 安装和使用 + +- 安装 swoole 和 xlswriter 扩展 + +``` +自行搜索安装教程 +``` + +- 获取代码 + +```bash +git clone https://git.ctexthuang.com/ctexthuang/hyperf_test.git + +mkdir uploads +``` + +- vendor + +```bash +composer install +``` + +- 运行 + +```bash +cp .env.example .env + +vim .env + +php bin/hyperf.php start +``` + +- command 函数 +```bash +#框架自有 +php bin/hyperf.php gen:controller LoginController +php bin/hyperf.php gen:model UserModel +php bin/hyperf.php gen:request LoginRequest +php bin/hyperf.php gen:command TestCommand +php bin/hyperf.php gen:job TestJob +php bin/hyperf.php gen:listener TestListener +php bin/hyperf.php gen:middleware AuthMiddleware +php bin/hyperf.php gen:amqp-consumer DemoConsumer +php bin/hyperf.php gen:amqp-producer DemoProducer + +#新增命令 +php bin/hyperf.php gen:service LoginService +php bin/hyperf.php gen:cron OssTask +php bin/hyperf.php gen:event TestEvent +``` + +## Git 贡献提交规范 + +- `feat` 新功能 +- `fix` 修补 bug +- `docs` 文档 +- `style` 格式、样式(不影响代码运行的变动) +- `refactor` 重构(即不是新增功能,也不是修改 BUG 的代码) +- `perf` 优化相关,比如提升性能、体验 +- `test` 添加测试 +- `build` 编译相关的修改,对项目构建或者依赖的改动 +- `ci` 持续集成修改 +- `chore` 构建过程或辅助工具的变动 +- `revert` 回滚到上一个版本 +- `workflow` 工作流改进 +- `mod` 不确定分类的修改 +- `wip` 开发中 +- `types` 类型 \ No newline at end of file diff --git a/app/Aspect/CommonReturnAspect.php b/app/Aspect/CommonReturnAspect.php new file mode 100644 index 0000000..2a890db --- /dev/null +++ b/app/Aspect/CommonReturnAspect.php @@ -0,0 +1,77 @@ +process(); + // 在调用后进行处理 + + // 未登录返回 0 + $this->userId = Context::get('user_id',0); + + $this->writeResponseLog(json_encode($result)); + + return $result; + } + + /** + * 写入请求日志 + * @param string $content + * @return void + */ + private function writeResponseLog(string $content): void + { + echo json_encode($this->request->all()); + echo $this->userId; + echo json_encode($content); + } +} \ No newline at end of file diff --git a/app/Command/ServiceCommand.php b/app/Command/ServiceCommand.php new file mode 100644 index 0000000..7fe0e6a --- /dev/null +++ b/app/Command/ServiceCommand.php @@ -0,0 +1,49 @@ +setDescription('Create a new service class'); + + parent::configure(); + } +} \ No newline at end of file diff --git a/app/Command/stubs/service.stub b/app/Command/stubs/service.stub new file mode 100644 index 0000000..547fe5f --- /dev/null +++ b/app/Command/stubs/service.stub @@ -0,0 +1,20 @@ +handle(); + } + + public function cache() + { + + } +} diff --git a/app/Interface/Test/Adapter/PaymentGateway.php b/app/Interface/Test/Adapter/PaymentGateway.php new file mode 100644 index 0000000..1237322 --- /dev/null +++ b/app/Interface/Test/Adapter/PaymentGateway.php @@ -0,0 +1,8 @@ + $code, + 'message' => $msg, + 'data' => $data + ]; + + return $this->afterSuccess(array_merge($res, $debug)); + } + + /** + * 通用 fail 返回 + * @param string $msg + * @param array $data + * @param ReturnCode $code + * @param array $debug + * @return array + */ + final public function error(string $msg = 'error', ReturnCode $code = ReturnCode::ERROR, array $data = [], array $debug = []): array + { + $res = [ + 'code' => $code, + 'message' => $msg, + 'data' => $data + ]; + + return $this->afterSuccess(array_merge($res, $debug)); + } + + /** + * 通用类调子类返回方便切面类识别 + * @param array $res + * @return array + */ + abstract protected function afterSuccess(array $res): array; +} \ No newline at end of file diff --git a/app/Lib/Return/TestReturn.php b/app/Lib/Return/TestReturn.php new file mode 100644 index 0000000..2e25ee3 --- /dev/null +++ b/app/Lib/Return/TestReturn.php @@ -0,0 +1,15 @@ +handle($request); + } +} diff --git a/app/Middleware/CorsMiddleware.php b/app/Middleware/CorsMiddleware.php new file mode 100644 index 0000000..05a0f8c --- /dev/null +++ b/app/Middleware/CorsMiddleware.php @@ -0,0 +1,23 @@ +handle($request); + } +} diff --git a/app/Service/BaseService.php b/app/Service/BaseService.php new file mode 100644 index 0000000..5b9d5a8 --- /dev/null +++ b/app/Service/BaseService.php @@ -0,0 +1,20 @@ +alipay = $alipay; + } + + /** + * @param float $amount + * @return array + */ + public function pay(float $amount): array + { + return $this->alipay->sendPayment($amount); + } +} \ No newline at end of file diff --git a/app/Service/Test/Adapter/Pay/AlipayService.php b/app/Service/Test/Adapter/Pay/AlipayService.php new file mode 100644 index 0000000..138d292 --- /dev/null +++ b/app/Service/Test/Adapter/Pay/AlipayService.php @@ -0,0 +1,27 @@ + 'success', + 'amount' => $amount, + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Adapter/Pay/WechatPayAdapter.php b/app/Service/Test/Adapter/Pay/WechatPayAdapter.php new file mode 100644 index 0000000..428b6a2 --- /dev/null +++ b/app/Service/Test/Adapter/Pay/WechatPayAdapter.php @@ -0,0 +1,36 @@ +wechatPay = $wechatPay; + } + + /** + * @param float $amount + * @return array + */ + public function pay(float $amount): array + { + return $this->wechatPay->makePayment($amount); + } +} \ No newline at end of file diff --git a/app/Service/Test/Adapter/Pay/WechatPayService.php b/app/Service/Test/Adapter/Pay/WechatPayService.php new file mode 100644 index 0000000..f2d7465 --- /dev/null +++ b/app/Service/Test/Adapter/Pay/WechatPayService.php @@ -0,0 +1,28 @@ + 'error', + 'status' => 'fail', + 'amount' => $amount, + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Adapter/PayService.php b/app/Service/Test/Adapter/PayService.php new file mode 100644 index 0000000..7376add --- /dev/null +++ b/app/Service/Test/Adapter/PayService.php @@ -0,0 +1,39 @@ +pay(100.00); + echo 'alipayRes:'. json_encode($alipayRes).PHP_EOL; + + $wechatPay = new WechatPayService(); + $wechatPayAdapter = new WechatPayAdapter($wechatPay); + $wechatPayRes = $wechatPayAdapter->pay(100.00); + + echo 'wechatPayRes:'. json_encode($wechatPayRes).PHP_EOL; + + return $this->return->success('success',[ + 'alipayRes' => $alipayRes, + 'wechatPayRes' => $wechatPayRes, + ]); + } +} \ No newline at end of file diff --git a/app/Service/Test/AdapterTestService.php b/app/Service/Test/AdapterTestService.php new file mode 100644 index 0000000..719af68 --- /dev/null +++ b/app/Service/Test/AdapterTestService.php @@ -0,0 +1,22 @@ +return->success(); + } +} \ No newline at end of file diff --git a/app/Service/Test/TestBaseService.php b/app/Service/Test/TestBaseService.php new file mode 100644 index 0000000..f6aeb8f --- /dev/null +++ b/app/Service/Test/TestBaseService.php @@ -0,0 +1,18 @@ + [ LogLevel::ALERT, LogLevel::CRITICAL, - LogLevel::DEBUG, +// LogLevel::DEBUG, LogLevel::EMERGENCY, LogLevel::ERROR, LogLevel::INFO, diff --git a/request/http-client.env.json b/request/http-client.env.json new file mode 100644 index 0000000..693b998 --- /dev/null +++ b/request/http-client.env.json @@ -0,0 +1,11 @@ +{ + "dev": { + "host": "https://test.ctexthuang.com" + }, + "local": { + "host": "http://127.0.0.1:9501" + }, + "prod": { + "host": "https://prod.ctexthuang.com" + } +} \ No newline at end of file diff --git a/request/test.http b/request/test.http new file mode 100644 index 0000000..3c8f900 --- /dev/null +++ b/request/test.http @@ -0,0 +1,9 @@ +### Adapte pay test +GET {{host}}/adapter/test/pay +Content-Type: application/x-www-form-urlencoded + +account=13632877014&password=123456 + +> {% + client.global.set("admin_token", response.body.data.token); +%}