105 lines
2.5 KiB
PHP
105 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace app\common\library;
|
|
|
|
class RedpacketLua
|
|
{
|
|
/**
|
|
* 抢红包Lua脚本
|
|
* 保证原子性操作,防止超抢
|
|
*/
|
|
public static function grabRedpacketScript()
|
|
{
|
|
return <<<LUA
|
|
-- KEYS[1]: 红包key, KEYS[2]: 用户集合key, KEYS[3]: 用户ID
|
|
-- ARGV[1]: 当前时间
|
|
|
|
local redpacketKey = KEYS[1]
|
|
local userSetKey = KEYS[2]
|
|
local userId = KEYS[3]
|
|
local currentTime = tonumber(ARGV[1])
|
|
|
|
-- 检查红包是否存在
|
|
local redpacketData = redis.call('HGETALL', redpacketKey)
|
|
if not redpacketData or #redpacketData == 0 then
|
|
return {0, "红包不存在"}
|
|
end
|
|
|
|
-- 将哈希数据转为table
|
|
local redpacket = {}
|
|
for i = 1, #redpacketData, 2 do
|
|
redpacket[redpacketData[i]] = redpacketData[i + 1]
|
|
end
|
|
|
|
-- 检查红包状态
|
|
local status = tonumber(redpacket['status'])
|
|
local startTime = tonumber(redpacket['start_time'])
|
|
local endTime = tonumber(redpacket['end_time'])
|
|
|
|
if status == 0 then
|
|
if currentTime < startTime then
|
|
return {0, "红包还未开始"}
|
|
else
|
|
-- 更新状态为进行中
|
|
redis.call('HSET', redpacketKey, 'status', 1)
|
|
status = 1
|
|
end
|
|
end
|
|
|
|
if status ~= 1 then
|
|
return {0, "红包已结束"}
|
|
end
|
|
|
|
if currentTime > endTime then
|
|
redis.call('HSET', redpacketKey, 'status', 2)
|
|
return {0, "红包已结束"}
|
|
end
|
|
|
|
-- 检查是否已经抢过
|
|
local hasGrabbed = redis.call('SISMEMBER', userSetKey, userId)
|
|
if hasGrabbed == 1 then
|
|
return {0, "已经抢过该红包"}
|
|
end
|
|
|
|
-- 检查是否还有剩余
|
|
local leftAmount = tonumber(redpacket['left_amount'])
|
|
local leftCount = tonumber(redpacket['left_count'])
|
|
|
|
if leftCount <= 0 or leftAmount <= 0 then
|
|
return {0, "红包已抢完"}
|
|
end
|
|
|
|
-- 计算红包金额
|
|
local amount = 0
|
|
if leftCount == 1 then
|
|
-- 最后一个红包,获得剩余所有金额
|
|
amount = leftAmount
|
|
else
|
|
-- 随机算法:二倍均值法,保证公平性
|
|
local maxAmount = leftAmount / leftCount * 2
|
|
amount = math.random(1, math.floor(maxAmount * 100)) / 100
|
|
-- 确保金额不会超过剩余金额
|
|
if amount > leftAmount then
|
|
amount = leftAmount
|
|
end
|
|
end
|
|
|
|
-- 更新红包数据
|
|
local newLeftAmount = leftAmount - amount
|
|
local newLeftCount = leftCount - 1
|
|
|
|
redis.call('HSET', redpacketKey, 'left_amount', newLeftAmount)
|
|
redis.call('HSET', redpacketKey, 'left_count', newLeftCount)
|
|
|
|
-- 标记用户已抢
|
|
redis.call('SADD', userSetKey, userId)
|
|
|
|
-- 如果抢完了,更新状态
|
|
if newLeftCount == 0 then
|
|
redis.call('HSET', redpacketKey, 'status', 2)
|
|
end
|
|
|
|
return {1, tostring(amount)}
|
|
LUA;
|
|
}
|
|
} |