BuyHandler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Module\AppGame\Handler\Shop;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Game\Services\ConsumeService;
  5. use App\Module\Game\Services\RewardService;
  6. use App\Module\Shop\Validations\ShopBuyValidation;
  7. use Google\Protobuf\Internal\Message;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use UCore\Helper\Logger;
  11. use Uraus\Kku\Request\RequestShopBuy;
  12. use Uraus\Kku\Response\ResponseShopBuy;
  13. use UCore\Exception\LogicException;
  14. /**
  15. * 处理商品购买请求
  16. */
  17. class BuyHandler extends BaseHandler
  18. {
  19. /**
  20. * 是否需要登录
  21. * @var bool
  22. */
  23. protected bool $need_login = true;
  24. /**
  25. * 处理商品购买请求
  26. *
  27. * @param RequestShopBuy $data 商品购买请求数据
  28. * @return ResponseShopBuy 商品购买响应
  29. */
  30. public function handle(Message $data): Message
  31. {
  32. // 创建响应对象
  33. $response = new ResponseShopBuy();
  34. try {
  35. // 获取请求参数
  36. $goodId = $data->getGoodId();
  37. $number = $data->getNumber();
  38. $userId = $this->user_id;
  39. // 先进行验证,避免不必要的事务开销
  40. $validation = new ShopBuyValidation([
  41. 'user_id' => $userId,
  42. 'good_id' => $goodId,
  43. 'number' => $number
  44. ]);
  45. // 验证数据
  46. $validation->validated();
  47. // 从验证结果中获取商品信息
  48. $shopItem = $validation->shop_item;
  49. // 验证通过后,开启事务
  50. DB::beginTransaction();
  51. // 执行消耗组逻辑(如果商品有消耗组)
  52. if ($shopItem->consume_group_id) {
  53. // 循环执行消耗,支持购买多个数量
  54. for ($i = 0; $i < $number; $i++) {
  55. $consumeResult = ConsumeService::executeConsume(
  56. $userId,
  57. $shopItem->consume_group_id,
  58. 'shop_buy',
  59. $goodId,
  60. false // 不重复检查,因为验证阶段已经检查过
  61. );
  62. if (!$consumeResult->success) {
  63. throw new LogicException("消耗资源失败:" . $consumeResult->message);
  64. }
  65. }
  66. }
  67. // 执行奖励组逻辑(如果商品有奖励组)
  68. if ($shopItem->reward_group_id) {
  69. // 循环发放奖励,支持购买多个数量
  70. for ($i = 0; $i < $number; $i++) {
  71. $rewardResult = RewardService::grantReward(
  72. $userId,
  73. $shopItem->reward_group_id,
  74. 'shop_buy',
  75. $goodId
  76. );
  77. if (!$rewardResult->success) {
  78. throw new LogicException("发放奖励失败:" . $rewardResult->errorMessage);
  79. }
  80. }
  81. }
  82. // 记录购买记录(总价暂时设为0,因为不再有固定价格)
  83. $shopItem->recordPurchase($userId, $number, 0);
  84. // 更新限购计数
  85. $shopItem->updatePurchaseLimitCounters($userId, $number);
  86. // 提交事务
  87. DB::commit();
  88. // 记录日志
  89. Log::info('用户购买商品成功', [
  90. 'user_id' => $userId,
  91. 'good_id' => $goodId,
  92. 'number' => $number,
  93. 'consume_group_id' => $shopItem->consume_group_id,
  94. 'reward_group_id' => $shopItem->reward_group_id,
  95. 'shop_item_name' => $shopItem->name,
  96. 'has_consume_group' => !empty($shopItem->consume_group_id),
  97. 'has_reward_group' => !empty($shopItem->reward_group_id)
  98. ]);
  99. } catch (\Exception $e) {
  100. // 系统异常,需要回滚事务
  101. if (DB::transactionLevel() > 0) {
  102. DB::rollBack();
  103. }
  104. Logger::exception('购买商品操作异常', $e, [
  105. 'user_id' => $userId ?? null,
  106. 'good_id' => $goodId ?? null,
  107. 'number' => $number ?? null,
  108. ]);
  109. // 重新抛出异常,交由框架处理
  110. throw $e;
  111. }
  112. return $response;
  113. }
  114. }