CancelHandler.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Module\AppGame\Handler\Matchexchange;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\AppGame\Validations\MatchexchangeCancelValidation;
  5. use App\Module\Mex\Services\MexOrderService;
  6. use Google\Protobuf\Internal\Message;
  7. use Uraus\Kku\Request\RequestMatchexchangeCancel;
  8. use Uraus\Kku\Response\ResponseMatchexchangeCancel;
  9. use Uraus\Kku\Common\RESPONSE_CODE;
  10. use UCore\Exception\LogicException;
  11. use Illuminate\Support\Facades\Log;
  12. /**
  13. * 处理取消挂单请求
  14. */
  15. class CancelHandler extends BaseHandler
  16. {
  17. /**
  18. * 是否需要登录
  19. * @var bool
  20. */
  21. protected bool $need_login = true;
  22. /**
  23. * 处理取消挂单请求
  24. *
  25. * @param RequestMatchexchangeCancel $data 取消挂单请求数据
  26. * @return ResponseMatchexchangeCancel 取消挂单响应
  27. */
  28. public function handle(Message $data): Message
  29. {
  30. // 创建验证对象
  31. $validation = MatchexchangeCancelValidation::makeByProrobufUser($data);
  32. // 验证请求数据
  33. $validation->validated();
  34. // 获取验证后的安全数据
  35. $orderId = $validation->getOrderId();
  36. $userId = $validation->getUserId();
  37. // 创建响应对象
  38. $response = new ResponseMatchexchangeCancel();
  39. try {
  40. // 调用服务取消订单
  41. $result = MexOrderService::cancelOrder($userId, $orderId);
  42. // 检查操作结果
  43. if (!$result['success']) {
  44. $this->response->setCode(RESPONSE_CODE::VALIDATE_ERROR);
  45. $this->response->setMsg($result['message'] ?? '取消订单失败');
  46. return $response;
  47. }
  48. // 更新用户活动时间
  49. $this->updateUserActivityTime();
  50. // 设置成功响应
  51. $this->response->setCode(RESPONSE_CODE::OK);
  52. $this->response->setMsg('订单取消成功');
  53. } catch (LogicException $e) {
  54. // 业务逻辑异常
  55. Log::warning('取消挂单业务异常', [
  56. 'user_id' => $userId,
  57. 'order_id' => $orderId,
  58. 'error' => $e->getMessage()
  59. ]);
  60. $this->response->setCode(RESPONSE_CODE::VALIDATE_ERROR);
  61. $this->response->setMsg($e->getMessage());
  62. } catch (\Exception $e) {
  63. // 系统异常
  64. Log::error('取消挂单系统异常', [
  65. 'user_id' => $userId,
  66. 'order_id' => $orderId,
  67. 'error' => $e->getMessage(),
  68. 'trace' => $e->getTraceAsString()
  69. ]);
  70. $this->response->setCode(RESPONSE_CODE::SERVER_ERROR);
  71. $this->response->setMsg('系统繁忙,请稍后重试');
  72. }
  73. return $response;
  74. }
  75. }