fix: use transaction to protect redeem process

This commit is contained in:
JustSong 2023-07-07 21:26:45 +08:00
parent 280df27705
commit d17bdc40a7

View File

@ -3,6 +3,7 @@ package model
import ( import (
"errors" "errors"
"fmt" "fmt"
"gorm.io/gorm"
"one-api/common" "one-api/common"
) )
@ -48,26 +49,27 @@ func Redeem(key string, userId int) (quota int, err error) {
return 0, errors.New("无效的 user id") return 0, errors.New("无效的 user id")
} }
redemption := &Redemption{} redemption := &Redemption{}
err = DB.Where("`key` = ?", key).First(redemption).Error
if err != nil { err = DB.Transaction(func(tx *gorm.DB) error {
return 0, errors.New("无效的兑换码") err := DB.Where("`key` = ?", key).First(redemption).Error
} if err != nil {
if redemption.Status != common.RedemptionCodeStatusEnabled { return errors.New("无效的兑换码")
return 0, errors.New("该兑换码已被使用") }
} if redemption.Status != common.RedemptionCodeStatusEnabled {
err = IncreaseUserQuota(userId, redemption.Quota) return errors.New("该兑换码已被使用")
if err != nil { }
return 0, err err = DB.Model(&User{}).Where("id = ?", userId).Update("quota", gorm.Expr("quota + ?", redemption.Quota)).Error
} if err != nil {
go func() { return err
}
redemption.RedeemedTime = common.GetTimestamp() redemption.RedeemedTime = common.GetTimestamp()
redemption.Status = common.RedemptionCodeStatusUsed redemption.Status = common.RedemptionCodeStatusUsed
err := redemption.SelectUpdate() return redemption.SelectUpdate()
if err != nil { })
common.SysError("failed to update redemption status: " + err.Error()) if err != nil {
} return 0, errors.New("兑换失败," + err.Error())
RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s", common.LogQuota(redemption.Quota))) }
}() RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s", common.LogQuota(redemption.Quota)))
return redemption.Quota, nil return redemption.Quota, nil
} }