|
|
6 months ago | |
|---|---|---|
| .. | ||
| Urs | 6 months ago | |
| README.md | 6 months ago | |
此目录用于存放第三方服务对接包,每个包都遵循ThirdParty模块的标准化规范。
ThirdParty/
├── README.md # 本说明文件
├── Urs/ # URS包示例(已重构)
│ ├── Request/ # 请求类目录
│ │ ├── UrsGetUserInfoRequest.php # 获取用户信息请求类
│ │ ├── UrsGetUserTeamRequest.php # 获取用户团队关系请求类
│ │ ├── UrsGetUserLevelCountRequest.php # 获取用户下级统计请求类
│ │ ├── UrsRegisterRequest.php # 用户注册请求类
│ │ ├── UrsDepositRequest.php # 充值请求类
│ │ ├── UrsWithdrawRequest.php # 提取请求类
│ │ └── UrsCheckBalanceRequest.php # 余额检查请求类
│ ├── Services/ # 服务类目录
│ │ └── UrsService.php # URS统一服务类
│ ├── Webhook/ # Webhook处理器目录
│ │ ├── UrsRegisterWebhook.php # 注册通知处理器
│ │ ├── UrsDepositWebhook.php # 充值通知处理器
│ │ ├── UrsWithdrawWebhook.php # 提取通知处理器
│ │ └── UrsCheckWebhook.php # 余额检查处理器
│ ├── Util/ # 工具类目录
│ │ └── CryptoService.php # 加密服务类
│ ├── Docs/ # 文档目录
│ └── UrsServiceProvider.php # URS服务提供者
└── [其他包]/ # 其他第三方服务包
重要更新:ThirdParty模块的Request机制已重构,遵循"一个Request类只完成一种请求"的设计原则。
ThirdParty\{包名} 命名空间Urs、Alipay、Wechat每个包至少包含以下文件:
Request/)App\Module\ThirdParty\Services\BaseRequesthandler(array $params): array 方法{包名}{操作名}Request.phpServices/{包名}Service.php){包名}Webhook.php)App\Module\ThirdParty\Services\BaseWebhookhandler(string $action, Request $request): array 方法{包名}ServiceProvider.php)Illuminate\Support\ServiceProviderboot() 方法中注册Webhook处理器thirdparty_services 表中注册服务配置config 字段中(JSON格式)<?php
namespace ThirdParty\YourPackage\Request;
use App\Module\ThirdParty\Services\BaseRequest;
// 专门处理用户信息获取的请求类
class YourPackageGetUserRequest extends BaseRequest
{
public function __construct()
{
parent::__construct('your_package'); // 服务代码
}
protected function handler(array $params): array
{
// 验证必需参数
if (empty($params['user_id'])) {
throw new \Exception('user_id参数是必填的');
}
// 获取配置
$config = $this->getConfig();
// 实现具体的用户信息获取逻辑
// ... 业务逻辑
return $result;
}
}
// 专门处理用户注册的请求类
class YourPackageRegisterRequest extends BaseRequest
{
public function __construct()
{
parent::__construct('your_package');
}
protected function handler(array $params): array
{
// 验证必需参数
if (empty($params['username'])) {
throw new \Exception('username参数是必填的');
}
// 实现具体的用户注册逻辑
// ... 业务逻辑
return $result;
}
}
<?php
namespace ThirdParty\YourPackage\Services;
use ThirdParty\YourPackage\Request\YourPackageGetUserRequest;
use ThirdParty\YourPackage\Request\YourPackageRegisterRequest;
class YourPackageService
{
/**
* 获取用户信息
*/
public static function getUser(int $userId): array
{
$request = new YourPackageGetUserRequest();
return $request->request(['user_id' => $userId]);
}
/**
* 注册用户
*/
public static function registerUser(string $username): array
{
$request = new YourPackageRegisterRequest();
return $request->request(['username' => $username]);
}
}
#### 创建Webhook处理器
php <?php namespace ThirdParty\YourPackage; use App\Module\ThirdParty\Services\BaseWebhook; use Illuminate\Http\Request;
class YourPackageWebhook extends BaseWebhook {
public function __construct(Request $request)
{
parent::__construct('your_package', $request);
}
protected function handler(string $action, Request $request): array
{
// 实现具体的Webhook处理逻辑
switch ($action) {
case 'notify':
return $this->handleNotify($request);
// ... 其他操作
}
}
}
#### 注册服务提供者
php <?php namespace ThirdParty\YourPackage; use Illuminate\Support\ServiceProvider; use App\Module\ThirdParty\Services\WebhookDispatchService;
class YourPackageServiceProvider extends ServiceProvider {
public function boot()
{
WebhookDispatchService::registerPackageHandlers('your_package', [
'notify' => YourPackageWebhook::class,
// ... 其他处理器
]);
}
}
## 注册步骤
### 1. 在composer.json中注册命名空间
json {
"autoload": {
"psr-4": {
"ThirdParty\\YourPackage\\": "ThirdParty/YourPackage/"
}
}
}
### 2. 在config/app.php中注册服务提供者
php 'providers' => [
// ...
ThirdParty\YourPackage\YourPackageServiceProvider::class,
],
### 3. 在数据库中注册服务配置
sql
INSERT INTO kku_thirdparty_services (
`name`, `code`, `type`, `provider`, `description`,
`base_url`, `auth_type`, `status`, `config`
) VALUES (
'您的服务名称', 'your_package', 'CUSTOM', 'YOUR_PROVIDER', '服务描述',
'https://api.yourservice.com', 'API_KEY', 'ACTIVE',
JSON_OBJECT(
'api_url', 'https://api.yourservice.com',
'app_id', 'your_app_id',
'app_secret', 'your_app_secret'
)
);
## 基类功能
### BaseRequest 提供的功能
- ✅ 自动配置读取
- ✅ 配额检查和更新
- ✅ 请求日志记录
- ✅ 错误处理
- ✅ 凭证管理
### BaseWebhook 提供的功能
- ✅ 签名验证
- ✅ 请求格式验证
- ✅ Webhook日志记录
- ✅ 错误处理和响应
- ✅ 配置访问
## Webhook路由
所有Webhook请求都通过以下路由格式访问:
POST /thirdParty/webhook/{包名}/{处理器路由} ```
例如:
/thirdParty/webhook/urs/register - URS注册通知/thirdParty/webhook/alipay/notify - 支付宝支付通知/thirdParty/webhook/wechat/callback - 微信回调所有包的请求和Webhook处理都会自动记录到 thirdparty_logs 表中,可以通过后台管理界面查看:
如有问题,请参考:
app/Module/ThirdParty/Docs/基础架构使用示例.md - 详细使用示例app/Module/ThirdParty/Docs/第三方包.md - 规范说明app/Module/ThirdParty/README.md - 模块文档