BuyHandler.php 4.2 KB

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