feat: publish sanitized custom.97 source

This commit is contained in:
cneicy committed 2026-10-27 20:43:39 +08:00
1 parent 8e6ed0317b
commit 86c0276156
1022 files changed
+69903 -19207

No files matched your search

+11 -12
View File
@@ -2,7 +2,6 @@ package model
import (
"errors"
"fmt"
"strconv"
"github.com/QuantumNous/new-api/common"
@@ -126,7 +125,7 @@ func SearchRedemptions(keyword string, status string, startIdx int, num int) (re
func GetRedemptionById(id int) (*Redemption, error) {
if id == 0 {
return nil, errors.New("id 为空!")
return nil, common.NewMessage("ID is empty")
}
redemption := Redemption{Id: id}
var err error = nil
@@ -136,10 +135,10 @@ func GetRedemptionById(id int) (*Redemption, error) {
func Redeem(key string, userId int) (quota int, err error) {
if key == "" {
return 0, errors.New("未提供兑换码")
return 0, errors.New("redemption code not provided")
}
if userId == 0 {
return 0, errors.New("无效的 user id")
return 0, errors.New("invalid user id")
}
redemption := &Redemption{}
@@ -151,13 +150,13 @@ func Redeem(key string, userId int) (quota int, err error) {
err = DB.Transaction(func(tx *gorm.DB) error {
err := lockForUpdate(tx).Where(keyCol+" = ?", key).First(redemption).Error
if err != nil {
return errors.New("无效的兑换码")
return errors.New("invalid redemption code")
}
if redemption.Status != common.RedemptionCodeStatusEnabled {
return errors.New("该兑换码已被使用")
return errors.New("redemption code already used")
}
if redemption.ExpiredTime != 0 && redemption.ExpiredTime < common.GetTimestamp() {
return errors.New("该兑换码已过期")
return errors.New("redemption code expired")
}
// Compare-and-swap on status: only the transaction that flips
// enabled -> used may credit quota, so a concurrent redeem of the
@@ -173,16 +172,16 @@ func Redeem(key string, userId int) (quota int, err error) {
return result.Error
}
if result.RowsAffected == 0 {
return errors.New("该兑换码已被使用")
return errors.New("redemption code already used")
}
return creditTopUpQuota(tx, userId, redemption.Quota, nil)
})
if err != nil {
common.SysError("redemption failed: " + err.Error())
common.SysError(common.LogText("redemption failed: %s", err.Error()))
return 0, ErrRedeemFailed
}
syncCreditUserQuotaCache(userId, redemption.Quota, "redemption")
RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s,兑换码ID %d", logger.LogQuota(redemption.Quota), redemption.Id))
syncCreditUserQuotaCache(userId, redemption.Quota, common.LogText("redemption"))
RecordLog(userId, LogTypeTopup, common.NewMessage("Topped up {{quota}} with a redemption code, code ID {{id}}", map[string]any{"quota": logger.FormatQuota(redemption.Quota), "id": redemption.Id}))
return redemption.Quota, nil
}
@@ -224,7 +223,7 @@ func (redemption *Redemption) Delete() error {
func DeleteRedemptionById(id int) (err error) {
if id == 0 {
return errors.New("id 为空!")
return common.NewMessage("ID is empty")
}
redemption := Redemption{Id: id}
err = DB.Where(redemption).First(&redemption).Error