BuyHandler.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Module\AppGame\Handler\Shop;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Fund\Enums\LOG_TYPE;
  5. use App\Module\Fund\Logic\User as FundUser;
  6. use App\Module\GameItems\Services\ItemService;
  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 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. // TODO: 使用消耗组和奖励组系统进行购买
  52. // 这里需要实现消耗组和奖励组的逻辑,暂时使用旧的逻辑作为占位符
  53. // 暂时的处理:如果商品有消耗组,需要调用消耗组服务
  54. if ($shopItem->consumeGroup) {
  55. // TODO: 调用消耗组服务扣除资源
  56. // $consumeResult = ConsumeGroupService::consume($userId, $shopItem->consume_group_id, $number);
  57. // if (!$consumeResult['success']) {
  58. // throw new LogicException($consumeResult['message']);
  59. // }
  60. // 暂时的占位符逻辑 - 假设扣除100金币
  61. $fundResult = FundUser::handle(
  62. $userId,
  63. 1, // 假设货币类型1是金币
  64. -100 * $number, // 假设每个商品100金币
  65. LOG_TYPE::TRADE,
  66. $goodId,
  67. "购买商品:{$shopItem->name} x {$number}"
  68. );
  69. if (is_string($fundResult)) {
  70. throw new LogicException("购买失败:" . $fundResult);
  71. }
  72. }
  73. // 暂时的处理:如果商品有奖励组,需要调用奖励组服务
  74. if ($shopItem->rewardGroup) {
  75. // TODO: 调用奖励组服务发放奖励
  76. // $rewardResult = RewardGroupService::giveReward($userId, $shopItem->reward_group_id, $number);
  77. // if (!$rewardResult['success']) {
  78. // throw new LogicException($rewardResult['message']);
  79. // }
  80. // 暂时的占位符逻辑 - 假设给予物品ID为1的物品
  81. ItemService::addItem(
  82. $userId,
  83. 1, // 假设物品ID为1
  84. $number, // 数量
  85. [
  86. 'source_type' => 'shop_buy',
  87. 'source_id' => $goodId,
  88. 'details' => [
  89. 'shop_item_id' => $goodId,
  90. 'shop_item_name' => $shopItem->name,
  91. 'quantity' => $number
  92. ]
  93. ]
  94. );
  95. }
  96. // 记录购买记录(总价暂时设为0,因为不再有固定价格)
  97. $shopItem->recordPurchase($userId, $number, 0);
  98. // 提交事务
  99. DB::commit();
  100. // 设置响应状态
  101. $this->response->setCode(0);
  102. $this->response->setMsg('购买成功');
  103. // 记录日志
  104. Log::info('用户购买商品成功', [
  105. 'user_id' => $userId,
  106. 'good_id' => $goodId,
  107. 'number' => $number,
  108. 'consume_group_id' => $shopItem->consume_group_id,
  109. 'reward_group_id' => $shopItem->reward_group_id,
  110. 'shop_item_name' => $shopItem->name
  111. ]);
  112. } catch (\UCore\Exception\ValidateException $e) {
  113. // 验证失败,此时可能还没有开启事务
  114. $this->response->setCode(400);
  115. $this->response->setMsg($e->getMessage());
  116. Log::warning('商品购买验证失败', [
  117. 'user_id' => $userId ?? null,
  118. 'good_id' => $goodId ?? null,
  119. 'number' => $number ?? null,
  120. 'error' => $e->getMessage()
  121. ]);
  122. } catch (LogicException $e) {
  123. // 业务逻辑异常,需要回滚事务
  124. if (DB::transactionLevel() > 0) {
  125. DB::rollBack();
  126. }
  127. $this->response->setCode(400);
  128. $this->response->setMsg($e->getMessage());
  129. Log::warning('用户购买商品失败', [
  130. 'user_id' => $userId ?? null,
  131. 'good_id' => $goodId ?? null,
  132. 'number' => $number ?? null,
  133. 'error' => $e->getMessage()
  134. ]);
  135. } catch (\Exception $e) {
  136. // 系统异常,需要回滚事务
  137. if (DB::transactionLevel() > 0) {
  138. DB::rollBack();
  139. }
  140. $this->response->setCode(500);
  141. $this->response->setMsg('系统错误,请稍后再试');
  142. Log::error('购买商品操作异常', [
  143. 'user_id' => $userId ?? null,
  144. 'good_id' => $goodId ?? null,
  145. 'number' => $number ?? null,
  146. 'error' => $e->getMessage(),
  147. 'trace' => $e->getTraceAsString()
  148. ]);
  149. }
  150. return $response;
  151. }
  152. }