From 60734409191e9996c7a166952133c426b5a9fb2a Mon Sep 17 00:00:00 2001 From: qinguoyi <1532979219@qq.com> Date: Sat, 10 Aug 2024 11:21:07 +0800 Subject: [PATCH] fix:getTokenById return token nil, make panic --- controller/billing.go | 8 +++++--- model/token.go | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/controller/billing.go b/controller/billing.go index 0d03e4c1..e837157f 100644 --- a/controller/billing.go +++ b/controller/billing.go @@ -17,9 +17,11 @@ func GetSubscription(c *gin.Context) { if config.DisplayTokenStatEnabled { tokenId := c.GetInt(ctxkey.TokenId) token, err = model.GetTokenById(tokenId) - expiredTime = token.ExpiredTime - remainQuota = token.RemainQuota - usedQuota = token.UsedQuota + if err == nil { + expiredTime = token.ExpiredTime + remainQuota = token.RemainQuota + usedQuota = token.UsedQuota + } } else { userId := c.GetInt(ctxkey.Id) remainQuota, err = model.GetUserQuota(userId) diff --git a/model/token.go b/model/token.go index 96e6b491..eb69bac5 100644 --- a/model/token.go +++ b/model/token.go @@ -254,11 +254,14 @@ func PreConsumeTokenQuota(tokenId int, quota int64) (err error) { func PostConsumeTokenQuota(tokenId int, quota int64) (err error) { token, err := GetTokenById(tokenId) - if quota > 0 { - err = DecreaseUserQuota(token.UserId, quota) - } else { - err = IncreaseUserQuota(token.UserId, -quota) + if err == nil { + if quota > 0 { + err = DecreaseUserQuota(token.UserId, quota) + } else { + err = IncreaseUserQuota(token.UserId, -quota) + } } + if err != nil { return err }