validatePermissions($context['app']->scopes ?? [], $context)) { return $this->errorResponse('权限不足', null, 403); } // 添加应用ID到数据中 $data['transfer_app_id'] = $context['app']->id ?? null; if (!$data['transfer_app_id']) { return $this->errorResponse('应用ID缺失', null, 400); } // 验证请求数据 $validation = new TransferOutValidation($data); if (!$validation->validate()) { return $this->errorResponse('数据验证失败', $validation->getErrors(), 422); } // 获取验证后的数据 $validatedData = $validation->getValidatedData(); // 调用Transfer服务创建转出订单 $result = TransferService::createTransferOut($validatedData); if (is_string($result)) { // 返回错误信息 return $this->errorResponse($result, null, 400); } // 记录操作日志 $this->logAction('transfer.out.create', [ 'order_id' => $result->id, 'business_id' => $result->out_order_id, 'amount' => $result->amount, 'user_id' => $result->user_id ], $context); // 返回成功响应 return $this->successResponse('转出订单创建成功', [ 'order_id' => $result->id, 'business_id' => $result->out_order_id, 'amount' => $result->amount, 'out_amount' => $result->out_amount, 'exchange_rate' => $result->exchange_rate, 'status' => $result->status->value, 'status_text' => $result->status->getDescription(), 'created_at' => $result->created_at->toISOString() ]); } catch (\Exception $e) { // 记录错误日志 $this->logError('transfer.out.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' => true, 'description' => '外部业务订单ID,用于防重复提交' ], 'user_id' => [ 'type' => 'integer', 'required' => true, 'description' => '转出用户ID' ], 'amount' => [ 'type' => 'string', 'required' => true, 'description' => '转出金额(内部金额)' ], 'out_user_id' => [ 'type' => 'string', 'required' => false, 'description' => '外部用户ID(可选)' ], 'remark' => [ 'type' => 'string', 'required' => false, 'description' => '备注信息' ], 'callback_data' => [ 'type' => 'object', 'required' => false, 'description' => '回调数据,将在回调时原样返回' ] ]; } /** * 获取响应参数说明 * * @return array */ public function getResponseParameters(): array { return [ 'order_id' => [ 'type' => 'integer', 'description' => '内部订单ID' ], 'business_id' => [ 'type' => 'string', 'description' => '外部业务订单ID' ], 'amount' => [ 'type' => 'string', 'description' => '内部金额' ], 'out_amount' => [ 'type' => 'string', 'description' => '外部金额(按汇率转换后)' ], 'exchange_rate' => [ 'type' => 'string', 'description' => '使用的汇率' ], 'status' => [ 'type' => 'integer', 'description' => '订单状态值' ], 'status_text' => [ 'type' => 'string', 'description' => '订单状态描述' ], 'created_at' => [ 'type' => 'string', 'description' => '创建时间(ISO格式)' ] ]; } }