Compare commits

...

3 Commits

Author SHA1 Message Date
JustSong
d17bdc40a7 fix: use transaction to protect redeem process 2023-07-07 21:26:45 +08:00
JustSong
280df27705 docs: update README 2023-07-05 18:43:11 +08:00
JustSong
991f5bf4ee fix: fix http2: invalid Connection request header: [\"upgrade\"]" 2023-07-04 21:24:49 +08:00
3 changed files with 22 additions and 20 deletions

View File

@@ -51,7 +51,7 @@ _✨ All in one 的 OpenAI 接口,整合各种 API 访问方式,开箱即用
<a href="https://iamazing.cn/page/reward">赞赏支持</a> <a href="https://iamazing.cn/page/reward">赞赏支持</a>
</p> </p>
> **Note**:本项目为开源项目,在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及**法律法规**的情况下使用,不得用于非法用途。 > **Note**:本项目为开源项目,使用者必须在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及**法律法规**的情况下使用,不得用于非法用途。
> **Note**:使用 Docker 拉取的最新镜像可能是 `alpha` 版本,如果追求稳定性请手动指定版本。 > **Note**:使用 Docker 拉取的最新镜像可能是 `alpha` 版本,如果追求稳定性请手动指定版本。

View File

@@ -151,7 +151,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
} }
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type")) req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
req.Header.Set("Accept", c.Request.Header.Get("Accept")) req.Header.Set("Accept", c.Request.Header.Get("Accept"))
req.Header.Set("Connection", c.Request.Header.Get("Connection")) //req.Header.Set("Connection", c.Request.Header.Get("Connection"))
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {

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
} }