From eccb8c3cb3e9db477a9384095fafd31b6f132b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=92=8A?= Date: Sat, 27 Sep 2025 09:19:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=B6=85=E5=87=BA=E5=8F=91?= =?UTF-8?q?=E6=94=BE=E7=A4=BC=E7=89=A9=E9=97=AE=E9=A2=98-=E7=9B=B2?= =?UTF-8?q?=E7=9B=92=E8=BD=AC=E7=9B=98-=E5=8A=A0Redis=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/model/BlindBoxTurntableGiftDraw.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/application/api/model/BlindBoxTurntableGiftDraw.php b/application/api/model/BlindBoxTurntableGiftDraw.php index d1331ea..0ddc041 100644 --- a/application/api/model/BlindBoxTurntableGiftDraw.php +++ b/application/api/model/BlindBoxTurntableGiftDraw.php @@ -7,12 +7,22 @@ use think\Log; use think\Model; use think\Db; use think\Session; +use think\cache\driver\Redis; + /* * 盲盒转盘优化后方法 * */ class BlindBoxTurntableGiftDraw extends Model { + private $redis; + + public function __construct() + { + parent::__construct(); + $this->redis = new Redis(['database' => 1]); + } + /** * 重构后的抽奖方法 - 优化响应速度 */ @@ -48,8 +58,11 @@ class BlindBoxTurntableGiftDraw extends Model $currentXlhPeriodsNum = $precomputeResult['data']['current_xlh_periods_num']; $xlhIsPiaoPing = $precomputeResult['data']['xlh_is_piao_ping']; $expectedCount = count(explode(',', $gift_user_ids)) * $num; + if(count($precomputedResults) != $expectedCount){ Log::record('抽奖结果数量与预期数量不一致,请重试!预期: '.$expectedCount.', 实际: '.count($precomputedResults).': ' . $room_id."【数据】".var_export($precomputedResults, true),"infos-mhzp"); + // 记录错误到Redis + $this->recordDrawErrorToRedis($expectedCount, count($precomputedResults), $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults); return ['code' => 0, 'msg' => '抽奖结果数量与预期数量不一致,请重试! ', 'data' => null]; } @@ -1547,4 +1560,59 @@ class BlindBoxTurntableGiftDraw extends Model return ['code' => 1, 'msg' => '成功', 'data' => $result_list]; } + //错误日志记录------------------------------------------------------- + /** + * 记录抽奖错误日志到Redis + */ + private function recordDrawErrorToRedis($expectedCount, $actualCount, $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults) + { + $errorData = [ + 'timestamp' => time(), + 'expected_count' => $expectedCount, + 'actual_count' => $actualCount, + 'room_id' => $room_id, + 'user_id' => $user_id, + 'gift_bag_id' => $gift_bag_id, + 'num' => $num, + 'gift_user_ids' => $gift_user_ids, + 'precomputed_results_count' => count($precomputedResults), + 'error_type' => 'draw_count_mismatch' + ]; + + // 将错误信息推送到Redis列表中 + $this->redis->lpush('blind_box_draw_errors', json_encode($errorData)); + + // 同时记录到哈希结构,便于按房间ID或用户ID查询 + $errorKey = 'blind_box_error:' . $room_id . ':' . time() . ':' . uniqid(); + $this->redis->hmset($errorKey, $errorData); + $this->redis->expire($errorKey, 86400 * 7); // 设置1天过期时间 + } + + /** + * 获取抽奖错误统计信息 + */ + public function getDrawErrorStats() + { + $errors = $this->redis->lrange('blind_box_draw_errors', 0, 99); // 获取最近100条错误记录 + $stats = [ + 'total_errors' => $this->redis->llen('blind_box_draw_errors'), + 'recent_errors' => [], + 'error_distribution' => [] + ]; + + foreach ($errors as $error) { + $errorData = json_decode($error, true); + $stats['recent_errors'][] = $errorData; + + // 统计错误分布 + $errorType = $errorData['error_type'] ?? 'unknown'; + if (!isset($stats['error_distribution'][$errorType])) { + $stats['error_distribution'][$errorType] = 0; + } + $stats['error_distribution'][$errorType]++; + } + + return $stats; + } + }