CancelHandler.php 2.0 KB

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