fix 领红包加锁
This commit is contained in:
@@ -7,6 +7,7 @@ import com.tailbet.model.entity.RedPacketRecv;
|
||||
import com.tailbet.model.vo.R;
|
||||
import com.tailbet.model.vo.UserContext;
|
||||
import com.tailbet.service.IRedPacketService;
|
||||
import com.tailbet.util.RedisLockUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -42,7 +43,12 @@ public class RedPacketController {
|
||||
public R<RedPacketRecv> receiveRp(@RequestBody ReceiveRpDTO dto) {
|
||||
try {
|
||||
Long userId = UserContext.getUserId();
|
||||
var recv = redPacketService.receiveRp(dto.getRpId(), userId);
|
||||
// 使用分布式锁防止多领
|
||||
var recv = RedisLockUtil.lock("rp:receive:" + dto.getRpId(),
|
||||
() -> redPacketService.receiveRp(dto.getRpId(), userId));
|
||||
if (recv == null) {
|
||||
return R.fail("领取失败,请重试");
|
||||
}
|
||||
return R.ok(recv);
|
||||
} catch (RuntimeException e) {
|
||||
return R.fail(e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user