123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace app\common\behavior;
- use app\common\Service\LotteryChanceService;
- /**
- * 订单相关抽奖钩子
- * 在订单状态变更时自动检查并分发抽奖机会
- */
- class LotteryOrderHook
- {
- /**
- * 订单支付完成后的钩子
- * @param array $params 订单信息
- */
- public function orderPaid($params)
- {
- try {
- $orderInfo = $params['order'] ?? [];
- $userId = $orderInfo['user_id'] ?? 0;
-
- if (!$userId || empty($orderInfo)) {
- return;
- }
- // 构建订单信息用于抽奖条件验证
- $lotteryOrderInfo = [
- 'id' => $orderInfo['id'] ?? 0,
- 'user_id' => $userId,
- 'total_amount' => $orderInfo['total_amount'] ?? 0,
- 'goods' => $this->getOrderGoods($orderInfo['id'] ?? 0),
- 'type' => 'order'
- ];
- // 检查并分发抽奖机会
- $grantedChances = LotteryChanceService::checkAndGrantChanceForOrder($lotteryOrderInfo, $userId);
-
- // 可以在这里添加通知逻辑,告知用户获得了抽奖机会
- if (!empty($grantedChances)) {
- $this->notifyUserGetChances($userId, $grantedChances);
- }
-
- } catch (\Exception $e) {
- // 记录错误日志,但不影响订单正常流程
- trace('抽奖机会分发失败: ' . $e->getMessage(), 'error');
- }
- }
- /**
- * 充值完成后的钩子
- * @param array $params 充值信息
- */
- public function rechargePaid($params)
- {
- try {
- $rechargeInfo = $params['recharge'] ?? [];
- $userId = $rechargeInfo['user_id'] ?? 0;
-
- if (!$userId || empty($rechargeInfo)) {
- return;
- }
- // 构建充值信息用于抽奖条件验证
- $lotteryRechargeInfo = [
- 'id' => $rechargeInfo['id'] ?? 0,
- 'user_id' => $userId,
- 'amount' => $rechargeInfo['amount'] ?? 0,
- 'type' => 'recharge'
- ];
- // 检查并分发抽奖机会
- $grantedChances = LotteryChanceService::checkAndGrantChanceForRecharge($lotteryRechargeInfo, $userId);
-
- // 可以在这里添加通知逻辑,告知用户获得了抽奖机会
- if (!empty($grantedChances)) {
- $this->notifyUserGetChances($userId, $grantedChances);
- }
-
- } catch (\Exception $e) {
- // 记录错误日志,但不影响充值正常流程
- trace('抽奖机会分发失败: ' . $e->getMessage(), 'error');
- }
- }
- /**
- * 获取订单商品信息
- * @param int $orderId 订单ID
- * @return array
- */
- private function getOrderGoods($orderId)
- {
- if (!$orderId) {
- return [];
- }
- try {
- $orderGoods = \app\common\model\OrderGoods::where('order_id', $orderId)
- ->field('goods_id,goods_sku_id,goods_num')
- ->select();
-
- $goods = [];
- foreach ($orderGoods as $item) {
- $goods[] = [
- 'goods_id' => $item->goods_id,
- 'goods_sku_id' => $item->goods_sku_id,
- 'nums' => $item->goods_num
- ];
- }
-
- return $goods;
- } catch (\Exception $e) {
- trace('获取订单商品失败: ' . $e->getMessage(), 'error');
- return [];
- }
- }
- /**
- * 通知用户获得抽奖机会
- * @param int $userId 用户ID
- * @param array $grantedChances 获得的抽奖机会
- */
- private function notifyUserGetChances($userId, $grantedChances)
- {
- try {
- // 这里可以接入消息推送系统
- // 例如:站内信、短信、微信消息等
-
- foreach ($grantedChances as $chance) {
- $message = sprintf(
- '恭喜您获得「%s」%d次抽奖机会,快去参与抽奖吧!',
- $chance['activity_name'],
- $chance['chances']
- );
-
- // 发送站内消息示例
- $this->sendInternalMessage($userId, '抽奖机会获得通知', $message);
- }
-
- } catch (\Exception $e) {
- trace('抽奖机会通知发送失败: ' . $e->getMessage(), 'error');
- }
- }
- /**
- * 发送站内消息
- * @param int $userId 用户ID
- * @param string $title 消息标题
- * @param string $content 消息内容
- */
- private function sendInternalMessage($userId, $title, $content)
- {
- // 这里需要根据实际的消息系统实现
- // 示例代码:
- /*
- $messageData = [
- 'user_id' => $userId,
- 'title' => $title,
- 'content' => $content,
- 'type' => 'lottery',
- 'createtime' => time()
- ];
-
- \app\common\model\Message::create($messageData);
- */
- }
- }
|