This commit is contained in:
2025-10-11 14:49:25 +08:00
parent ac9182c566
commit 7415193bba
5 changed files with 68 additions and 127 deletions

View File

@@ -30,7 +30,7 @@ class RedpacketService
/**
* 抢红包并返回详细结果
*/
public function grabWithResult($redpacketId, $userId, $password = '')
public function grabWithResult($redpacketId, $userId)
{
$redpacketModel = new Redpacket();
$redpacket = $redpacketModel->getRedpacketInfo($redpacketId);
@@ -43,17 +43,6 @@ class RedpacketService
];
}
// 验证口令红包
if ($redpacket['type'] == Redpacket::TYPE_PASSWORD) {
if (empty($password) || $password != $redpacket['password']) {
return [
'code' => 0,
'msg' => '口令错误',
'data' => null
];
}
}
// 验证领取条件
$conditionCheck = $this->checkConditionsWithResult($redpacket, $userId);
if ($conditionCheck['code'] != 1) {
@@ -129,7 +118,8 @@ class RedpacketService
'change_value' => $amount,
'room_id' => $redpacket['room_id'],
'money_type' => $redpacket['coin_type'],
'change_type' => 66,//抢红包收入
//记录日志 32-发红包金币29-发红包钻石30-抢红包金币31-抢红包(钻石)
'change_type' => $redpacket['coin_type'] == 1 ? 30 : 31,
'from_id' => $redpacket['room_id'],
'remarks' => '抢红包收入',
'createtime' => time()
@@ -153,9 +143,8 @@ class RedpacketService
$grabResult = $this->getGrabResult($redpacketId, $userId);
return [
'success' => true,
'code' => 'grab_success',
'message' => '抢红包成功',
'code' => 1,
'msg' => '抢红包成功',
'data' => $grabResult
];
@@ -167,9 +156,9 @@ class RedpacketService
$redis->sRem($userSetKey, $userId);
return [
'success' => false,
'code' => 'system_error',
'message' => '系统错误,请重试'
'code' => 0,
'msg' => '系统错误,请重试',
'data' => null
];
}
}
@@ -357,85 +346,6 @@ class RedpacketService
return ['code' => 1];
}
/**
* 抢红包
*/
// public function grab($redpacketId, $userId, $password = '')
// {
// $redpacketModel = new Redpacket();
// $redpacket = $redpacketModel->getRedpacketInfo($redpacketId);
//
// if (!$redpacket) {
// throw new \Exception('红包不存在');
// }
//
// // 验证口令红包
// if ($redpacket['type'] == Redpacket::TYPE_PASSWORD) {
// if (empty($password) || $password != $redpacket['password']) {
// throw new \Exception('口令错误');
// }
// }
//
// // 验证领取条件
// $this->checkConditions($redpacket, $userId);
//
// // 使用Redis+Lua保证原子性操作
// $redis = Cache::store('redis')->handler();
// $script = RedpacketLua::grabRedpacketScript();
//
// $redpacketKey = "redpacket:{$redpacketId}";
// $userSetKey = "redpacket_users:{$redpacketId}";
//
// $result = $redis->eval($script, [
// $redpacketKey,
// $userSetKey,
// $userId,
// time()
// ], 3);
//
// if ($result[0] == 0) {
// throw new \Exception($result[1]);
// }
//
// $amount = floatval($result[1]);
//
// // Lua脚本执行成功记录到数据库
// Db::startTrans();
// try {
// // 创建领取记录
// $recordData = [
// 'redpacket_id' => $redpacketId,
// 'user_id' => $userId,
// 'amount' => $amount
// ];
//
// $recordModel = new RedpacketRecord();
// $recordModel->save($recordData);
//
// // 更新用户钱包
// $walletModel = new UserWallet();
// $walletModel->increaseBalance($userId, $redpacket['coin_type'], $amount);
//
// // 更新红包剩余数量和金额
// Db::name('redpacket')
// ->where('id', $redpacketId)
// ->dec('left_amount', $amount)
// ->dec('left_count', 1)
// ->update();
//
// Db::commit();
//
// return $amount;
//
// } catch (\Exception $e) {
// Db::rollback();
// // 回滚Redis操作
// $redis->hIncrByFloat($redpacketKey, 'left_amount', $amount);
// $redis->hIncrBy($redpacketKey, 'left_count', 1);
// $redis->sRem($userSetKey, $userId);
// throw $e;
// }
// }
/**
* 获取红包详情和领取记录