支持从计费渠道端点返回日期

This commit is contained in:
ckt1031 2023-07-20 20:48:50 +08:00
parent 4139a7036f
commit be688f71dc
2 changed files with 17 additions and 11 deletions

View File

@ -1,25 +1,27 @@
package controller package controller
import ( import (
"github.com/gin-gonic/gin"
"one-api/common" "one-api/common"
"one-api/model" "one-api/model"
"github.com/gin-gonic/gin"
) )
func GetSubscription(c *gin.Context) { func GetSubscription(c *gin.Context) {
var remainQuota int var quota int
var usedQuota int
var err error var err error
var token *model.Token var expirationDate int64
tokenId := c.GetInt("token_id")
token, err := model.GetTokenById(tokenId)
expirationDate = token.ExpiredTime
if common.DisplayTokenStatEnabled { if common.DisplayTokenStatEnabled {
tokenId := c.GetInt("token_id") quota = token.RemainQuota
token, err = model.GetTokenById(tokenId)
remainQuota = token.RemainQuota
usedQuota = token.UsedQuota
} else { } else {
userId := c.GetInt("id") userId := c.GetInt("id")
remainQuota, err = model.GetUserQuota(userId) quota, err = model.GetUserQuota(userId)
usedQuota, err = model.GetUserUsedQuota(userId)
} }
if err != nil { if err != nil {
openAIError := OpenAIError{ openAIError := OpenAIError{
@ -31,7 +33,6 @@ func GetSubscription(c *gin.Context) {
}) })
return return
} }
quota := remainQuota + usedQuota
amount := float64(quota) amount := float64(quota)
if common.DisplayInCurrencyEnabled { if common.DisplayInCurrencyEnabled {
amount /= common.QuotaPerUnit amount /= common.QuotaPerUnit
@ -45,6 +46,7 @@ func GetSubscription(c *gin.Context) {
SoftLimitUSD: amount, SoftLimitUSD: amount,
HardLimitUSD: amount, HardLimitUSD: amount,
SystemHardLimitUSD: amount, SystemHardLimitUSD: amount,
AccessUntil: expirationDate,
} }
c.JSON(200, subscription) c.JSON(200, subscription)
return return

View File

@ -22,6 +22,7 @@ type OpenAISubscriptionResponse struct {
SoftLimitUSD float64 `json:"soft_limit_usd"` SoftLimitUSD float64 `json:"soft_limit_usd"`
HardLimitUSD float64 `json:"hard_limit_usd"` HardLimitUSD float64 `json:"hard_limit_usd"`
SystemHardLimitUSD float64 `json:"system_hard_limit_usd"` SystemHardLimitUSD float64 `json:"system_hard_limit_usd"`
AccessUntil int64 `json:"access_until"`
} }
type OpenAIUsageDailyCost struct { type OpenAIUsageDailyCost struct {
@ -96,6 +97,9 @@ func GetResponseBody(method, url string, channel *model.Channel, headers http.He
if err != nil { if err != nil {
return nil, err return nil, err
} }
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status code: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return nil, err return nil, err