fix:getTokenById return token nil, make panic

This commit is contained in:
qinguoyi 2024-08-10 11:21:07 +08:00
parent f9774698e9
commit 6073440919
2 changed files with 12 additions and 7 deletions

View File

@ -17,9 +17,11 @@ func GetSubscription(c *gin.Context) {
if config.DisplayTokenStatEnabled { if config.DisplayTokenStatEnabled {
tokenId := c.GetInt(ctxkey.TokenId) tokenId := c.GetInt(ctxkey.TokenId)
token, err = model.GetTokenById(tokenId) token, err = model.GetTokenById(tokenId)
expiredTime = token.ExpiredTime if err == nil {
remainQuota = token.RemainQuota expiredTime = token.ExpiredTime
usedQuota = token.UsedQuota remainQuota = token.RemainQuota
usedQuota = token.UsedQuota
}
} else { } else {
userId := c.GetInt(ctxkey.Id) userId := c.GetInt(ctxkey.Id)
remainQuota, err = model.GetUserQuota(userId) remainQuota, err = model.GetUserQuota(userId)

View File

@ -254,11 +254,14 @@ func PreConsumeTokenQuota(tokenId int, quota int64) (err error) {
func PostConsumeTokenQuota(tokenId int, quota int64) (err error) { func PostConsumeTokenQuota(tokenId int, quota int64) (err error) {
token, err := GetTokenById(tokenId) token, err := GetTokenById(tokenId)
if quota > 0 { if err == nil {
err = DecreaseUserQuota(token.UserId, quota) if quota > 0 {
} else { err = DecreaseUserQuota(token.UserId, quota)
err = IncreaseUserQuota(token.UserId, -quota) } else {
err = IncreaseUserQuota(token.UserId, -quota)
}
} }
if err != nil { if err != nil {
return err return err
} }