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

@@ -55,11 +55,27 @@ class Redpacket extends Model
}
// 扣除余额
Db::name('user_wallet')
$delres = Db::name('user_wallet')
->where('user_id', $data['user_id'])
->dec($coinField, $data['total_amount'])
->update();
//记录日志 32-发红包金币29-发红包钻石30-抢红包金币31-抢红包(钻石)
//记录用户金币日志
$data = [
'user_id' => $data['user_id'],
'change_value' => $data['total_amount'],
'room_id' => $data['room_id'],
'money_type' => $data['coin_type'],
'change_type' => $coinField == self::COIN_GOLD ? 32 : 29,
'from_id' => $data['room_id'],
'remarks' => $coinField == self::COIN_GOLD ? '金币(发红包)' : '钻石(发红包)',
'createtime' => time()
];
$res = Db::name('vs_user_money_log')->insert($data);
if(!$res || !$delres){
Db::rollback();
}
// 计算开始时间
$startTime = $data['countdown'] > 0 ? (time() + $data['countdown']) : time();
$endTime = $startTime + 120; // 2分钟后结束

View File

@@ -78,6 +78,14 @@ class UserWallet extends Model
//新人充值好礼
const NEW_USER_CHARGE_GIFT = 28;
//发红包(金币)
const RED_PACKET_COIN = 32;
//发红包(钻石)
const RED_PACKET_DIAMOND = 29;
//抢红包(金币)
const RED_PACKET_COIN_RECEIVE = 30;
//抢红包(钻石)
const RED_PACKET_DIAMOND_RECEIVE = 31;
//金币支出类型数组
public $coin_consumption_type_array = [
@@ -86,12 +94,14 @@ class UserWallet extends Model
self::OPERATION_GIFT,
self::GUILD_EXIT,
self::HEADLINE_REWARD,
self::TRANSFER_COIN
self::TRANSFER_COIN,
self::RED_PACKET_COIN,
];
//钻石支出类型数组
public $diamond_consumption_type_array = [
self::OPERATION_WITHDRAW,
self::MONEY_CONVERSION
self::MONEY_CONVERSION,
self::RED_PACKET_DIAMOND
];
@@ -141,7 +151,11 @@ class UserWallet extends Model
self::TRANSFER_COIN => '赠送好友金币',
self::RECEIVE_COIN => '好友转赠所得金币',
self::HOUR_RANK_COIN => '小时榜获得金币',
self::NEW_USER_CHARGE_GIFT => '新人充值好礼'
self::NEW_USER_CHARGE_GIFT => '新人充值好礼',
self::RED_PACKET_COIN => '发红包(金币)',
self::RED_PACKET_DIAMOND => '发红包(钻石)',
self::RED_PACKET_COIN_RECEIVE => '抢红包(金币)',
self::RED_PACKET_DIAMOND_RECEIVE => '抢红包(钻石)',
];
return $status[$type] ?? '';
}

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;
// }
// }
/**
* 获取红包详情和领取记录