OpenboxHandler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Module\AppGame\Handler\Item;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\GameItems\Services\ChestService;
  5. use App\Module\GameItems\Validations\ChestOpenValidation;
  6. use Google\Protobuf\Internal\Message;
  7. use Illuminate\Support\Facades\Log;
  8. use Uraus\Kku\Request\RequestItemOpenBox;
  9. use Uraus\Kku\Response\ResponseItemOpenBox;
  10. use UCore\Exception\LogicException;
  11. /**
  12. * 处理宝箱开启请求 - 使用消耗组/奖励组系统
  13. */
  14. class OpenboxHandler extends BaseHandler
  15. {
  16. /**
  17. * 是否需要登录
  18. * @var bool
  19. */
  20. protected bool $need_login = true;
  21. /**
  22. * 处理宝箱开启请求
  23. *
  24. * @param RequestItemOpenbox $data 宝箱开启请求数据
  25. * @return ResponseItemOpenbox 宝箱开启响应
  26. */
  27. public function handle(Message $data): Message
  28. {
  29. // 创建响应对象
  30. $response = new ResponseItemOpenbox();
  31. try {
  32. // 获取请求参数
  33. $itemId = $data->getItemId();
  34. $instanceId = $data->getItemInstanceId();
  35. $selectIds = iterator_to_array($data->getSelectIds()->getIterator());
  36. $userId = $this->user_id;
  37. // 先进行验证,避免不必要的事务开销
  38. $validation = new ChestOpenValidation([
  39. 'user_id' => $userId,
  40. 'item_id' => $itemId,
  41. 'instance_id' => $instanceId,
  42. 'quantity' => 1
  43. ]);
  44. // 验证数据
  45. $validation->validated();
  46. // 使用新宝箱服务开启宝箱
  47. $result = ChestService::openChest($userId, $itemId, 1, [
  48. 'select_ids' => $selectIds,
  49. 'source_type' => 'app_open_box',
  50. 'device_info' => request()->userAgent(),
  51. 'ip_address' => request()->ip(),
  52. ]);
  53. if (!$result || !$result['success']) {
  54. throw new LogicException("开启宝箱失败,请检查宝箱是否存在或是否可开启");
  55. }
  56. // 设置响应状态
  57. $this->response->setCode(0);
  58. $this->response->setMsg('开启宝箱成功');
  59. Log::info('用户开启宝箱成功', [
  60. 'user_id' => $userId,
  61. 'item_id' => $itemId,
  62. 'instance_id' => $instanceId,
  63. 'select_ids' => $selectIds,
  64. 'result' => $result,
  65. 'version' => 'new'
  66. ]);
  67. } catch (\UCore\Exception\ValidateException $e) {
  68. // 验证失败
  69. $this->response->setCode(400);
  70. $this->response->setMsg($e->getMessage());
  71. Log::warning('宝箱开启验证失败', [
  72. 'user_id' => $userId ?? null,
  73. 'item_id' => $itemId ?? null,
  74. 'instance_id' => $instanceId ?? null,
  75. 'error' => $e->getMessage()
  76. ]);
  77. } catch (LogicException $e) {
  78. // 业务逻辑异常
  79. $this->response->setCode(400);
  80. $this->response->setMsg($e->getMessage());
  81. Log::warning('用户开启宝箱失败', [
  82. 'user_id' => $userId ?? null,
  83. 'item_id' => $itemId ?? null,
  84. 'instance_id' => $instanceId ?? null,
  85. 'error' => $e->getMessage()
  86. ]);
  87. } catch (\Exception $e) {
  88. // 系统异常
  89. $this->response->setCode(500);
  90. $this->response->setMsg('系统错误,请稍后再试');
  91. Log::error('开启宝箱操作异常', [
  92. 'user_id' => $userId ?? null,
  93. 'item_id' => $itemId ?? null,
  94. 'instance_id' => $instanceId ?? null,
  95. 'error' => $e->getMessage(),
  96. 'trace' => $e->getTraceAsString()
  97. ]);
  98. }
  99. return $response;
  100. }
  101. }