LotteryOrderHook.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\common\behavior;
  3. use app\common\Service\LotteryChanceService;
  4. /**
  5. * 订单相关抽奖钩子
  6. * 在订单状态变更时自动检查并分发抽奖机会
  7. */
  8. class LotteryOrderHook
  9. {
  10. /**
  11. * 订单支付完成后的钩子
  12. * @param array $params 订单信息
  13. */
  14. public function orderPaid($params)
  15. {
  16. try {
  17. $orderInfo = $params['order'] ?? [];
  18. $userId = $orderInfo['user_id'] ?? 0;
  19. if (!$userId || empty($orderInfo)) {
  20. return;
  21. }
  22. // 构建订单信息用于抽奖条件验证
  23. $lotteryOrderInfo = [
  24. 'id' => $orderInfo['id'] ?? 0,
  25. 'user_id' => $userId,
  26. 'total_amount' => $orderInfo['total_amount'] ?? 0,
  27. 'goods' => $this->getOrderGoods($orderInfo['id'] ?? 0),
  28. 'type' => 'order'
  29. ];
  30. // 检查并分发抽奖机会
  31. $grantedChances = LotteryChanceService::checkAndGrantChanceForOrder($lotteryOrderInfo, $userId);
  32. // 可以在这里添加通知逻辑,告知用户获得了抽奖机会
  33. if (!empty($grantedChances)) {
  34. $this->notifyUserGetChances($userId, $grantedChances);
  35. }
  36. } catch (\Exception $e) {
  37. // 记录错误日志,但不影响订单正常流程
  38. trace('抽奖机会分发失败: ' . $e->getMessage(), 'error');
  39. }
  40. }
  41. /**
  42. * 充值完成后的钩子
  43. * @param array $params 充值信息
  44. */
  45. public function rechargePaid($params)
  46. {
  47. try {
  48. $rechargeInfo = $params['recharge'] ?? [];
  49. $userId = $rechargeInfo['user_id'] ?? 0;
  50. if (!$userId || empty($rechargeInfo)) {
  51. return;
  52. }
  53. // 构建充值信息用于抽奖条件验证
  54. $lotteryRechargeInfo = [
  55. 'id' => $rechargeInfo['id'] ?? 0,
  56. 'user_id' => $userId,
  57. 'amount' => $rechargeInfo['amount'] ?? 0,
  58. 'type' => 'recharge'
  59. ];
  60. // 检查并分发抽奖机会
  61. $grantedChances = LotteryChanceService::checkAndGrantChanceForRecharge($lotteryRechargeInfo, $userId);
  62. // 可以在这里添加通知逻辑,告知用户获得了抽奖机会
  63. if (!empty($grantedChances)) {
  64. $this->notifyUserGetChances($userId, $grantedChances);
  65. }
  66. } catch (\Exception $e) {
  67. // 记录错误日志,但不影响充值正常流程
  68. trace('抽奖机会分发失败: ' . $e->getMessage(), 'error');
  69. }
  70. }
  71. /**
  72. * 获取订单商品信息
  73. * @param int $orderId 订单ID
  74. * @return array
  75. */
  76. private function getOrderGoods($orderId)
  77. {
  78. if (!$orderId) {
  79. return [];
  80. }
  81. try {
  82. $orderGoods = \app\common\model\OrderGoods::where('order_id', $orderId)
  83. ->field('goods_id,goods_sku_id,goods_num')
  84. ->select();
  85. $goods = [];
  86. foreach ($orderGoods as $item) {
  87. $goods[] = [
  88. 'goods_id' => $item->goods_id,
  89. 'goods_sku_id' => $item->goods_sku_id,
  90. 'nums' => $item->goods_num
  91. ];
  92. }
  93. return $goods;
  94. } catch (\Exception $e) {
  95. trace('获取订单商品失败: ' . $e->getMessage(), 'error');
  96. return [];
  97. }
  98. }
  99. /**
  100. * 通知用户获得抽奖机会
  101. * @param int $userId 用户ID
  102. * @param array $grantedChances 获得的抽奖机会
  103. */
  104. private function notifyUserGetChances($userId, $grantedChances)
  105. {
  106. try {
  107. // 这里可以接入消息推送系统
  108. // 例如:站内信、短信、微信消息等
  109. foreach ($grantedChances as $chance) {
  110. $message = sprintf(
  111. '恭喜您获得「%s」%d次抽奖机会,快去参与抽奖吧!',
  112. $chance['activity_name'],
  113. $chance['chances']
  114. );
  115. // 发送站内消息示例
  116. $this->sendInternalMessage($userId, '抽奖机会获得通知', $message);
  117. }
  118. } catch (\Exception $e) {
  119. trace('抽奖机会通知发送失败: ' . $e->getMessage(), 'error');
  120. }
  121. }
  122. /**
  123. * 发送站内消息
  124. * @param int $userId 用户ID
  125. * @param string $title 消息标题
  126. * @param string $content 消息内容
  127. */
  128. private function sendInternalMessage($userId, $title, $content)
  129. {
  130. // 这里需要根据实际的消息系统实现
  131. // 示例代码:
  132. /*
  133. $messageData = [
  134. 'user_id' => $userId,
  135. 'title' => $title,
  136. 'content' => $content,
  137. 'type' => 'lottery',
  138. 'createtime' => time()
  139. ];
  140. \app\common\model\Message::create($messageData);
  141. */
  142. }
  143. }