$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); */ } }