| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace App\Module\OpenAPI\Handlers\Transfer;
- use App\Module\OpenAPI\Handlers\BaseHandler;
- use App\Module\OpenAPI\Enums\SCOPE_TYPE;
- use App\Module\Transfer\Services\TransferService;
- use Illuminate\Http\JsonResponse;
- /**
- * 转账查询处理器
- * 处理查询划转订单状态的请求
- */
- class TransferQueryHandler extends BaseHandler
- {
- /**
- * 获取所需权限范围
- *
- * @return array
- */
- public function getRequiredScopes(): array
- {
- return [SCOPE_TYPE::TRANSFER_QUERY];
- }
- /**
- * 处理查询请求
- *
- * @param array $data 请求数据
- * @param array $context 上下文信息
- * @return JsonResponse
- */
- public function handle(array $data, array $context = []): JsonResponse
- {
- try {
- // 验证权限
- if (!$this->validatePermissions($context['app']->scopes ?? [], $context)) {
- return $this->errorResponse('权限不足', null, 403);
- }
- // 获取查询参数
- $businessId = $data['business_id'] ?? null;
- $orderId = $data['order_id'] ?? null;
- $appId = $context['app']->id ?? null;
- if (!$businessId && !$orderId) {
- return $this->errorResponse('缺少查询参数', [
- 'message' => '必须提供 business_id 或 order_id 其中之一'
- ], 400);
- }
- if (!$appId) {
- return $this->errorResponse('应用ID缺失', null, 400);
- }
- // 根据不同参数查询订单
- $order = null;
- if ($businessId) {
- // 根据业务ID查询
- $order = TransferService::getOrderByOutId($businessId, $appId);
- } elseif ($orderId) {
- // 根据订单ID查询
- $orderDto = TransferService::getOrderInfo($orderId);
- // 验证订单是否属于当前应用
- if ($orderDto && $orderDto->out_id == $appId) {
- $order = $orderDto;
- }
- }
- if (!$order) {
- return $this->errorResponse('订单不存在', null, 404);
- }
- // 记录操作日志
- $this->logAction('transfer.query', [
- 'order_id' => $order->id,
- 'business_id' => $order->out_order_id,
- 'query_type' => $businessId ? 'business_id' : 'order_id'
- ], $context);
- // 返回订单信息
- return $this->successResponse('查询成功', [
- 'order_id' => $order->id,
- 'business_id' => $order->out_order_id,
- 'type' => $order->type->value,
- 'type_text' => $order->type->getDescription(),
- 'status' => $order->status->value,
- 'status_text' => $order->status->getDescription(),
- 'amount' => $order->amount,
- 'out_amount' => $order->out_amount,
- 'exchange_rate' => $order->exchange_rate,
- 'user_id' => $order->user_id,
- 'out_user_id' => $order->out_user_id,
- 'remark' => $order->remark,
- 'callback_data' => $order->callback_data,
- 'error_message' => $order->error_message,
- 'created_at' => $order->created_at->toISOString(),
- 'processed_at' => $order->processed_at?->toISOString(),
- 'completed_at' => $order->completed_at?->toISOString(),
- ]);
- } catch (\Exception $e) {
- // 记录错误日志
- $this->logError('transfer.query.error', $e, $context);
- return $this->errorResponse('查询失败', [
- 'error' => $e->getMessage()
- ], 500);
- }
- }
- /**
- * 获取Handler描述
- *
- * @return string
- */
- public function getDescription(): string
- {
- return '查询划转订单状态和详细信息';
- }
- /**
- * 获取请求参数说明
- *
- * @return array
- */
- public function getRequestParameters(): array
- {
- return [
- 'business_id' => [
- 'type' => 'string',
- 'required' => false,
- 'description' => '外部业务订单ID(与order_id二选一)'
- ],
- 'order_id' => [
- 'type' => 'integer',
- 'required' => false,
- 'description' => '内部订单ID(与business_id二选一)'
- ]
- ];
- }
- /**
- * 获取响应参数说明
- *
- * @return array
- */
- public function getResponseParameters(): array
- {
- return [
- 'order_id' => [
- 'type' => 'integer',
- 'description' => '内部订单ID'
- ],
- 'business_id' => [
- 'type' => 'string',
- 'description' => '外部业务订单ID'
- ],
- 'type' => [
- 'type' => 'integer',
- 'description' => '订单类型(1=转入,2=转出)'
- ],
- 'type_text' => [
- 'type' => 'string',
- 'description' => '订单类型描述'
- ],
- 'status' => [
- 'type' => 'integer',
- 'description' => '订单状态值'
- ],
- 'status_text' => [
- 'type' => 'string',
- 'description' => '订单状态描述'
- ],
- 'amount' => [
- 'type' => 'string',
- 'description' => '内部金额'
- ],
- 'out_amount' => [
- 'type' => 'string',
- 'description' => '外部金额'
- ],
- 'exchange_rate' => [
- 'type' => 'string',
- 'description' => '汇率'
- ],
- 'user_id' => [
- 'type' => 'integer',
- 'description' => '用户ID'
- ],
- 'out_user_id' => [
- 'type' => 'string',
- 'description' => '外部用户ID'
- ],
- 'remark' => [
- 'type' => 'string',
- 'description' => '备注信息'
- ],
- 'callback_data' => [
- 'type' => 'object',
- 'description' => '回调数据'
- ],
- 'error_message' => [
- 'type' => 'string',
- 'description' => '错误信息(如有)'
- ],
- 'created_at' => [
- 'type' => 'string',
- 'description' => '创建时间'
- ],
- 'processed_at' => [
- 'type' => 'string',
- 'description' => '处理时间'
- ],
- 'completed_at' => [
- 'type' => 'string',
- 'description' => '完成时间'
- ]
- ];
- }
- }
|