TransferQueryHandler.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Module\OpenAPI\Handlers\Transfer;
  3. use App\Module\OpenAPI\Handlers\BaseHandler;
  4. use App\Module\OpenAPI\Enums\SCOPE_TYPE;
  5. use App\Module\Transfer\Services\TransferService;
  6. use Illuminate\Http\JsonResponse;
  7. /**
  8. * 转账查询处理器
  9. * 处理查询划转订单状态的请求
  10. */
  11. class TransferQueryHandler extends BaseHandler
  12. {
  13. /**
  14. * 获取所需权限范围
  15. *
  16. * @return array
  17. */
  18. public function getRequiredScopes(): array
  19. {
  20. return [SCOPE_TYPE::TRANSFER_QUERY];
  21. }
  22. /**
  23. * 处理查询请求
  24. *
  25. * @param array $data 请求数据
  26. * @param array $context 上下文信息
  27. * @return JsonResponse
  28. */
  29. public function handle(array $data, array $context = []): JsonResponse
  30. {
  31. try {
  32. // 验证权限
  33. if (!$this->validatePermissions($context['app']->scopes ?? [], $context)) {
  34. return $this->errorResponse('权限不足', null, 403);
  35. }
  36. // 获取查询参数
  37. $businessId = $data['business_id'] ?? null;
  38. $orderId = $data['order_id'] ?? null;
  39. $appId = $context['app']->id ?? null;
  40. if (!$businessId && !$orderId) {
  41. return $this->errorResponse('缺少查询参数', [
  42. 'message' => '必须提供 business_id 或 order_id 其中之一'
  43. ], 400);
  44. }
  45. if (!$appId) {
  46. return $this->errorResponse('应用ID缺失', null, 400);
  47. }
  48. // 根据不同参数查询订单
  49. $order = null;
  50. if ($businessId) {
  51. // 根据业务ID查询
  52. $order = TransferService::getOrderByOutId($businessId, $appId);
  53. } elseif ($orderId) {
  54. // 根据订单ID查询
  55. $orderDto = TransferService::getOrderInfo($orderId);
  56. // 验证订单是否属于当前应用
  57. if ($orderDto && $orderDto->out_id == $appId) {
  58. $order = $orderDto;
  59. }
  60. }
  61. if (!$order) {
  62. return $this->errorResponse('订单不存在', null, 404);
  63. }
  64. // 记录操作日志
  65. $this->logAction('transfer.query', [
  66. 'order_id' => $order->id,
  67. 'business_id' => $order->out_order_id,
  68. 'query_type' => $businessId ? 'business_id' : 'order_id'
  69. ], $context);
  70. // 返回订单信息
  71. return $this->successResponse('查询成功', [
  72. 'order_id' => $order->id,
  73. 'business_id' => $order->out_order_id,
  74. 'type' => $order->type->value,
  75. 'type_text' => $order->type->getDescription(),
  76. 'status' => $order->status->value,
  77. 'status_text' => $order->status->getDescription(),
  78. 'amount' => $order->amount,
  79. 'out_amount' => $order->out_amount,
  80. 'exchange_rate' => $order->exchange_rate,
  81. 'user_id' => $order->user_id,
  82. 'out_user_id' => $order->out_user_id,
  83. 'remark' => $order->remark,
  84. 'callback_data' => $order->callback_data,
  85. 'error_message' => $order->error_message,
  86. 'created_at' => $order->created_at->toISOString(),
  87. 'processed_at' => $order->processed_at?->toISOString(),
  88. 'completed_at' => $order->completed_at?->toISOString(),
  89. ]);
  90. } catch (\Exception $e) {
  91. // 记录错误日志
  92. $this->logError('transfer.query.error', $e, $context);
  93. return $this->errorResponse('查询失败', [
  94. 'error' => $e->getMessage()
  95. ], 500);
  96. }
  97. }
  98. /**
  99. * 获取Handler描述
  100. *
  101. * @return string
  102. */
  103. public function getDescription(): string
  104. {
  105. return '查询划转订单状态和详细信息';
  106. }
  107. /**
  108. * 获取请求参数说明
  109. *
  110. * @return array
  111. */
  112. public function getRequestParameters(): array
  113. {
  114. return [
  115. 'business_id' => [
  116. 'type' => 'string',
  117. 'required' => false,
  118. 'description' => '外部业务订单ID(与order_id二选一)'
  119. ],
  120. 'order_id' => [
  121. 'type' => 'integer',
  122. 'required' => false,
  123. 'description' => '内部订单ID(与business_id二选一)'
  124. ]
  125. ];
  126. }
  127. /**
  128. * 获取响应参数说明
  129. *
  130. * @return array
  131. */
  132. public function getResponseParameters(): array
  133. {
  134. return [
  135. 'order_id' => [
  136. 'type' => 'integer',
  137. 'description' => '内部订单ID'
  138. ],
  139. 'business_id' => [
  140. 'type' => 'string',
  141. 'description' => '外部业务订单ID'
  142. ],
  143. 'type' => [
  144. 'type' => 'integer',
  145. 'description' => '订单类型(1=转入,2=转出)'
  146. ],
  147. 'type_text' => [
  148. 'type' => 'string',
  149. 'description' => '订单类型描述'
  150. ],
  151. 'status' => [
  152. 'type' => 'integer',
  153. 'description' => '订单状态值'
  154. ],
  155. 'status_text' => [
  156. 'type' => 'string',
  157. 'description' => '订单状态描述'
  158. ],
  159. 'amount' => [
  160. 'type' => 'string',
  161. 'description' => '内部金额'
  162. ],
  163. 'out_amount' => [
  164. 'type' => 'string',
  165. 'description' => '外部金额'
  166. ],
  167. 'exchange_rate' => [
  168. 'type' => 'string',
  169. 'description' => '汇率'
  170. ],
  171. 'user_id' => [
  172. 'type' => 'integer',
  173. 'description' => '用户ID'
  174. ],
  175. 'out_user_id' => [
  176. 'type' => 'string',
  177. 'description' => '外部用户ID'
  178. ],
  179. 'remark' => [
  180. 'type' => 'string',
  181. 'description' => '备注信息'
  182. ],
  183. 'callback_data' => [
  184. 'type' => 'object',
  185. 'description' => '回调数据'
  186. ],
  187. 'error_message' => [
  188. 'type' => 'string',
  189. 'description' => '错误信息(如有)'
  190. ],
  191. 'created_at' => [
  192. 'type' => 'string',
  193. 'description' => '创建时间'
  194. ],
  195. 'processed_at' => [
  196. 'type' => 'string',
  197. 'description' => '处理时间'
  198. ],
  199. 'completed_at' => [
  200. 'type' => 'string',
  201. 'description' => '完成时间'
  202. ]
  203. ];
  204. }
  205. }