解决超出发放礼物问题-巡乐会-日志加缓存
This commit is contained in:
@@ -7,7 +7,8 @@ use think\Log;
|
|||||||
use think\Model;
|
use think\Model;
|
||||||
use think\Db;
|
use think\Db;
|
||||||
use think\Session;
|
use think\Session;
|
||||||
use think\cache\driver\Redis;
|
//use think\cache\driver\Redis;
|
||||||
|
use Redis;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 盲盒转盘优化后方法
|
* 盲盒转盘优化后方法
|
||||||
@@ -20,7 +21,16 @@ class BlindBoxTurntableGiftDraw extends Model
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->redis = new Redis(['database' => 1]);
|
try {
|
||||||
|
$this->redis = new Redis();
|
||||||
|
// 连接到Redis服务器
|
||||||
|
$this->redis->connect(config('redis.host'), config('redis.port')); // 根据实际配置调整主机和端口
|
||||||
|
// 选择数据库1
|
||||||
|
$this->redis->select(1);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::record('Redis连接失败: ' . $e->getMessage(), 'error');
|
||||||
|
$this->redis = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,7 +70,6 @@ class BlindBoxTurntableGiftDraw extends Model
|
|||||||
$expectedCount = count(explode(',', $gift_user_ids)) * $num;
|
$expectedCount = count(explode(',', $gift_user_ids)) * $num;
|
||||||
|
|
||||||
if(count($precomputedResults) != $expectedCount){
|
if(count($precomputedResults) != $expectedCount){
|
||||||
Log::record('抽奖结果数量与预期数量不一致,请重试!预期: '.$expectedCount.', 实际: '.count($precomputedResults).': ' . $room_id."【数据】".var_export($precomputedResults, true),"infos-mhzp");
|
|
||||||
// 记录错误到Redis
|
// 记录错误到Redis
|
||||||
$this->recordDrawErrorToRedis($expectedCount, count($precomputedResults), $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults);
|
$this->recordDrawErrorToRedis($expectedCount, count($precomputedResults), $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults);
|
||||||
return ['code' => 0, 'msg' => '抽奖结果数量与预期数量不一致,请重试! ', 'data' => null];
|
return ['code' => 0, 'msg' => '抽奖结果数量与预期数量不一致,请重试! ', 'data' => null];
|
||||||
@@ -1566,21 +1575,32 @@ class BlindBoxTurntableGiftDraw extends Model
|
|||||||
*/
|
*/
|
||||||
private function recordDrawErrorToRedis($expectedCount, $actualCount, $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults)
|
private function recordDrawErrorToRedis($expectedCount, $actualCount, $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults)
|
||||||
{
|
{
|
||||||
$errorData = [
|
// 检查Redis连接是否可用
|
||||||
'timestamp' => time(),
|
if (!$this->redis) {
|
||||||
'expected_count' => $expectedCount,
|
Log::record('Redis连接不可用,无法记录错误日志', 'error');
|
||||||
'actual_count' => $actualCount,
|
return;
|
||||||
'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),
|
|
||||||
'precomputed_results' => $precomputedResults,
|
|
||||||
'error_type' => 'draw_count_mismatch'
|
|
||||||
];
|
|
||||||
|
|
||||||
// 将错误信息推送到Redis列表中
|
try {
|
||||||
$this->redis->set('blind_box_draw_errors', json_encode($errorData),86400 * 7);
|
$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),
|
||||||
|
'precomputed_results' => $precomputedResults,
|
||||||
|
'error_type' => 'draw_count_mismatch'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 使用正确的Redis方法存储数据
|
||||||
|
$key = 'blind_box_draw_errors_' . date('Y-m-d-H-i-s');
|
||||||
|
$this->redis->setex($key, 86400 * 7, json_encode($errorData));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::record('Redis操作失败: ' . $e->getMessage(), 'error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user